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)) { |
| 494 | if (PyUnicode_READY(unicode) < 0) |
| 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 | |
| 767 | if (PyUnicode_READY(unicode) < 0) |
| 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 { |
| 1001 | kind_state = PyUnicode_4BYTE_KIND; |
| 1002 | char_size = 4; |
| 1003 | if (sizeof(wchar_t) == 4) |
| 1004 | is_sharing = 1; |
| 1005 | } |
| 1006 | |
| 1007 | /* Ensure we won't overflow the size. */ |
| 1008 | if (size < 0) { |
| 1009 | PyErr_SetString(PyExc_SystemError, |
| 1010 | "Negative size passed to PyUnicode_New"); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1014 | return PyErr_NoMemory(); |
| 1015 | |
| 1016 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1017 | * PyObject_New() so we are able to allocate space for the object and |
| 1018 | * it's data buffer. |
| 1019 | */ |
| 1020 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1021 | if (obj == NULL) |
| 1022 | return PyErr_NoMemory(); |
| 1023 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1024 | if (obj == NULL) |
| 1025 | return NULL; |
| 1026 | |
| 1027 | unicode = (PyCompactUnicodeObject *)obj; |
| 1028 | if (is_ascii) |
| 1029 | data = ((PyASCIIObject*)obj) + 1; |
| 1030 | else |
| 1031 | data = unicode + 1; |
| 1032 | _PyUnicode_LENGTH(unicode) = size; |
| 1033 | _PyUnicode_HASH(unicode) = -1; |
| 1034 | _PyUnicode_STATE(unicode).interned = 0; |
| 1035 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 1036 | _PyUnicode_STATE(unicode).compact = 1; |
| 1037 | _PyUnicode_STATE(unicode).ready = 1; |
| 1038 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1039 | if (is_ascii) { |
| 1040 | ((char*)data)[size] = 0; |
| 1041 | _PyUnicode_WSTR(unicode) = NULL; |
| 1042 | } |
| 1043 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 1044 | ((char*)data)[size] = 0; |
| 1045 | _PyUnicode_WSTR(unicode) = NULL; |
| 1046 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1048 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1049 | } |
| 1050 | else { |
| 1051 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1052 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 1054 | ((Py_UCS2*)data)[size] = 0; |
| 1055 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 1056 | ((Py_UCS4*)data)[size] = 0; |
| 1057 | if (is_sharing) { |
| 1058 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1059 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1060 | } |
| 1061 | else { |
| 1062 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1063 | _PyUnicode_WSTR(unicode) = NULL; |
| 1064 | } |
| 1065 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1066 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1067 | return obj; |
| 1068 | } |
| 1069 | |
| 1070 | #if SIZEOF_WCHAR_T == 2 |
| 1071 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1072 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1073 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | |
| 1075 | This function assumes that unicode can hold one more code point than wstr |
| 1076 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1077 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1078 | 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] | 1079 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1080 | { |
| 1081 | const wchar_t *iter; |
| 1082 | Py_UCS4 *ucs4_out; |
| 1083 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1084 | assert(unicode != NULL); |
| 1085 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1086 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1087 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1088 | |
| 1089 | for (iter = begin; iter < end; ) { |
| 1090 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1091 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1092 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1093 | && (iter+1) < end |
| 1094 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1095 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1096 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | iter += 2; |
| 1098 | } |
| 1099 | else { |
| 1100 | *ucs4_out++ = *iter; |
| 1101 | iter++; |
| 1102 | } |
| 1103 | } |
| 1104 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1105 | _PyUnicode_GET_LENGTH(unicode))); |
| 1106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | } |
| 1108 | #endif |
| 1109 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1110 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1111 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1112 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1113 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1114 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1115 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1116 | return -1; |
| 1117 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1121 | static int |
| 1122 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1123 | PyObject *from, Py_ssize_t from_start, |
| 1124 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1125 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1126 | unsigned int from_kind, to_kind; |
| 1127 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1128 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1129 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1130 | assert(PyUnicode_Check(from)); |
| 1131 | assert(PyUnicode_Check(to)); |
| 1132 | assert(PyUnicode_IS_READY(from)); |
| 1133 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1135 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1136 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1137 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1138 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1139 | if (how_many == 0) |
| 1140 | return 0; |
| 1141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1143 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1145 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1147 | #ifdef Py_DEBUG |
| 1148 | if (!check_maxchar |
| 1149 | && (from_kind > to_kind |
| 1150 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1151 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1152 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1153 | Py_UCS4 ch; |
| 1154 | Py_ssize_t i; |
| 1155 | for (i=0; i < how_many; i++) { |
| 1156 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1157 | assert(ch <= to_maxchar); |
| 1158 | } |
| 1159 | } |
| 1160 | #endif |
| 1161 | fast = (from_kind == to_kind); |
| 1162 | if (check_maxchar |
| 1163 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1164 | { |
| 1165 | /* deny latin1 => ascii */ |
| 1166 | fast = 0; |
| 1167 | } |
| 1168 | |
| 1169 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1170 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1171 | (char*)from_data + from_kind * from_start, |
| 1172 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1174 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1175 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1176 | { |
| 1177 | _PyUnicode_CONVERT_BYTES( |
| 1178 | Py_UCS1, Py_UCS2, |
| 1179 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1180 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1181 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1182 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1183 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1184 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1185 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1186 | { |
| 1187 | _PyUnicode_CONVERT_BYTES( |
| 1188 | Py_UCS1, Py_UCS4, |
| 1189 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1190 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1191 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1192 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1193 | } |
| 1194 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1195 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1196 | { |
| 1197 | _PyUnicode_CONVERT_BYTES( |
| 1198 | Py_UCS2, Py_UCS4, |
| 1199 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1200 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1201 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1202 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1203 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1204 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1205 | /* check if max_char(from substring) <= max_char(to) */ |
| 1206 | if (from_kind > to_kind |
| 1207 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1208 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1209 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1210 | /* slow path to check for character overflow */ |
| 1211 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1212 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1213 | Py_ssize_t i; |
| 1214 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1215 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1216 | for (i=0; i < how_many; i++) { |
| 1217 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1218 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1219 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1220 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1221 | #else |
| 1222 | if (!check_maxchar) { |
| 1223 | for (i=0; i < how_many; i++) { |
| 1224 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1225 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1226 | } |
| 1227 | } |
| 1228 | else { |
| 1229 | for (i=0; i < how_many; i++) { |
| 1230 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1231 | if (ch > to_maxchar) |
| 1232 | return 1; |
| 1233 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1237 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1238 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1239 | assert(0 && "inconsistent state"); |
| 1240 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1241 | } |
| 1242 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1248 | PyObject *from, Py_ssize_t from_start, |
| 1249 | Py_ssize_t how_many) |
| 1250 | { |
| 1251 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1252 | } |
| 1253 | |
| 1254 | Py_ssize_t |
| 1255 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1256 | PyObject *from, Py_ssize_t from_start, |
| 1257 | Py_ssize_t how_many) |
| 1258 | { |
| 1259 | int err; |
| 1260 | |
| 1261 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1262 | PyErr_BadInternalCall(); |
| 1263 | return -1; |
| 1264 | } |
| 1265 | |
| 1266 | if (PyUnicode_READY(from)) |
| 1267 | return -1; |
| 1268 | if (PyUnicode_READY(to)) |
| 1269 | return -1; |
| 1270 | |
| 1271 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1272 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1273 | PyErr_Format(PyExc_SystemError, |
| 1274 | "Cannot write %zi characters at %zi " |
| 1275 | "in a string of %zi characters", |
| 1276 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (how_many == 0) |
| 1281 | return 0; |
| 1282 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1283 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1284 | return -1; |
| 1285 | |
| 1286 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1287 | if (err) { |
| 1288 | PyErr_Format(PyExc_SystemError, |
| 1289 | "Cannot copy %s characters " |
| 1290 | "into a string of %s characters", |
| 1291 | unicode_kind_name(from), |
| 1292 | unicode_kind_name(to)); |
| 1293 | return -1; |
| 1294 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1295 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1298 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1299 | correct string length can be computed before converting a string to UCS4. |
| 1300 | This function counts single surrogates as a character and not as a pair. |
| 1301 | |
| 1302 | Return 0 on success, or -1 on error. */ |
| 1303 | static int |
| 1304 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1305 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1306 | { |
| 1307 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1308 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1309 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1310 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1311 | *num_surrogates = 0; |
| 1312 | *maxchar = 0; |
| 1313 | |
| 1314 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1316 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1317 | && (iter+1) < end |
| 1318 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1320 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1321 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | iter += 2; |
| 1323 | } |
| 1324 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1325 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1326 | { |
| 1327 | ch = *iter; |
| 1328 | iter++; |
| 1329 | } |
| 1330 | if (ch > *maxchar) { |
| 1331 | *maxchar = ch; |
| 1332 | if (*maxchar > MAX_UNICODE) { |
| 1333 | PyErr_Format(PyExc_ValueError, |
| 1334 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1335 | ch); |
| 1336 | return -1; |
| 1337 | } |
| 1338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | } |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1344 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1345 | #endif |
| 1346 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1347 | int |
| 1348 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | { |
| 1350 | wchar_t *end; |
| 1351 | Py_UCS4 maxchar = 0; |
| 1352 | Py_ssize_t num_surrogates; |
| 1353 | #if SIZEOF_WCHAR_T == 2 |
| 1354 | Py_ssize_t length_wo_surrogates; |
| 1355 | #endif |
| 1356 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1357 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1358 | strings were created using _PyObject_New() and where no canonical |
| 1359 | representation (the str field) has been set yet aka strings |
| 1360 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1361 | assert(_PyUnicode_CHECK(unicode)); |
| 1362 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1364 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1365 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1366 | /* Actually, it should neither be interned nor be anything else: */ |
| 1367 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | |
| 1369 | #ifdef Py_DEBUG |
| 1370 | ++unicode_ready_calls; |
| 1371 | #endif |
| 1372 | |
| 1373 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1374 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1375 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1379 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1380 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | PyErr_NoMemory(); |
| 1382 | return -1; |
| 1383 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1384 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | _PyUnicode_WSTR(unicode), end, |
| 1386 | PyUnicode_1BYTE_DATA(unicode)); |
| 1387 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1388 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1389 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1390 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1391 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1392 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1393 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | } |
| 1395 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1396 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1397 | _PyUnicode_UTF8(unicode) = NULL; |
| 1398 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | } |
| 1400 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1401 | _PyUnicode_WSTR(unicode) = NULL; |
| 1402 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1403 | } |
| 1404 | /* In this case we might have to convert down from 4-byte native |
| 1405 | wchar_t to 2-byte unicode. */ |
| 1406 | else if (maxchar < 65536) { |
| 1407 | assert(num_surrogates == 0 && |
| 1408 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1409 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1410 | #if SIZEOF_WCHAR_T == 2 |
| 1411 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1412 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1413 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1414 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1415 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1416 | _PyUnicode_UTF8(unicode) = NULL; |
| 1417 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1418 | #else |
| 1419 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1420 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1421 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1422 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1423 | PyErr_NoMemory(); |
| 1424 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1425 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1426 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1427 | _PyUnicode_WSTR(unicode), end, |
| 1428 | PyUnicode_2BYTE_DATA(unicode)); |
| 1429 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1430 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1431 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1432 | _PyUnicode_UTF8(unicode) = NULL; |
| 1433 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1434 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1435 | _PyUnicode_WSTR(unicode) = NULL; |
| 1436 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1437 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1438 | } |
| 1439 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1440 | else { |
| 1441 | #if SIZEOF_WCHAR_T == 2 |
| 1442 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1443 | new normalized 4-byte version. */ |
| 1444 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1445 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1446 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1447 | PyErr_NoMemory(); |
| 1448 | return -1; |
| 1449 | } |
| 1450 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1451 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | _PyUnicode_UTF8(unicode) = NULL; |
| 1453 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1454 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1455 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1456 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1457 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1458 | _PyUnicode_WSTR(unicode) = NULL; |
| 1459 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1460 | #else |
| 1461 | assert(num_surrogates == 0); |
| 1462 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1463 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1465 | _PyUnicode_UTF8(unicode) = NULL; |
| 1466 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1468 | #endif |
| 1469 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1470 | } |
| 1471 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1472 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1476 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1477 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1478 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1479 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1480 | case SSTATE_NOT_INTERNED: |
| 1481 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1483 | case SSTATE_INTERNED_MORTAL: |
| 1484 | /* revive dead object temporarily for DelItem */ |
| 1485 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1486 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1487 | Py_FatalError( |
| 1488 | "deletion of interned string failed"); |
| 1489 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1491 | case SSTATE_INTERNED_IMMORTAL: |
| 1492 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1494 | default: |
| 1495 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1498 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1499 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1500 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1501 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1502 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1503 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1504 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1505 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1508 | #ifdef Py_DEBUG |
| 1509 | static int |
| 1510 | unicode_is_singleton(PyObject *unicode) |
| 1511 | { |
| 1512 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1513 | if (unicode == unicode_empty) |
| 1514 | return 1; |
| 1515 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1516 | { |
| 1517 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1518 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1519 | return 1; |
| 1520 | } |
| 1521 | return 0; |
| 1522 | } |
| 1523 | #endif |
| 1524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1525 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1526 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1527 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1528 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1529 | if (Py_REFCNT(unicode) != 1) |
| 1530 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1531 | if (_PyUnicode_HASH(unicode) != -1) |
| 1532 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1533 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1534 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1535 | if (!PyUnicode_CheckExact(unicode)) |
| 1536 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1537 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1538 | /* singleton refcount is greater than 1 */ |
| 1539 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1540 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1541 | return 1; |
| 1542 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1543 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1544 | static int |
| 1545 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1546 | { |
| 1547 | PyObject *unicode; |
| 1548 | Py_ssize_t old_length; |
| 1549 | |
| 1550 | assert(p_unicode != NULL); |
| 1551 | unicode = *p_unicode; |
| 1552 | |
| 1553 | assert(unicode != NULL); |
| 1554 | assert(PyUnicode_Check(unicode)); |
| 1555 | assert(0 <= length); |
| 1556 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1557 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1558 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1559 | else |
| 1560 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1561 | if (old_length == length) |
| 1562 | return 0; |
| 1563 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1564 | if (length == 0) { |
| 1565 | Py_DECREF(*p_unicode); |
| 1566 | *p_unicode = unicode_empty; |
| 1567 | Py_INCREF(*p_unicode); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1571 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1572 | PyObject *copy = resize_copy(unicode, length); |
| 1573 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1575 | Py_DECREF(*p_unicode); |
| 1576 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1580 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1581 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1582 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1583 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1584 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1585 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1586 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1587 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1588 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1591 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1593 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1594 | PyObject *unicode; |
| 1595 | if (p_unicode == NULL) { |
| 1596 | PyErr_BadInternalCall(); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1600 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1601 | { |
| 1602 | PyErr_BadInternalCall(); |
| 1603 | return -1; |
| 1604 | } |
| 1605 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1606 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1607 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1608 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1609 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1610 | { |
| 1611 | PyObject *result; |
| 1612 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1613 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1614 | return 0; |
| 1615 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1616 | maxchar); |
| 1617 | if (result == NULL) |
| 1618 | return -1; |
| 1619 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1620 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1621 | Py_DECREF(*p_unicode); |
| 1622 | *p_unicode = result; |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | static int |
| 1627 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1628 | Py_UCS4 ch) |
| 1629 | { |
| 1630 | if (unicode_widen(p_unicode, ch) < 0) |
| 1631 | return -1; |
| 1632 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1633 | PyUnicode_DATA(*p_unicode), |
| 1634 | (*pos)++, ch); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1638 | static PyObject* |
| 1639 | get_latin1_char(unsigned char ch) |
| 1640 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1641 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1642 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1643 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1644 | if (!unicode) |
| 1645 | return NULL; |
| 1646 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1647 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1648 | unicode_latin1[ch] = unicode; |
| 1649 | } |
| 1650 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1651 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1654 | PyObject * |
| 1655 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1656 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1657 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | Py_UCS4 maxchar = 0; |
| 1659 | Py_ssize_t num_surrogates; |
| 1660 | |
| 1661 | if (u == NULL) |
| 1662 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1663 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1664 | /* If the Unicode data is known at construction time, we can apply |
| 1665 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | /* Optimization for empty strings */ |
| 1668 | if (size == 0 && unicode_empty != NULL) { |
| 1669 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1670 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1671 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1672 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1673 | /* Single character Unicode objects in the Latin-1 range are |
| 1674 | shared when using this constructor */ |
| 1675 | if (size == 1 && *u < 256) |
| 1676 | return get_latin1_char((unsigned char)*u); |
| 1677 | |
| 1678 | /* If not empty and not single character, copy the Unicode data |
| 1679 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1680 | if (find_maxchar_surrogates(u, u + size, |
| 1681 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1682 | return NULL; |
| 1683 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1684 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | if (!unicode) |
| 1686 | return NULL; |
| 1687 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1688 | switch (PyUnicode_KIND(unicode)) { |
| 1689 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1690 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1691 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1692 | break; |
| 1693 | case PyUnicode_2BYTE_KIND: |
| 1694 | #if Py_UNICODE_SIZE == 2 |
| 1695 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1696 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1697 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1698 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1699 | #endif |
| 1700 | break; |
| 1701 | case PyUnicode_4BYTE_KIND: |
| 1702 | #if SIZEOF_WCHAR_T == 2 |
| 1703 | /* This is the only case which has to process surrogates, thus |
| 1704 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1705 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | #else |
| 1707 | assert(num_surrogates == 0); |
| 1708 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1709 | #endif |
| 1710 | break; |
| 1711 | default: |
| 1712 | assert(0 && "Impossible state"); |
| 1713 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1715 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1718 | PyObject * |
| 1719 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1720 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1721 | if (size < 0) { |
| 1722 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1723 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1724 | return NULL; |
| 1725 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1726 | if (u != NULL) |
| 1727 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1728 | else |
| 1729 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1732 | PyObject * |
| 1733 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1734 | { |
| 1735 | size_t size = strlen(u); |
| 1736 | if (size > PY_SSIZE_T_MAX) { |
| 1737 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1738 | return NULL; |
| 1739 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1740 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1743 | PyObject * |
| 1744 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1745 | { |
| 1746 | if (!id->object) { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1747 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1748 | strlen(id->string), |
| 1749 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1750 | if (!id->object) |
| 1751 | return NULL; |
| 1752 | PyUnicode_InternInPlace(&id->object); |
| 1753 | assert(!id->next); |
| 1754 | id->next = static_strings; |
| 1755 | static_strings = id; |
| 1756 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1757 | return id->object; |
| 1758 | } |
| 1759 | |
| 1760 | void |
| 1761 | _PyUnicode_ClearStaticStrings() |
| 1762 | { |
| 1763 | _Py_Identifier *i; |
| 1764 | for (i = static_strings; i; i = i->next) { |
| 1765 | Py_DECREF(i->object); |
| 1766 | i->object = NULL; |
| 1767 | i->next = NULL; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1771 | /* Internal function, don't check maximum character */ |
| 1772 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1773 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1774 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1775 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1776 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1777 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1778 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1779 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1780 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1781 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1782 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1783 | unicode = PyUnicode_New(size, 127); |
| 1784 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1785 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1786 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1787 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1788 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1791 | static Py_UCS4 |
| 1792 | kind_maxchar_limit(unsigned int kind) |
| 1793 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1794 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1795 | case PyUnicode_1BYTE_KIND: |
| 1796 | return 0x80; |
| 1797 | case PyUnicode_2BYTE_KIND: |
| 1798 | return 0x100; |
| 1799 | case PyUnicode_4BYTE_KIND: |
| 1800 | return 0x10000; |
| 1801 | default: |
| 1802 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1803 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1807 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1808 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1809 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1810 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1811 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1812 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1813 | if (size == 0) { |
| 1814 | Py_INCREF(unicode_empty); |
| 1815 | return unicode_empty; |
| 1816 | } |
| 1817 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1818 | if (size == 1) |
| 1819 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1820 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1821 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1822 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1823 | if (!res) |
| 1824 | return NULL; |
| 1825 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1826 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1827 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1830 | static PyObject* |
| 1831 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1832 | { |
| 1833 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1834 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1835 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1836 | if (size == 0) { |
| 1837 | Py_INCREF(unicode_empty); |
| 1838 | return unicode_empty; |
| 1839 | } |
| 1840 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1841 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1842 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1843 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1844 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1845 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1846 | if (!res) |
| 1847 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1848 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1849 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1850 | else { |
| 1851 | _PyUnicode_CONVERT_BYTES( |
| 1852 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1853 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1854 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1855 | return res; |
| 1856 | } |
| 1857 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1858 | static PyObject* |
| 1859 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1860 | { |
| 1861 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1862 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1863 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1864 | if (size == 0) { |
| 1865 | Py_INCREF(unicode_empty); |
| 1866 | return unicode_empty; |
| 1867 | } |
| 1868 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1869 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1870 | return get_latin1_char((unsigned char)u[0]); |
| 1871 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1872 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1873 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1874 | if (!res) |
| 1875 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1876 | if (max_char < 256) |
| 1877 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1878 | PyUnicode_1BYTE_DATA(res)); |
| 1879 | else if (max_char < 0x10000) |
| 1880 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1881 | PyUnicode_2BYTE_DATA(res)); |
| 1882 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1883 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1884 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1885 | return res; |
| 1886 | } |
| 1887 | |
| 1888 | PyObject* |
| 1889 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1890 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1891 | if (size < 0) { |
| 1892 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1893 | return NULL; |
| 1894 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1895 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1896 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1897 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1898 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1899 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1900 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1901 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1902 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1903 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1904 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1908 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1909 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1910 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1911 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1912 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1913 | { |
| 1914 | PyObject *unicode, *copy; |
| 1915 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1916 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1917 | unsigned int kind; |
| 1918 | |
| 1919 | assert(p_unicode != NULL); |
| 1920 | unicode = *p_unicode; |
| 1921 | assert(PyUnicode_IS_READY(unicode)); |
| 1922 | if (PyUnicode_IS_ASCII(unicode)) |
| 1923 | return; |
| 1924 | |
| 1925 | len = PyUnicode_GET_LENGTH(unicode); |
| 1926 | kind = PyUnicode_KIND(unicode); |
| 1927 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1928 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1929 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1930 | if (max_char >= 128) |
| 1931 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1932 | } |
| 1933 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1934 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1935 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1936 | if (max_char >= 256) |
| 1937 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1938 | } |
| 1939 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1940 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1941 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1942 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1943 | if (max_char >= 0x10000) |
| 1944 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1945 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1946 | copy = PyUnicode_New(len, max_char); |
| 1947 | copy_characters(copy, 0, unicode, 0, len); |
| 1948 | Py_DECREF(unicode); |
| 1949 | *p_unicode = copy; |
| 1950 | } |
| 1951 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1952 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 1953 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1954 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1955 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1956 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1957 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1958 | if (!PyUnicode_Check(unicode)) { |
| 1959 | PyErr_BadInternalCall(); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | if (PyUnicode_READY(unicode)) |
| 1963 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1964 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1965 | length = PyUnicode_GET_LENGTH(unicode); |
| 1966 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1967 | if (!copy) |
| 1968 | return NULL; |
| 1969 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1970 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1971 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1972 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1973 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1974 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1978 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1979 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1980 | |
| 1981 | void* |
| 1982 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1983 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1984 | Py_ssize_t len; |
| 1985 | void *result; |
| 1986 | unsigned int skind; |
| 1987 | |
| 1988 | if (PyUnicode_READY(s)) |
| 1989 | return NULL; |
| 1990 | |
| 1991 | len = PyUnicode_GET_LENGTH(s); |
| 1992 | skind = PyUnicode_KIND(s); |
| 1993 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1994 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | return NULL; |
| 1996 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1997 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1998 | case PyUnicode_2BYTE_KIND: |
| 1999 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2000 | if (!result) |
| 2001 | return PyErr_NoMemory(); |
| 2002 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2003 | _PyUnicode_CONVERT_BYTES( |
| 2004 | Py_UCS1, Py_UCS2, |
| 2005 | PyUnicode_1BYTE_DATA(s), |
| 2006 | PyUnicode_1BYTE_DATA(s) + len, |
| 2007 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2009 | case PyUnicode_4BYTE_KIND: |
| 2010 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2011 | if (!result) |
| 2012 | return PyErr_NoMemory(); |
| 2013 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2014 | _PyUnicode_CONVERT_BYTES( |
| 2015 | Py_UCS2, Py_UCS4, |
| 2016 | PyUnicode_2BYTE_DATA(s), |
| 2017 | PyUnicode_2BYTE_DATA(s) + len, |
| 2018 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2019 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2020 | else { |
| 2021 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2022 | _PyUnicode_CONVERT_BYTES( |
| 2023 | Py_UCS1, Py_UCS4, |
| 2024 | PyUnicode_1BYTE_DATA(s), |
| 2025 | PyUnicode_1BYTE_DATA(s) + len, |
| 2026 | result); |
| 2027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2028 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2029 | default: |
| 2030 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2031 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2032 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2033 | return NULL; |
| 2034 | } |
| 2035 | |
| 2036 | static Py_UCS4* |
| 2037 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2038 | int copy_null) |
| 2039 | { |
| 2040 | int kind; |
| 2041 | void *data; |
| 2042 | Py_ssize_t len, targetlen; |
| 2043 | if (PyUnicode_READY(string) == -1) |
| 2044 | return NULL; |
| 2045 | kind = PyUnicode_KIND(string); |
| 2046 | data = PyUnicode_DATA(string); |
| 2047 | len = PyUnicode_GET_LENGTH(string); |
| 2048 | targetlen = len; |
| 2049 | if (copy_null) |
| 2050 | targetlen++; |
| 2051 | if (!target) { |
| 2052 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2053 | PyErr_NoMemory(); |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2057 | if (!target) { |
| 2058 | PyErr_NoMemory(); |
| 2059 | return NULL; |
| 2060 | } |
| 2061 | } |
| 2062 | else { |
| 2063 | if (targetsize < targetlen) { |
| 2064 | PyErr_Format(PyExc_SystemError, |
| 2065 | "string is longer than the buffer"); |
| 2066 | if (copy_null && 0 < targetsize) |
| 2067 | target[0] = 0; |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2071 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2072 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2073 | _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] | 2074 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2075 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2076 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2077 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2078 | } |
| 2079 | else { |
| 2080 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2081 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2082 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2083 | if (copy_null) |
| 2084 | target[len] = 0; |
| 2085 | return target; |
| 2086 | } |
| 2087 | |
| 2088 | Py_UCS4* |
| 2089 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2090 | int copy_null) |
| 2091 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2092 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2093 | PyErr_BadInternalCall(); |
| 2094 | return NULL; |
| 2095 | } |
| 2096 | return as_ucs4(string, target, targetsize, copy_null); |
| 2097 | } |
| 2098 | |
| 2099 | Py_UCS4* |
| 2100 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2101 | { |
| 2102 | return as_ucs4(string, NULL, 0, 1); |
| 2103 | } |
| 2104 | |
| 2105 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2106 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2107 | PyObject * |
| 2108 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2109 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2111 | if (size == 0) { |
| 2112 | Py_INCREF(unicode_empty); |
| 2113 | return unicode_empty; |
| 2114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2115 | PyErr_BadInternalCall(); |
| 2116 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2119 | if (size == -1) { |
| 2120 | size = wcslen(w); |
| 2121 | } |
| 2122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2123 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2126 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2127 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2128 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2129 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2130 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2131 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2132 | *fmt++ = '%'; |
| 2133 | if (width) { |
| 2134 | if (zeropad) |
| 2135 | *fmt++ = '0'; |
| 2136 | fmt += sprintf(fmt, "%d", width); |
| 2137 | } |
| 2138 | if (precision) |
| 2139 | fmt += sprintf(fmt, ".%d", precision); |
| 2140 | if (longflag) |
| 2141 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2142 | else if (longlongflag) { |
| 2143 | /* longlongflag should only ever be nonzero on machines with |
| 2144 | HAVE_LONG_LONG defined */ |
| 2145 | #ifdef HAVE_LONG_LONG |
| 2146 | char *f = PY_FORMAT_LONG_LONG; |
| 2147 | while (*f) |
| 2148 | *fmt++ = *f++; |
| 2149 | #else |
| 2150 | /* we shouldn't ever get here */ |
| 2151 | assert(0); |
| 2152 | *fmt++ = 'l'; |
| 2153 | #endif |
| 2154 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2155 | else if (size_tflag) { |
| 2156 | char *f = PY_FORMAT_SIZE_T; |
| 2157 | while (*f) |
| 2158 | *fmt++ = *f++; |
| 2159 | } |
| 2160 | *fmt++ = c; |
| 2161 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2164 | /* helper for PyUnicode_FromFormatV() */ |
| 2165 | |
| 2166 | static const char* |
| 2167 | parse_format_flags(const char *f, |
| 2168 | int *p_width, int *p_precision, |
| 2169 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2170 | { |
| 2171 | int width, precision, longflag, longlongflag, size_tflag; |
| 2172 | |
| 2173 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2174 | f++; |
| 2175 | width = 0; |
| 2176 | while (Py_ISDIGIT((unsigned)*f)) |
| 2177 | width = (width*10) + *f++ - '0'; |
| 2178 | precision = 0; |
| 2179 | if (*f == '.') { |
| 2180 | f++; |
| 2181 | while (Py_ISDIGIT((unsigned)*f)) |
| 2182 | precision = (precision*10) + *f++ - '0'; |
| 2183 | if (*f == '%') { |
| 2184 | /* "%.3%s" => f points to "3" */ |
| 2185 | f--; |
| 2186 | } |
| 2187 | } |
| 2188 | if (*f == '\0') { |
| 2189 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2190 | f--; |
| 2191 | } |
| 2192 | if (p_width != NULL) |
| 2193 | *p_width = width; |
| 2194 | if (p_precision != NULL) |
| 2195 | *p_precision = precision; |
| 2196 | |
| 2197 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2198 | longflag = 0; |
| 2199 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2200 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2203 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2204 | longflag = 1; |
| 2205 | ++f; |
| 2206 | } |
| 2207 | #ifdef HAVE_LONG_LONG |
| 2208 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2209 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2210 | longlongflag = 1; |
| 2211 | f += 2; |
| 2212 | } |
| 2213 | #endif |
| 2214 | } |
| 2215 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2216 | 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] | 2217 | size_tflag = 1; |
| 2218 | ++f; |
| 2219 | } |
| 2220 | if (p_longflag != NULL) |
| 2221 | *p_longflag = longflag; |
| 2222 | if (p_longlongflag != NULL) |
| 2223 | *p_longlongflag = longlongflag; |
| 2224 | if (p_size_tflag != NULL) |
| 2225 | *p_size_tflag = size_tflag; |
| 2226 | return f; |
| 2227 | } |
| 2228 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2229 | /* maximum number of characters required for output of %ld. 21 characters |
| 2230 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2231 | #define MAX_LONG_CHARS 21 |
| 2232 | /* maximum number of characters required for output of %lld. |
| 2233 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2234 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2235 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2236 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2237 | PyObject * |
| 2238 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2239 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2240 | va_list count; |
| 2241 | Py_ssize_t callcount = 0; |
| 2242 | PyObject **callresults = NULL; |
| 2243 | PyObject **callresult = NULL; |
| 2244 | Py_ssize_t n = 0; |
| 2245 | int width = 0; |
| 2246 | int precision = 0; |
| 2247 | int zeropad; |
| 2248 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2249 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2250 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2251 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2252 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2253 | Py_UCS4 argmaxchar; |
| 2254 | Py_ssize_t numbersize = 0; |
| 2255 | char *numberresults = NULL; |
| 2256 | char *numberresult = NULL; |
| 2257 | Py_ssize_t i; |
| 2258 | int kind; |
| 2259 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2260 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2261 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2262 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2263 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2264 | * 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] | 2265 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2266 | * also estimate a upper bound for all the number formats in the string, |
| 2267 | * 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] | 2268 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2269 | for (f = format; *f; f++) { |
| 2270 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2271 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2273 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2274 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2275 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2277 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2278 | #ifdef HAVE_LONG_LONG |
| 2279 | if (longlongflag) { |
| 2280 | if (width < MAX_LONG_LONG_CHARS) |
| 2281 | width = MAX_LONG_LONG_CHARS; |
| 2282 | } |
| 2283 | else |
| 2284 | #endif |
| 2285 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2286 | including sign. Decimal takes the most space. This |
| 2287 | isn't enough for octal. If a width is specified we |
| 2288 | need more (which we allocate later). */ |
| 2289 | if (width < MAX_LONG_CHARS) |
| 2290 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2291 | |
| 2292 | /* account for the size + '\0' to separate numbers |
| 2293 | inside of the numberresults buffer */ |
| 2294 | numbersize += (width + 1); |
| 2295 | } |
| 2296 | } |
| 2297 | else if ((unsigned char)*f > 127) { |
| 2298 | PyErr_Format(PyExc_ValueError, |
| 2299 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2300 | "string, got a non-ASCII byte: 0x%02x", |
| 2301 | (unsigned char)*f); |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | } |
| 2305 | /* step 2: allocate memory for the results of |
| 2306 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2307 | if (callcount) { |
| 2308 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2309 | if (!callresults) { |
| 2310 | PyErr_NoMemory(); |
| 2311 | return NULL; |
| 2312 | } |
| 2313 | callresult = callresults; |
| 2314 | } |
| 2315 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2316 | if (numbersize) { |
| 2317 | numberresults = PyObject_Malloc(numbersize); |
| 2318 | if (!numberresults) { |
| 2319 | PyErr_NoMemory(); |
| 2320 | goto fail; |
| 2321 | } |
| 2322 | numberresult = numberresults; |
| 2323 | } |
| 2324 | |
| 2325 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2326 | for (f = format; *f; f++) { |
| 2327 | if (*f == '%') { |
| 2328 | const char* p; |
| 2329 | int longflag; |
| 2330 | int longlongflag; |
| 2331 | int size_tflag; |
| 2332 | int numprinted; |
| 2333 | |
| 2334 | p = f; |
| 2335 | zeropad = (f[1] == '0'); |
| 2336 | f = parse_format_flags(f, &width, &precision, |
| 2337 | &longflag, &longlongflag, &size_tflag); |
| 2338 | switch (*f) { |
| 2339 | case 'c': |
| 2340 | { |
| 2341 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2342 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2343 | n++; |
| 2344 | break; |
| 2345 | } |
| 2346 | case '%': |
| 2347 | n++; |
| 2348 | break; |
| 2349 | case 'i': |
| 2350 | case 'd': |
| 2351 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2352 | width, precision, *f); |
| 2353 | if (longflag) |
| 2354 | numprinted = sprintf(numberresult, fmt, |
| 2355 | va_arg(count, long)); |
| 2356 | #ifdef HAVE_LONG_LONG |
| 2357 | else if (longlongflag) |
| 2358 | numprinted = sprintf(numberresult, fmt, |
| 2359 | va_arg(count, PY_LONG_LONG)); |
| 2360 | #endif |
| 2361 | else if (size_tflag) |
| 2362 | numprinted = sprintf(numberresult, fmt, |
| 2363 | va_arg(count, Py_ssize_t)); |
| 2364 | else |
| 2365 | numprinted = sprintf(numberresult, fmt, |
| 2366 | va_arg(count, int)); |
| 2367 | n += numprinted; |
| 2368 | /* advance by +1 to skip over the '\0' */ |
| 2369 | numberresult += (numprinted + 1); |
| 2370 | assert(*(numberresult - 1) == '\0'); |
| 2371 | assert(*(numberresult - 2) != '\0'); |
| 2372 | assert(numprinted >= 0); |
| 2373 | assert(numberresult <= numberresults + numbersize); |
| 2374 | break; |
| 2375 | case 'u': |
| 2376 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2377 | width, precision, 'u'); |
| 2378 | if (longflag) |
| 2379 | numprinted = sprintf(numberresult, fmt, |
| 2380 | va_arg(count, unsigned long)); |
| 2381 | #ifdef HAVE_LONG_LONG |
| 2382 | else if (longlongflag) |
| 2383 | numprinted = sprintf(numberresult, fmt, |
| 2384 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2385 | #endif |
| 2386 | else if (size_tflag) |
| 2387 | numprinted = sprintf(numberresult, fmt, |
| 2388 | va_arg(count, size_t)); |
| 2389 | else |
| 2390 | numprinted = sprintf(numberresult, fmt, |
| 2391 | va_arg(count, unsigned int)); |
| 2392 | n += numprinted; |
| 2393 | numberresult += (numprinted + 1); |
| 2394 | assert(*(numberresult - 1) == '\0'); |
| 2395 | assert(*(numberresult - 2) != '\0'); |
| 2396 | assert(numprinted >= 0); |
| 2397 | assert(numberresult <= numberresults + numbersize); |
| 2398 | break; |
| 2399 | case 'x': |
| 2400 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2401 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2402 | n += numprinted; |
| 2403 | numberresult += (numprinted + 1); |
| 2404 | assert(*(numberresult - 1) == '\0'); |
| 2405 | assert(*(numberresult - 2) != '\0'); |
| 2406 | assert(numprinted >= 0); |
| 2407 | assert(numberresult <= numberresults + numbersize); |
| 2408 | break; |
| 2409 | case 'p': |
| 2410 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2411 | /* %p is ill-defined: ensure leading 0x. */ |
| 2412 | if (numberresult[1] == 'X') |
| 2413 | numberresult[1] = 'x'; |
| 2414 | else if (numberresult[1] != 'x') { |
| 2415 | memmove(numberresult + 2, numberresult, |
| 2416 | strlen(numberresult) + 1); |
| 2417 | numberresult[0] = '0'; |
| 2418 | numberresult[1] = 'x'; |
| 2419 | numprinted += 2; |
| 2420 | } |
| 2421 | n += numprinted; |
| 2422 | numberresult += (numprinted + 1); |
| 2423 | assert(*(numberresult - 1) == '\0'); |
| 2424 | assert(*(numberresult - 2) != '\0'); |
| 2425 | assert(numprinted >= 0); |
| 2426 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | case 's': |
| 2429 | { |
| 2430 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2431 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2432 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2433 | if (!str) |
| 2434 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2436 | unicode objects, there is no need to call ready on them */ |
| 2437 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2438 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2439 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2440 | /* Remember the str and switch to the next slot */ |
| 2441 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2442 | break; |
| 2443 | } |
| 2444 | case 'U': |
| 2445 | { |
| 2446 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2447 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | if (PyUnicode_READY(obj) == -1) |
| 2449 | goto fail; |
| 2450 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2451 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2453 | break; |
| 2454 | } |
| 2455 | case 'V': |
| 2456 | { |
| 2457 | PyObject *obj = va_arg(count, PyObject *); |
| 2458 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2459 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2460 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2461 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2462 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2463 | if (PyUnicode_READY(obj) == -1) |
| 2464 | goto fail; |
| 2465 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2466 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2467 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2468 | *callresult++ = NULL; |
| 2469 | } |
| 2470 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2471 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2472 | if (!str_obj) |
| 2473 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2474 | if (PyUnicode_READY(str_obj)) { |
| 2475 | Py_DECREF(str_obj); |
| 2476 | goto fail; |
| 2477 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2478 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2479 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2480 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2481 | *callresult++ = str_obj; |
| 2482 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2483 | break; |
| 2484 | } |
| 2485 | case 'S': |
| 2486 | { |
| 2487 | PyObject *obj = va_arg(count, PyObject *); |
| 2488 | PyObject *str; |
| 2489 | assert(obj); |
| 2490 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2491 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2492 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2493 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2494 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2495 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2496 | /* Remember the str and switch to the next slot */ |
| 2497 | *callresult++ = str; |
| 2498 | break; |
| 2499 | } |
| 2500 | case 'R': |
| 2501 | { |
| 2502 | PyObject *obj = va_arg(count, PyObject *); |
| 2503 | PyObject *repr; |
| 2504 | assert(obj); |
| 2505 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2506 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2507 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2508 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2509 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2510 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2511 | /* Remember the repr and switch to the next slot */ |
| 2512 | *callresult++ = repr; |
| 2513 | break; |
| 2514 | } |
| 2515 | case 'A': |
| 2516 | { |
| 2517 | PyObject *obj = va_arg(count, PyObject *); |
| 2518 | PyObject *ascii; |
| 2519 | assert(obj); |
| 2520 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2521 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2522 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2523 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2524 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2525 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2526 | /* Remember the repr and switch to the next slot */ |
| 2527 | *callresult++ = ascii; |
| 2528 | break; |
| 2529 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2530 | default: |
| 2531 | /* if we stumble upon an unknown |
| 2532 | formatting code, copy the rest of |
| 2533 | the format string to the output |
| 2534 | string. (we cannot just skip the |
| 2535 | code, since there's no way to know |
| 2536 | what's in the argument list) */ |
| 2537 | n += strlen(p); |
| 2538 | goto expand; |
| 2539 | } |
| 2540 | } else |
| 2541 | n++; |
| 2542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2543 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2544 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2545 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2546 | we don't have to resize the string. |
| 2547 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2548 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2549 | if (!string) |
| 2550 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2551 | kind = PyUnicode_KIND(string); |
| 2552 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2553 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2554 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2555 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2556 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2557 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2558 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2559 | |
| 2560 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2561 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2562 | /* checking for == because the last argument could be a empty |
| 2563 | string, which causes i to point to end, the assert at the end of |
| 2564 | the loop */ |
| 2565 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2566 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2567 | switch (*f) { |
| 2568 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2569 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2570 | const int ordinal = va_arg(vargs, int); |
| 2571 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2572 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2573 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2574 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2576 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2577 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2578 | case 'p': |
| 2579 | /* unused, since we already have the result */ |
| 2580 | if (*f == 'p') |
| 2581 | (void) va_arg(vargs, void *); |
| 2582 | else |
| 2583 | (void) va_arg(vargs, int); |
| 2584 | /* extract the result from numberresults and append. */ |
| 2585 | for (; *numberresult; ++i, ++numberresult) |
| 2586 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2587 | /* skip over the separating '\0' */ |
| 2588 | assert(*numberresult == '\0'); |
| 2589 | numberresult++; |
| 2590 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | case 's': |
| 2593 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2594 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2595 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2596 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2597 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2598 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2599 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2600 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2601 | /* We're done with the unicode()/repr() => forget it */ |
| 2602 | Py_DECREF(*callresult); |
| 2603 | /* switch to next unicode()/repr() result */ |
| 2604 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2605 | break; |
| 2606 | } |
| 2607 | case 'U': |
| 2608 | { |
| 2609 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2610 | Py_ssize_t size; |
| 2611 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2612 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2613 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2614 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2615 | break; |
| 2616 | } |
| 2617 | case 'V': |
| 2618 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2620 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2621 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2622 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2623 | size = PyUnicode_GET_LENGTH(obj); |
| 2624 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2625 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2626 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2627 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2628 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2629 | assert(PyUnicode_KIND(*callresult) <= |
| 2630 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2631 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2632 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2633 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2634 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2635 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | break; |
| 2637 | } |
| 2638 | case 'S': |
| 2639 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2640 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2641 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2642 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2643 | /* unused, since we already have the result */ |
| 2644 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2645 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2646 | copy_characters(string, i, *callresult, 0, size); |
| 2647 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2648 | /* We're done with the unicode()/repr() => forget it */ |
| 2649 | Py_DECREF(*callresult); |
| 2650 | /* switch to next unicode()/repr() result */ |
| 2651 | ++callresult; |
| 2652 | break; |
| 2653 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2654 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2655 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2656 | break; |
| 2657 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | for (; *p; ++p, ++i) |
| 2659 | PyUnicode_WRITE(kind, data, i, *p); |
| 2660 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2661 | goto end; |
| 2662 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2663 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2664 | else { |
| 2665 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2666 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2667 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2669 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2670 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2671 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2672 | if (callresults) |
| 2673 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2674 | if (numberresults) |
| 2675 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2676 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2677 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2678 | if (callresults) { |
| 2679 | PyObject **callresult2 = callresults; |
| 2680 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2681 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2682 | ++callresult2; |
| 2683 | } |
| 2684 | PyObject_Free(callresults); |
| 2685 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2686 | if (numberresults) |
| 2687 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2688 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2691 | PyObject * |
| 2692 | PyUnicode_FromFormat(const char *format, ...) |
| 2693 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | PyObject* ret; |
| 2695 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2696 | |
| 2697 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2698 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2699 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2700 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2701 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2702 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2703 | va_end(vargs); |
| 2704 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2707 | #ifdef HAVE_WCHAR_H |
| 2708 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2709 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2710 | convert a Unicode object to a wide character string. |
| 2711 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2712 | - 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] | 2713 | character) required to convert the unicode object. Ignore size argument. |
| 2714 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2715 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2716 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2717 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2718 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2719 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2720 | wchar_t *w, |
| 2721 | Py_ssize_t size) |
| 2722 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2723 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2724 | const wchar_t *wstr; |
| 2725 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2726 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2727 | if (wstr == NULL) |
| 2728 | return -1; |
| 2729 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2730 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2731 | if (size > res) |
| 2732 | size = res + 1; |
| 2733 | else |
| 2734 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2735 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2736 | return res; |
| 2737 | } |
| 2738 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2743 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2744 | wchar_t *w, |
| 2745 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | { |
| 2747 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2748 | PyErr_BadInternalCall(); |
| 2749 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2751 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2754 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2755 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2756 | Py_ssize_t *size) |
| 2757 | { |
| 2758 | wchar_t* buffer; |
| 2759 | Py_ssize_t buflen; |
| 2760 | |
| 2761 | if (unicode == NULL) { |
| 2762 | PyErr_BadInternalCall(); |
| 2763 | return NULL; |
| 2764 | } |
| 2765 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2766 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2767 | if (buflen == -1) |
| 2768 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2769 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2770 | PyErr_NoMemory(); |
| 2771 | return NULL; |
| 2772 | } |
| 2773 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2774 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2775 | if (buffer == NULL) { |
| 2776 | PyErr_NoMemory(); |
| 2777 | return NULL; |
| 2778 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2779 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | if (buflen == -1) |
| 2781 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2782 | if (size != NULL) |
| 2783 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2784 | return buffer; |
| 2785 | } |
| 2786 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2787 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2788 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2789 | PyObject * |
| 2790 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2791 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2792 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2793 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2794 | PyErr_SetString(PyExc_ValueError, |
| 2795 | "chr() arg not in range(0x110000)"); |
| 2796 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2797 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2799 | if (ordinal < 256) |
| 2800 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2801 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2802 | v = PyUnicode_New(1, ordinal); |
| 2803 | if (v == NULL) |
| 2804 | return NULL; |
| 2805 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2806 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2807 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2810 | PyObject * |
| 2811 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2813 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2814 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2815 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2816 | if (PyUnicode_READY(obj)) |
| 2817 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2818 | Py_INCREF(obj); |
| 2819 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2820 | } |
| 2821 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2822 | /* For a Unicode subtype that's not a Unicode object, |
| 2823 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2824 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2825 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2826 | PyErr_Format(PyExc_TypeError, |
| 2827 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2828 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2829 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2832 | PyObject * |
| 2833 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2834 | const char *encoding, |
| 2835 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2836 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2837 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2838 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2840 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2841 | PyErr_BadInternalCall(); |
| 2842 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2845 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2846 | if (PyBytes_Check(obj)) { |
| 2847 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2848 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2849 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2850 | } |
| 2851 | else { |
| 2852 | v = PyUnicode_Decode( |
| 2853 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2854 | encoding, errors); |
| 2855 | } |
| 2856 | return v; |
| 2857 | } |
| 2858 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2859 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2860 | PyErr_SetString(PyExc_TypeError, |
| 2861 | "decoding str is not supported"); |
| 2862 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2863 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2865 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2866 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2867 | PyErr_Format(PyExc_TypeError, |
| 2868 | "coercing to str: need bytes, bytearray " |
| 2869 | "or buffer-like object, %.80s found", |
| 2870 | Py_TYPE(obj)->tp_name); |
| 2871 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2872 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2874 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2875 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2876 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2878 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2879 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2881 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2882 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2885 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2886 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2887 | 1 on success. */ |
| 2888 | static int |
| 2889 | normalize_encoding(const char *encoding, |
| 2890 | char *lower, |
| 2891 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2892 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2893 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2894 | char *l; |
| 2895 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2896 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2897 | if (encoding == NULL) { |
| 2898 | strcpy(lower, "utf-8"); |
| 2899 | return 1; |
| 2900 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2901 | e = encoding; |
| 2902 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2903 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2904 | while (*e) { |
| 2905 | if (l == l_end) |
| 2906 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2907 | if (Py_ISUPPER(*e)) { |
| 2908 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2909 | } |
| 2910 | else if (*e == '_') { |
| 2911 | *l++ = '-'; |
| 2912 | e++; |
| 2913 | } |
| 2914 | else { |
| 2915 | *l++ = *e++; |
| 2916 | } |
| 2917 | } |
| 2918 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2919 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2922 | PyObject * |
| 2923 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2924 | Py_ssize_t size, |
| 2925 | const char *encoding, |
| 2926 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2927 | { |
| 2928 | PyObject *buffer = NULL, *unicode; |
| 2929 | Py_buffer info; |
| 2930 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2931 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2932 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2933 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2934 | if ((strcmp(lower, "utf-8") == 0) || |
| 2935 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2936 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2937 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2938 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2939 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2940 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2941 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2942 | else if (strcmp(lower, "mbcs") == 0) |
| 2943 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2944 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2945 | else if (strcmp(lower, "ascii") == 0) |
| 2946 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2947 | else if (strcmp(lower, "utf-16") == 0) |
| 2948 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2949 | else if (strcmp(lower, "utf-32") == 0) |
| 2950 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2951 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2952 | |
| 2953 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2954 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2955 | 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] | 2956 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2957 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | if (buffer == NULL) |
| 2959 | goto onError; |
| 2960 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2961 | if (unicode == NULL) |
| 2962 | goto onError; |
| 2963 | if (!PyUnicode_Check(unicode)) { |
| 2964 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2965 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2966 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | Py_DECREF(unicode); |
| 2968 | goto onError; |
| 2969 | } |
| 2970 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2971 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2973 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2974 | Py_XDECREF(buffer); |
| 2975 | return NULL; |
| 2976 | } |
| 2977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2978 | PyObject * |
| 2979 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2980 | const char *encoding, |
| 2981 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2982 | { |
| 2983 | PyObject *v; |
| 2984 | |
| 2985 | if (!PyUnicode_Check(unicode)) { |
| 2986 | PyErr_BadArgument(); |
| 2987 | goto onError; |
| 2988 | } |
| 2989 | |
| 2990 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2991 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2992 | |
| 2993 | /* Decode via the codec registry */ |
| 2994 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2995 | if (v == NULL) |
| 2996 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2997 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2998 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2999 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3000 | return NULL; |
| 3001 | } |
| 3002 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3003 | PyObject * |
| 3004 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3005 | const char *encoding, |
| 3006 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3007 | { |
| 3008 | PyObject *v; |
| 3009 | |
| 3010 | if (!PyUnicode_Check(unicode)) { |
| 3011 | PyErr_BadArgument(); |
| 3012 | goto onError; |
| 3013 | } |
| 3014 | |
| 3015 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3016 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3017 | |
| 3018 | /* Decode via the codec registry */ |
| 3019 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3020 | if (v == NULL) |
| 3021 | goto onError; |
| 3022 | if (!PyUnicode_Check(v)) { |
| 3023 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3024 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3025 | Py_TYPE(v)->tp_name); |
| 3026 | Py_DECREF(v); |
| 3027 | goto onError; |
| 3028 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3029 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3031 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3032 | return NULL; |
| 3033 | } |
| 3034 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3035 | PyObject * |
| 3036 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3037 | Py_ssize_t size, |
| 3038 | const char *encoding, |
| 3039 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | { |
| 3041 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | unicode = PyUnicode_FromUnicode(s, size); |
| 3044 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3045 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3047 | Py_DECREF(unicode); |
| 3048 | return v; |
| 3049 | } |
| 3050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3051 | PyObject * |
| 3052 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3053 | const char *encoding, |
| 3054 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3055 | { |
| 3056 | PyObject *v; |
| 3057 | |
| 3058 | if (!PyUnicode_Check(unicode)) { |
| 3059 | PyErr_BadArgument(); |
| 3060 | goto onError; |
| 3061 | } |
| 3062 | |
| 3063 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3064 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3065 | |
| 3066 | /* Encode via the codec registry */ |
| 3067 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3068 | if (v == NULL) |
| 3069 | goto onError; |
| 3070 | return v; |
| 3071 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3072 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3073 | return NULL; |
| 3074 | } |
| 3075 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3076 | static size_t |
| 3077 | wcstombs_errorpos(const wchar_t *wstr) |
| 3078 | { |
| 3079 | size_t len; |
| 3080 | #if SIZEOF_WCHAR_T == 2 |
| 3081 | wchar_t buf[3]; |
| 3082 | #else |
| 3083 | wchar_t buf[2]; |
| 3084 | #endif |
| 3085 | char outbuf[MB_LEN_MAX]; |
| 3086 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3087 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3088 | #if SIZEOF_WCHAR_T == 2 |
| 3089 | buf[2] = 0; |
| 3090 | #else |
| 3091 | buf[1] = 0; |
| 3092 | #endif |
| 3093 | start = wstr; |
| 3094 | while (*wstr != L'\0') |
| 3095 | { |
| 3096 | previous = wstr; |
| 3097 | #if SIZEOF_WCHAR_T == 2 |
| 3098 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3099 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3100 | { |
| 3101 | buf[0] = wstr[0]; |
| 3102 | buf[1] = wstr[1]; |
| 3103 | wstr += 2; |
| 3104 | } |
| 3105 | else { |
| 3106 | buf[0] = *wstr; |
| 3107 | buf[1] = 0; |
| 3108 | wstr++; |
| 3109 | } |
| 3110 | #else |
| 3111 | buf[0] = *wstr; |
| 3112 | wstr++; |
| 3113 | #endif |
| 3114 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3115 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3116 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3120 | return 0; |
| 3121 | } |
| 3122 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3123 | static int |
| 3124 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3125 | { |
| 3126 | if (errors == NULL) { |
| 3127 | *surrogateescape = 0; |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | if (strcmp(errors, "strict") == 0) { |
| 3132 | *surrogateescape = 0; |
| 3133 | return 0; |
| 3134 | } |
| 3135 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3136 | *surrogateescape = 1; |
| 3137 | return 0; |
| 3138 | } |
| 3139 | PyErr_Format(PyExc_ValueError, |
| 3140 | "only 'strict' and 'surrogateescape' error handlers " |
| 3141 | "are supported, not '%s'", |
| 3142 | errors); |
| 3143 | return -1; |
| 3144 | } |
| 3145 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3146 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3147 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3148 | { |
| 3149 | Py_ssize_t wlen, wlen2; |
| 3150 | wchar_t *wstr; |
| 3151 | PyObject *bytes = NULL; |
| 3152 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3153 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3154 | PyObject *exc; |
| 3155 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3156 | int surrogateescape; |
| 3157 | |
| 3158 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3159 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3160 | |
| 3161 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3162 | if (wstr == NULL) |
| 3163 | return NULL; |
| 3164 | |
| 3165 | wlen2 = wcslen(wstr); |
| 3166 | if (wlen2 != wlen) { |
| 3167 | PyMem_Free(wstr); |
| 3168 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3169 | return NULL; |
| 3170 | } |
| 3171 | |
| 3172 | if (surrogateescape) { |
| 3173 | /* locale encoding with surrogateescape */ |
| 3174 | char *str; |
| 3175 | |
| 3176 | str = _Py_wchar2char(wstr, &error_pos); |
| 3177 | if (str == NULL) { |
| 3178 | if (error_pos == (size_t)-1) { |
| 3179 | PyErr_NoMemory(); |
| 3180 | PyMem_Free(wstr); |
| 3181 | return NULL; |
| 3182 | } |
| 3183 | else { |
| 3184 | goto encode_error; |
| 3185 | } |
| 3186 | } |
| 3187 | PyMem_Free(wstr); |
| 3188 | |
| 3189 | bytes = PyBytes_FromString(str); |
| 3190 | PyMem_Free(str); |
| 3191 | } |
| 3192 | else { |
| 3193 | size_t len, len2; |
| 3194 | |
| 3195 | len = wcstombs(NULL, wstr, 0); |
| 3196 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3197 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3198 | goto encode_error; |
| 3199 | } |
| 3200 | |
| 3201 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3202 | if (bytes == NULL) { |
| 3203 | PyMem_Free(wstr); |
| 3204 | return NULL; |
| 3205 | } |
| 3206 | |
| 3207 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3208 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3209 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3210 | goto encode_error; |
| 3211 | } |
| 3212 | PyMem_Free(wstr); |
| 3213 | } |
| 3214 | return bytes; |
| 3215 | |
| 3216 | encode_error: |
| 3217 | errmsg = strerror(errno); |
| 3218 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3219 | |
| 3220 | if (error_pos == (size_t)-1) |
| 3221 | error_pos = wcstombs_errorpos(wstr); |
| 3222 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3223 | PyMem_Free(wstr); |
| 3224 | Py_XDECREF(bytes); |
| 3225 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3226 | if (errmsg != NULL) { |
| 3227 | size_t errlen; |
| 3228 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3229 | if (wstr != NULL) { |
| 3230 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3231 | PyMem_Free(wstr); |
| 3232 | } else |
| 3233 | errmsg = NULL; |
| 3234 | } |
| 3235 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3236 | reason = PyUnicode_FromString( |
| 3237 | "wcstombs() encountered an unencodable " |
| 3238 | "wide character"); |
| 3239 | if (reason == NULL) |
| 3240 | return NULL; |
| 3241 | |
| 3242 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3243 | "locale", unicode, |
| 3244 | (Py_ssize_t)error_pos, |
| 3245 | (Py_ssize_t)(error_pos+1), |
| 3246 | reason); |
| 3247 | Py_DECREF(reason); |
| 3248 | if (exc != NULL) { |
| 3249 | PyCodec_StrictErrors(exc); |
| 3250 | Py_XDECREF(exc); |
| 3251 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3252 | return NULL; |
| 3253 | } |
| 3254 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3255 | PyObject * |
| 3256 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3257 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3258 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3259 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3260 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3261 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3262 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3263 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3264 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3265 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3266 | the Python codec requires to encode at least its own filename. Use the C |
| 3267 | version of the locale codec until the codec registry is initialized and |
| 3268 | the Python codec is loaded. |
| 3269 | |
| 3270 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3271 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3272 | subinterpreters. */ |
| 3273 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3274 | return PyUnicode_AsEncodedString(unicode, |
| 3275 | Py_FileSystemDefaultEncoding, |
| 3276 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3277 | } |
| 3278 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3279 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3280 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3281 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3284 | PyObject * |
| 3285 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3286 | const char *encoding, |
| 3287 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3288 | { |
| 3289 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3290 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | if (!PyUnicode_Check(unicode)) { |
| 3293 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3294 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3295 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3296 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3297 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3298 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3299 | if ((strcmp(lower, "utf-8") == 0) || |
| 3300 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3301 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3302 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3303 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3304 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3305 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3306 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3307 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3308 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3309 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3310 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3311 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3312 | else if (strcmp(lower, "mbcs") == 0) |
| 3313 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3314 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3315 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3316 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | |
| 3319 | /* Encode via the codec registry */ |
| 3320 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3321 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3322 | return NULL; |
| 3323 | |
| 3324 | /* The normal path */ |
| 3325 | if (PyBytes_Check(v)) |
| 3326 | return v; |
| 3327 | |
| 3328 | /* 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] | 3329 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3330 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3331 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3332 | |
| 3333 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3334 | "encoder %s returned bytearray instead of bytes", |
| 3335 | encoding); |
| 3336 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3337 | Py_DECREF(v); |
| 3338 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3339 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3340 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3341 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3342 | Py_DECREF(v); |
| 3343 | return b; |
| 3344 | } |
| 3345 | |
| 3346 | PyErr_Format(PyExc_TypeError, |
| 3347 | "encoder did not return a bytes object (type=%.400s)", |
| 3348 | Py_TYPE(v)->tp_name); |
| 3349 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | } |
| 3352 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3353 | PyObject * |
| 3354 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3355 | const char *encoding, |
| 3356 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3357 | { |
| 3358 | PyObject *v; |
| 3359 | |
| 3360 | if (!PyUnicode_Check(unicode)) { |
| 3361 | PyErr_BadArgument(); |
| 3362 | goto onError; |
| 3363 | } |
| 3364 | |
| 3365 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3366 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* Encode via the codec registry */ |
| 3369 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3370 | if (v == NULL) |
| 3371 | goto onError; |
| 3372 | if (!PyUnicode_Check(v)) { |
| 3373 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3374 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3375 | Py_TYPE(v)->tp_name); |
| 3376 | Py_DECREF(v); |
| 3377 | goto onError; |
| 3378 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3380 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3381 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | return NULL; |
| 3383 | } |
| 3384 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3385 | static size_t |
| 3386 | mbstowcs_errorpos(const char *str, size_t len) |
| 3387 | { |
| 3388 | #ifdef HAVE_MBRTOWC |
| 3389 | const char *start = str; |
| 3390 | mbstate_t mbs; |
| 3391 | size_t converted; |
| 3392 | wchar_t ch; |
| 3393 | |
| 3394 | memset(&mbs, 0, sizeof mbs); |
| 3395 | while (len) |
| 3396 | { |
| 3397 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3398 | if (converted == 0) |
| 3399 | /* Reached end of string */ |
| 3400 | break; |
| 3401 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3402 | /* Conversion error or incomplete character */ |
| 3403 | return str - start; |
| 3404 | } |
| 3405 | else { |
| 3406 | str += converted; |
| 3407 | len -= converted; |
| 3408 | } |
| 3409 | } |
| 3410 | /* failed to find the undecodable byte sequence */ |
| 3411 | return 0; |
| 3412 | #endif |
| 3413 | return 0; |
| 3414 | } |
| 3415 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3416 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3417 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3418 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3419 | { |
| 3420 | wchar_t smallbuf[256]; |
| 3421 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3422 | wchar_t *wstr; |
| 3423 | size_t wlen, wlen2; |
| 3424 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3425 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3426 | size_t error_pos; |
| 3427 | char *errmsg; |
| 3428 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3429 | |
| 3430 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3431 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3432 | |
| 3433 | if (str[len] != '\0' || len != strlen(str)) { |
| 3434 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3435 | return NULL; |
| 3436 | } |
| 3437 | |
| 3438 | if (surrogateescape) |
| 3439 | { |
| 3440 | wstr = _Py_char2wchar(str, &wlen); |
| 3441 | if (wstr == NULL) { |
| 3442 | if (wlen == (size_t)-1) |
| 3443 | PyErr_NoMemory(); |
| 3444 | else |
| 3445 | PyErr_SetFromErrno(PyExc_OSError); |
| 3446 | return NULL; |
| 3447 | } |
| 3448 | |
| 3449 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3450 | PyMem_Free(wstr); |
| 3451 | } |
| 3452 | else { |
| 3453 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3454 | wlen = mbstowcs(NULL, str, 0); |
| 3455 | #else |
| 3456 | wlen = len; |
| 3457 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3458 | if (wlen == (size_t)-1) |
| 3459 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3460 | if (wlen+1 <= smallbuf_len) { |
| 3461 | wstr = smallbuf; |
| 3462 | } |
| 3463 | else { |
| 3464 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3465 | return PyErr_NoMemory(); |
| 3466 | |
| 3467 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3468 | if (!wstr) |
| 3469 | return PyErr_NoMemory(); |
| 3470 | } |
| 3471 | |
| 3472 | /* This shouldn't fail now */ |
| 3473 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3474 | if (wlen2 == (size_t)-1) { |
| 3475 | if (wstr != smallbuf) |
| 3476 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3477 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3478 | } |
| 3479 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3480 | assert(wlen2 == wlen); |
| 3481 | #endif |
| 3482 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3483 | if (wstr != smallbuf) |
| 3484 | PyMem_Free(wstr); |
| 3485 | } |
| 3486 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3487 | |
| 3488 | decode_error: |
| 3489 | errmsg = strerror(errno); |
| 3490 | assert(errmsg != NULL); |
| 3491 | |
| 3492 | error_pos = mbstowcs_errorpos(str, len); |
| 3493 | if (errmsg != NULL) { |
| 3494 | size_t errlen; |
| 3495 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3496 | if (wstr != NULL) { |
| 3497 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3498 | PyMem_Free(wstr); |
| 3499 | } else |
| 3500 | errmsg = NULL; |
| 3501 | } |
| 3502 | if (errmsg == NULL) |
| 3503 | reason = PyUnicode_FromString( |
| 3504 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3505 | if (reason == NULL) |
| 3506 | return NULL; |
| 3507 | |
| 3508 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3509 | "locale", str, len, |
| 3510 | (Py_ssize_t)error_pos, |
| 3511 | (Py_ssize_t)(error_pos+1), |
| 3512 | reason); |
| 3513 | Py_DECREF(reason); |
| 3514 | if (exc != NULL) { |
| 3515 | PyCodec_StrictErrors(exc); |
| 3516 | Py_XDECREF(exc); |
| 3517 | } |
| 3518 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3522 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3523 | { |
| 3524 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3525 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | |
| 3529 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3530 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3531 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3532 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3533 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3534 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3535 | PyObject* |
| 3536 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3537 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3538 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3539 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3540 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3541 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3542 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3543 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3544 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3545 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3546 | the Python codec requires to encode at least its own filename. Use the C |
| 3547 | version of the locale codec until the codec registry is initialized and |
| 3548 | the Python codec is loaded. |
| 3549 | |
| 3550 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3551 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3552 | subinterpreters. */ |
| 3553 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3554 | return PyUnicode_Decode(s, size, |
| 3555 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3556 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3557 | } |
| 3558 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3559 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3560 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3561 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3564 | |
| 3565 | int |
| 3566 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3567 | { |
| 3568 | PyObject *output = NULL; |
| 3569 | Py_ssize_t size; |
| 3570 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3571 | if (arg == NULL) { |
| 3572 | Py_DECREF(*(PyObject**)addr); |
| 3573 | return 1; |
| 3574 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3575 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3576 | output = arg; |
| 3577 | Py_INCREF(output); |
| 3578 | } |
| 3579 | else { |
| 3580 | arg = PyUnicode_FromObject(arg); |
| 3581 | if (!arg) |
| 3582 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3583 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3584 | Py_DECREF(arg); |
| 3585 | if (!output) |
| 3586 | return 0; |
| 3587 | if (!PyBytes_Check(output)) { |
| 3588 | Py_DECREF(output); |
| 3589 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3590 | return 0; |
| 3591 | } |
| 3592 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3593 | size = PyBytes_GET_SIZE(output); |
| 3594 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3595 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3596 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3597 | Py_DECREF(output); |
| 3598 | return 0; |
| 3599 | } |
| 3600 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3601 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3605 | int |
| 3606 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3607 | { |
| 3608 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3609 | if (arg == NULL) { |
| 3610 | Py_DECREF(*(PyObject**)addr); |
| 3611 | return 1; |
| 3612 | } |
| 3613 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3614 | if (PyUnicode_READY(arg)) |
| 3615 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3616 | output = arg; |
| 3617 | Py_INCREF(output); |
| 3618 | } |
| 3619 | else { |
| 3620 | arg = PyBytes_FromObject(arg); |
| 3621 | if (!arg) |
| 3622 | return 0; |
| 3623 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3624 | PyBytes_GET_SIZE(arg)); |
| 3625 | Py_DECREF(arg); |
| 3626 | if (!output) |
| 3627 | return 0; |
| 3628 | if (!PyUnicode_Check(output)) { |
| 3629 | Py_DECREF(output); |
| 3630 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3631 | return 0; |
| 3632 | } |
| 3633 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3634 | if (PyUnicode_READY(output) < 0) { |
| 3635 | Py_DECREF(output); |
| 3636 | return 0; |
| 3637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3638 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3639 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3640 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3641 | Py_DECREF(output); |
| 3642 | return 0; |
| 3643 | } |
| 3644 | *(PyObject**)addr = output; |
| 3645 | return Py_CLEANUP_SUPPORTED; |
| 3646 | } |
| 3647 | |
| 3648 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3649 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3650 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3651 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3652 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3653 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3654 | if (!PyUnicode_Check(unicode)) { |
| 3655 | PyErr_BadArgument(); |
| 3656 | return NULL; |
| 3657 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3658 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3659 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3660 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3661 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3662 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3663 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3664 | if (bytes == NULL) |
| 3665 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3666 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3667 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3668 | Py_DECREF(bytes); |
| 3669 | return NULL; |
| 3670 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3671 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3672 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3673 | PyBytes_AS_STRING(bytes), |
| 3674 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3675 | Py_DECREF(bytes); |
| 3676 | } |
| 3677 | |
| 3678 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3679 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3680 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3684 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3685 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3687 | } |
| 3688 | |
| 3689 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3690 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3691 | #endif |
| 3692 | |
| 3693 | |
| 3694 | Py_UNICODE * |
| 3695 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3696 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3697 | const unsigned char *one_byte; |
| 3698 | #if SIZEOF_WCHAR_T == 4 |
| 3699 | const Py_UCS2 *two_bytes; |
| 3700 | #else |
| 3701 | const Py_UCS4 *four_bytes; |
| 3702 | const Py_UCS4 *ucs4_end; |
| 3703 | Py_ssize_t num_surrogates; |
| 3704 | #endif |
| 3705 | wchar_t *w; |
| 3706 | wchar_t *wchar_end; |
| 3707 | |
| 3708 | if (!PyUnicode_Check(unicode)) { |
| 3709 | PyErr_BadArgument(); |
| 3710 | return NULL; |
| 3711 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3712 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3713 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3714 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3715 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3716 | |
| 3717 | #ifdef Py_DEBUG |
| 3718 | ++unicode_as_unicode_calls; |
| 3719 | #endif |
| 3720 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3721 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3722 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3723 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3724 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3725 | num_surrogates = 0; |
| 3726 | |
| 3727 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3728 | if (*four_bytes > 0xFFFF) |
| 3729 | ++num_surrogates; |
| 3730 | } |
| 3731 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3732 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3733 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3734 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3735 | PyErr_NoMemory(); |
| 3736 | return NULL; |
| 3737 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3738 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3739 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3740 | w = _PyUnicode_WSTR(unicode); |
| 3741 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3742 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3743 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3744 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3745 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3746 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3747 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3748 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3749 | } |
| 3750 | else |
| 3751 | *w = *four_bytes; |
| 3752 | |
| 3753 | if (w > wchar_end) { |
| 3754 | assert(0 && "Miscalculated string end"); |
| 3755 | } |
| 3756 | } |
| 3757 | *w = 0; |
| 3758 | #else |
| 3759 | /* sizeof(wchar_t) == 4 */ |
| 3760 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3761 | "should share memory already."); |
| 3762 | return NULL; |
| 3763 | #endif |
| 3764 | } |
| 3765 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3766 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3767 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3768 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3769 | PyErr_NoMemory(); |
| 3770 | return NULL; |
| 3771 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3772 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3773 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3774 | w = _PyUnicode_WSTR(unicode); |
| 3775 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3776 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3777 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3778 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3779 | for (; w < wchar_end; ++one_byte, ++w) |
| 3780 | *w = *one_byte; |
| 3781 | /* null-terminate the wstr */ |
| 3782 | *w = 0; |
| 3783 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3784 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3785 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3786 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3787 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3788 | *w = *two_bytes; |
| 3789 | /* null-terminate the wstr */ |
| 3790 | *w = 0; |
| 3791 | #else |
| 3792 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3793 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3794 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3795 | Py_FatalError("Impossible unicode object state, wstr " |
| 3796 | "and str should share memory already."); |
| 3797 | return NULL; |
| 3798 | #endif |
| 3799 | } |
| 3800 | else { |
| 3801 | assert(0 && "This should never happen."); |
| 3802 | } |
| 3803 | } |
| 3804 | } |
| 3805 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3806 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3807 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3810 | Py_UNICODE * |
| 3811 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3813 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3816 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3817 | Py_ssize_t |
| 3818 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | { |
| 3820 | if (!PyUnicode_Check(unicode)) { |
| 3821 | PyErr_BadArgument(); |
| 3822 | goto onError; |
| 3823 | } |
| 3824 | return PyUnicode_GET_SIZE(unicode); |
| 3825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3826 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3827 | return -1; |
| 3828 | } |
| 3829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3830 | Py_ssize_t |
| 3831 | PyUnicode_GetLength(PyObject *unicode) |
| 3832 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3833 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | PyErr_BadArgument(); |
| 3835 | return -1; |
| 3836 | } |
| 3837 | |
| 3838 | return PyUnicode_GET_LENGTH(unicode); |
| 3839 | } |
| 3840 | |
| 3841 | Py_UCS4 |
| 3842 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3843 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3844 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3845 | PyErr_BadArgument(); |
| 3846 | return (Py_UCS4)-1; |
| 3847 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3848 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3849 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3850 | return (Py_UCS4)-1; |
| 3851 | } |
| 3852 | return PyUnicode_READ_CHAR(unicode, index); |
| 3853 | } |
| 3854 | |
| 3855 | int |
| 3856 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3857 | { |
| 3858 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3859 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | return -1; |
| 3861 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3862 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3863 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3864 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3865 | return -1; |
| 3866 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3867 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3868 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3869 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3870 | index, ch); |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3874 | const char * |
| 3875 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3876 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3877 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3880 | /* create or adjust a UnicodeDecodeError */ |
| 3881 | static void |
| 3882 | make_decode_exception(PyObject **exceptionObject, |
| 3883 | const char *encoding, |
| 3884 | const char *input, Py_ssize_t length, |
| 3885 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3886 | const char *reason) |
| 3887 | { |
| 3888 | if (*exceptionObject == NULL) { |
| 3889 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3890 | encoding, input, length, startpos, endpos, reason); |
| 3891 | } |
| 3892 | else { |
| 3893 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3894 | goto onError; |
| 3895 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3896 | goto onError; |
| 3897 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3898 | goto onError; |
| 3899 | } |
| 3900 | return; |
| 3901 | |
| 3902 | onError: |
| 3903 | Py_DECREF(*exceptionObject); |
| 3904 | *exceptionObject = NULL; |
| 3905 | } |
| 3906 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3907 | /* error handling callback helper: |
| 3908 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3909 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3910 | and adjust various state variables. |
| 3911 | return 0 on success, -1 on error |
| 3912 | */ |
| 3913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3914 | static int |
| 3915 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3916 | const char *encoding, const char *reason, |
| 3917 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3918 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3919 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3920 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3921 | 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] | 3922 | |
| 3923 | PyObject *restuple = NULL; |
| 3924 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3925 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3926 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3927 | Py_ssize_t requiredsize; |
| 3928 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3929 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3930 | int res = -1; |
| 3931 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3932 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3933 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3934 | else |
| 3935 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3936 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3937 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3938 | *errorHandler = PyCodec_LookupError(errors); |
| 3939 | if (*errorHandler == NULL) |
| 3940 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3943 | make_decode_exception(exceptionObject, |
| 3944 | encoding, |
| 3945 | *input, *inend - *input, |
| 3946 | *startinpos, *endinpos, |
| 3947 | reason); |
| 3948 | if (*exceptionObject == NULL) |
| 3949 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3950 | |
| 3951 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3952 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3953 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3954 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3955 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3956 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3957 | } |
| 3958 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3959 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3960 | if (PyUnicode_READY(repunicode) < 0) |
| 3961 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* Copy back the bytes variables, which might have been modified by the |
| 3964 | callback */ |
| 3965 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3966 | if (!inputobj) |
| 3967 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3968 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3969 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3970 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3971 | *input = PyBytes_AS_STRING(inputobj); |
| 3972 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3973 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3974 | /* we can DECREF safely, as the exception has another reference, |
| 3975 | so the object won't go away. */ |
| 3976 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3977 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3979 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3980 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3981 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3982 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3983 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3984 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3985 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3986 | /* need more space? (at least enough for what we |
| 3987 | have+the replacement+the rest of the string (starting |
| 3988 | at the new input position), so we won't have to check space |
| 3989 | when there are no errors in the rest of the string) */ |
| 3990 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3991 | requiredsize = *outpos + replen + insize-newpos; |
| 3992 | if (requiredsize > outsize) { |
| 3993 | if (requiredsize<2*outsize) |
| 3994 | requiredsize = 2*outsize; |
| 3995 | if (unicode_resize(output, requiredsize) < 0) |
| 3996 | goto onError; |
| 3997 | } |
| 3998 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3999 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4000 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4001 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4002 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4003 | else { |
| 4004 | wchar_t *repwstr; |
| 4005 | Py_ssize_t repwlen; |
| 4006 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4007 | if (repwstr == NULL) |
| 4008 | goto onError; |
| 4009 | /* need more space? (at least enough for what we |
| 4010 | have+the replacement+the rest of the string (starting |
| 4011 | at the new input position), so we won't have to check space |
| 4012 | when there are no errors in the rest of the string) */ |
| 4013 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4014 | if (requiredsize > outsize) { |
| 4015 | if (requiredsize < 2*outsize) |
| 4016 | requiredsize = 2*outsize; |
| 4017 | if (unicode_resize(output, requiredsize) < 0) |
| 4018 | goto onError; |
| 4019 | } |
| 4020 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4021 | *outpos += repwlen; |
| 4022 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4023 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4024 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4025 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4026 | /* we made it! */ |
| 4027 | res = 0; |
| 4028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4030 | Py_XDECREF(restuple); |
| 4031 | return res; |
| 4032 | } |
| 4033 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4034 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4035 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4036 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4037 | |
| 4038 | /* Three simple macros defining base-64. */ |
| 4039 | |
| 4040 | /* Is c a base-64 character? */ |
| 4041 | |
| 4042 | #define IS_BASE64(c) \ |
| 4043 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4044 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4045 | ((c) >= '0' && (c) <= '9') || \ |
| 4046 | (c) == '+' || (c) == '/') |
| 4047 | |
| 4048 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4049 | |
| 4050 | #define FROM_BASE64(c) \ |
| 4051 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4052 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4053 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4054 | (c) == '+' ? 62 : 63) |
| 4055 | |
| 4056 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4057 | |
| 4058 | #define TO_BASE64(n) \ |
| 4059 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4060 | |
| 4061 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4062 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4063 | * byte not decoding to itself is the + which begins a base64 |
| 4064 | * string. */ |
| 4065 | |
| 4066 | #define DECODE_DIRECT(c) \ |
| 4067 | ((c) <= 127 && (c) != '+') |
| 4068 | |
| 4069 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4070 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4071 | * the above). See RFC2152. This array identifies these different |
| 4072 | * sets: |
| 4073 | * 0 : "Set D" |
| 4074 | * alphanumeric and '(),-./:? |
| 4075 | * 1 : "Set O" |
| 4076 | * !"#$%&*;<=>@[]^_`{|} |
| 4077 | * 2 : "whitespace" |
| 4078 | * ht nl cr sp |
| 4079 | * 3 : special (must be base64 encoded) |
| 4080 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4081 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4082 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4083 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4084 | char utf7_category[128] = { |
| 4085 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4086 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4087 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4088 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4089 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4090 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4091 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4092 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4093 | /* @ A B C D E F G H I J K L M N O */ |
| 4094 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4095 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4096 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4097 | /* ` a b c d e f g h i j k l m n o */ |
| 4098 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4099 | /* p q r s t u v w x y z { | } ~ del */ |
| 4100 | 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] | 4101 | }; |
| 4102 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4103 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4104 | * answer depends on whether we are encoding set O as itself, and also |
| 4105 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4106 | * clear that the answers to these questions vary between |
| 4107 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4108 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4109 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4110 | ((c) < 128 && (c) > 0 && \ |
| 4111 | ((utf7_category[(c)] == 0) || \ |
| 4112 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4113 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4114 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4115 | PyObject * |
| 4116 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4117 | Py_ssize_t size, |
| 4118 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4119 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4120 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4121 | } |
| 4122 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4123 | /* The decoder. The only state we preserve is our read position, |
| 4124 | * i.e. how many characters we have consumed. So if we end in the |
| 4125 | * middle of a shift sequence we have to back off the read position |
| 4126 | * and the output to the beginning of the sequence, otherwise we lose |
| 4127 | * all the shift state (seen bits, number of bits seen, high |
| 4128 | * surrogate). */ |
| 4129 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4130 | PyObject * |
| 4131 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4132 | Py_ssize_t size, |
| 4133 | const char *errors, |
| 4134 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4135 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4136 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4137 | Py_ssize_t startinpos; |
| 4138 | Py_ssize_t endinpos; |
| 4139 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4140 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4141 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4142 | const char *errmsg = ""; |
| 4143 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4144 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4145 | unsigned int base64bits = 0; |
| 4146 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4147 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4148 | PyObject *errorHandler = NULL; |
| 4149 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4150 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4151 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4152 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4153 | if (!unicode) |
| 4154 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4155 | if (size == 0) { |
| 4156 | if (consumed) |
| 4157 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4158 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4159 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4160 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4161 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4162 | e = s + size; |
| 4163 | |
| 4164 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4165 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4167 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4168 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4169 | if (inShift) { /* in a base-64 section */ |
| 4170 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4171 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4172 | base64bits += 6; |
| 4173 | s++; |
| 4174 | if (base64bits >= 16) { |
| 4175 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4176 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4177 | base64bits -= 16; |
| 4178 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4179 | if (surrogate) { |
| 4180 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4181 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4182 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4183 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4184 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4185 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4186 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4187 | } |
| 4188 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4189 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4190 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4191 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4192 | } |
| 4193 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4194 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4195 | /* first surrogate */ |
| 4196 | surrogate = outCh; |
| 4197 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4198 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4199 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4200 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | } |
| 4204 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4205 | inShift = 0; |
| 4206 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4207 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4208 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4209 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4210 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4211 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4212 | if (base64bits > 0) { /* left-over bits */ |
| 4213 | if (base64bits >= 6) { |
| 4214 | /* We've seen at least one base-64 character */ |
| 4215 | errmsg = "partial character in shift sequence"; |
| 4216 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4217 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4218 | else { |
| 4219 | /* Some bits remain; they should be zero */ |
| 4220 | if (base64buffer != 0) { |
| 4221 | errmsg = "non-zero padding bits in shift sequence"; |
| 4222 | goto utf7Error; |
| 4223 | } |
| 4224 | } |
| 4225 | } |
| 4226 | if (ch != '-') { |
| 4227 | /* '-' is absorbed; other terminating |
| 4228 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4229 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4230 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4231 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4232 | } |
| 4233 | } |
| 4234 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4235 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4236 | s++; /* consume '+' */ |
| 4237 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4238 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4239 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4240 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4241 | } |
| 4242 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4243 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4244 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4245 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4246 | } |
| 4247 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4248 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4249 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4250 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | s++; |
| 4252 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4253 | else { |
| 4254 | startinpos = s-starts; |
| 4255 | s++; |
| 4256 | errmsg = "unexpected special character"; |
| 4257 | goto utf7Error; |
| 4258 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4259 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4260 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4261 | endinpos = s-starts; |
| 4262 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4263 | errors, &errorHandler, |
| 4264 | "utf7", errmsg, |
| 4265 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4266 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4267 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4270 | /* end of string */ |
| 4271 | |
| 4272 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4273 | /* if we're in an inconsistent state, that's an error */ |
| 4274 | if (surrogate || |
| 4275 | (base64bits >= 6) || |
| 4276 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4277 | endinpos = size; |
| 4278 | if (unicode_decode_call_errorhandler( |
| 4279 | errors, &errorHandler, |
| 4280 | "utf7", "unterminated shift sequence", |
| 4281 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4282 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4283 | goto onError; |
| 4284 | if (s < e) |
| 4285 | goto restart; |
| 4286 | } |
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 | |
| 4289 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4290 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4291 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4292 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4293 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4294 | } |
| 4295 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4296 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4297 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4298 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4299 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4300 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4301 | goto onError; |
| 4302 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4303 | Py_XDECREF(errorHandler); |
| 4304 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4305 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4307 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4308 | Py_XDECREF(errorHandler); |
| 4309 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | Py_DECREF(unicode); |
| 4311 | return NULL; |
| 4312 | } |
| 4313 | |
| 4314 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4315 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4316 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4317 | int base64SetO, |
| 4318 | int base64WhiteSpace, |
| 4319 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4321 | int kind; |
| 4322 | void *data; |
| 4323 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4324 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4325 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4326 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4327 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4328 | unsigned int base64bits = 0; |
| 4329 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | char * out; |
| 4331 | char * start; |
| 4332 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4333 | if (PyUnicode_READY(str) < 0) |
| 4334 | return NULL; |
| 4335 | kind = PyUnicode_KIND(str); |
| 4336 | data = PyUnicode_DATA(str); |
| 4337 | len = PyUnicode_GET_LENGTH(str); |
| 4338 | |
| 4339 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4340 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4341 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4342 | /* It might be possible to tighten this worst case */ |
| 4343 | allocated = 8 * len; |
| 4344 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4345 | return PyErr_NoMemory(); |
| 4346 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4347 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4348 | if (v == NULL) |
| 4349 | return NULL; |
| 4350 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4351 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4352 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4353 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4354 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | if (inShift) { |
| 4356 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4357 | /* shifting out */ |
| 4358 | if (base64bits) { /* output remaining bits */ |
| 4359 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4360 | base64buffer = 0; |
| 4361 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4362 | } |
| 4363 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4365 | so no '-' is required, except if the character is itself a '-' */ |
| 4366 | if (IS_BASE64(ch) || ch == '-') { |
| 4367 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4368 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | *out++ = (char) ch; |
| 4370 | } |
| 4371 | else { |
| 4372 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4373 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4375 | else { /* not in a shift sequence */ |
| 4376 | if (ch == '+') { |
| 4377 | *out++ = '+'; |
| 4378 | *out++ = '-'; |
| 4379 | } |
| 4380 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4381 | *out++ = (char) ch; |
| 4382 | } |
| 4383 | else { |
| 4384 | *out++ = '+'; |
| 4385 | inShift = 1; |
| 4386 | goto encode_char; |
| 4387 | } |
| 4388 | } |
| 4389 | continue; |
| 4390 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4392 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4393 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4394 | /* code first surrogate */ |
| 4395 | base64bits += 16; |
| 4396 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4397 | while (base64bits >= 6) { |
| 4398 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4399 | base64bits -= 6; |
| 4400 | } |
| 4401 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4402 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4404 | base64bits += 16; |
| 4405 | base64buffer = (base64buffer << 16) | ch; |
| 4406 | while (base64bits >= 6) { |
| 4407 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4408 | base64bits -= 6; |
| 4409 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4410 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4411 | if (base64bits) |
| 4412 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4413 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4414 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4415 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4416 | return NULL; |
| 4417 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4418 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4419 | PyObject * |
| 4420 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4421 | Py_ssize_t size, |
| 4422 | int base64SetO, |
| 4423 | int base64WhiteSpace, |
| 4424 | const char *errors) |
| 4425 | { |
| 4426 | PyObject *result; |
| 4427 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4428 | if (tmp == NULL) |
| 4429 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4430 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4431 | base64WhiteSpace, errors); |
| 4432 | Py_DECREF(tmp); |
| 4433 | return result; |
| 4434 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4435 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4436 | #undef IS_BASE64 |
| 4437 | #undef FROM_BASE64 |
| 4438 | #undef TO_BASE64 |
| 4439 | #undef DECODE_DIRECT |
| 4440 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4443 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4444 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4445 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4446 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4447 | illegal prefix. See RFC 3629 for details */ |
| 4448 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4449 | 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] | 4450 | 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] | 4451 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4452 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4453 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4454 | 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] | 4455 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4456 | 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] | 4457 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4458 | 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] | 4459 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4460 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4461 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4462 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4463 | 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] | 4464 | }; |
| 4465 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4466 | PyObject * |
| 4467 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4468 | Py_ssize_t size, |
| 4469 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4470 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4471 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4472 | } |
| 4473 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4474 | #include "stringlib/ucs1lib.h" |
| 4475 | #include "stringlib/codecs.h" |
| 4476 | #include "stringlib/undef.h" |
| 4477 | |
| 4478 | #include "stringlib/ucs2lib.h" |
| 4479 | #include "stringlib/codecs.h" |
| 4480 | #include "stringlib/undef.h" |
| 4481 | |
| 4482 | #include "stringlib/ucs4lib.h" |
| 4483 | #include "stringlib/codecs.h" |
| 4484 | #include "stringlib/undef.h" |
| 4485 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4486 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4487 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4488 | |
| 4489 | /* Mask to quickly check whether a C 'long' contains a |
| 4490 | non-ASCII, UTF8-encoded char. */ |
| 4491 | #if (SIZEOF_LONG == 8) |
| 4492 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4493 | #elif (SIZEOF_LONG == 4) |
| 4494 | # define ASCII_CHAR_MASK 0x80808080L |
| 4495 | #else |
| 4496 | # error C 'long' size should be either 4 or 8! |
| 4497 | #endif |
| 4498 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4499 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4500 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4501 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4502 | 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] | 4503 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4504 | */ |
| 4505 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4506 | 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] | 4507 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4508 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4509 | const unsigned char *end = p + string_size; |
| 4510 | 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] | 4511 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4512 | assert(unicode_size != NULL); |
| 4513 | |
| 4514 | /* By having a cascade of independent loops which fallback onto each |
| 4515 | other, we minimize the amount of work done in the average loop |
| 4516 | iteration, and we also maximize the CPU's ability to predict |
| 4517 | branches correctly (because a given condition will have always the |
| 4518 | same boolean outcome except perhaps in the last iteration of the |
| 4519 | corresponding loop). |
| 4520 | In the general case this brings us rather close to decoding |
| 4521 | performance pre-PEP 393, despite the two-pass decoding. |
| 4522 | |
| 4523 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4524 | character has been encountered. It is actually a pessimization (by |
| 4525 | a significant factor) to use this loop on text with many non-ASCII |
| 4526 | characters, and it is important to avoid bad performance on valid |
| 4527 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4528 | */ |
| 4529 | |
| 4530 | /* ASCII */ |
| 4531 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4532 | /* Only check value if it's not a ASCII char... */ |
| 4533 | if (*p < 0x80) { |
| 4534 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4535 | an explanation. */ |
| 4536 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4537 | /* Help register allocation */ |
| 4538 | register const unsigned char *_p = p; |
| 4539 | while (_p < aligned_end) { |
| 4540 | unsigned long value = *(unsigned long *) _p; |
| 4541 | if (value & ASCII_CHAR_MASK) |
| 4542 | break; |
| 4543 | _p += SIZEOF_LONG; |
| 4544 | char_count += SIZEOF_LONG; |
| 4545 | } |
| 4546 | p = _p; |
| 4547 | if (p == end) |
| 4548 | break; |
| 4549 | } |
| 4550 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4551 | if (*p < 0x80) |
| 4552 | ++char_count; |
| 4553 | else |
| 4554 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4555 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4556 | *unicode_size = char_count; |
| 4557 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4558 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4559 | _ucs1loop: |
| 4560 | for (; p < end; ++p) { |
| 4561 | if (*p < 0xc4) |
| 4562 | char_count += ((*p & 0xc0) != 0x80); |
| 4563 | else |
| 4564 | goto _ucs2loop; |
| 4565 | } |
| 4566 | *unicode_size = char_count; |
| 4567 | return 255; |
| 4568 | |
| 4569 | _ucs2loop: |
| 4570 | for (; p < end; ++p) { |
| 4571 | if (*p < 0xf0) |
| 4572 | char_count += ((*p & 0xc0) != 0x80); |
| 4573 | else |
| 4574 | goto _ucs4loop; |
| 4575 | } |
| 4576 | *unicode_size = char_count; |
| 4577 | return 65535; |
| 4578 | |
| 4579 | _ucs4loop: |
| 4580 | for (; p < end; ++p) { |
| 4581 | char_count += ((*p & 0xc0) != 0x80); |
| 4582 | } |
| 4583 | *unicode_size = char_count; |
| 4584 | return 65537; |
| 4585 | } |
| 4586 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4587 | /* 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] | 4588 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4589 | 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] | 4590 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4591 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4592 | do { \ |
| 4593 | Py_ssize_t pos = index; \ |
| 4594 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4595 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4596 | goto onError; \ |
| 4597 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4598 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4599 | } while (0) |
| 4600 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4601 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4602 | decode_utf8_errors(const char *starts, |
| 4603 | Py_ssize_t size, |
| 4604 | const char *errors, |
| 4605 | Py_ssize_t *consumed, |
| 4606 | const char *s, |
| 4607 | PyObject *unicode, |
| 4608 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4609 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4611 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4612 | Py_ssize_t startinpos; |
| 4613 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4614 | const char *e = starts + size; |
| 4615 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4616 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4617 | PyObject *errorHandler = NULL; |
| 4618 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4619 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4620 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | |
| 4622 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4623 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4624 | |
| 4625 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4626 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4627 | input will consist of an overwhelming majority of ASCII |
| 4628 | characters, we try to optimize for this case by checking |
| 4629 | as many characters as a C 'long' can contain. |
| 4630 | First, check if we can do an aligned read, as most CPUs have |
| 4631 | a penalty for unaligned reads. |
| 4632 | */ |
| 4633 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4634 | /* Help register allocation */ |
| 4635 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4636 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4637 | while (_s < aligned_end) { |
| 4638 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4639 | and do a fast unrolled copy if it only contains ASCII |
| 4640 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4641 | unsigned long value = *(unsigned long *) _s; |
| 4642 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4643 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4644 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4645 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4646 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4647 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4648 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4649 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4650 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4651 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4652 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4653 | #endif |
| 4654 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4655 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4656 | } |
| 4657 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4658 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4659 | if (s == e) |
| 4660 | break; |
| 4661 | ch = (unsigned char)*s; |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4666 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4667 | s++; |
| 4668 | continue; |
| 4669 | } |
| 4670 | |
| 4671 | n = utf8_code_length[ch]; |
| 4672 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4673 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4674 | if (consumed) |
| 4675 | break; |
| 4676 | else { |
| 4677 | errmsg = "unexpected end of data"; |
| 4678 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4679 | endinpos = startinpos+1; |
| 4680 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4681 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4682 | goto utf8Error; |
| 4683 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4685 | |
| 4686 | switch (n) { |
| 4687 | |
| 4688 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4689 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4690 | startinpos = s-starts; |
| 4691 | endinpos = startinpos+1; |
| 4692 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4693 | |
| 4694 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4695 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4696 | startinpos = s-starts; |
| 4697 | endinpos = startinpos+1; |
| 4698 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | |
| 4700 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4701 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4702 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4703 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4704 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4705 | goto utf8Error; |
| 4706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4707 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4708 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4709 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | break; |
| 4711 | |
| 4712 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4713 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4714 | will result in surrogates in range d800-dfff. Surrogates are |
| 4715 | not valid UTF-8 so they are rejected. |
| 4716 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4717 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4718 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4719 | (s[2] & 0xc0) != 0x80 || |
| 4720 | ((unsigned char)s[0] == 0xE0 && |
| 4721 | (unsigned char)s[1] < 0xA0) || |
| 4722 | ((unsigned char)s[0] == 0xED && |
| 4723 | (unsigned char)s[1] > 0x9F)) { |
| 4724 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4725 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4726 | endinpos = startinpos + 1; |
| 4727 | |
| 4728 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4729 | continuation byte is s[2], so increment endinpos by 1, |
| 4730 | if not, s[1] is invalid and endinpos doesn't need to |
| 4731 | be incremented. */ |
| 4732 | if ((s[1] & 0xC0) == 0x80) |
| 4733 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4734 | goto utf8Error; |
| 4735 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4737 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4738 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4739 | break; |
| 4740 | |
| 4741 | case 4: |
| 4742 | if ((s[1] & 0xc0) != 0x80 || |
| 4743 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4744 | (s[3] & 0xc0) != 0x80 || |
| 4745 | ((unsigned char)s[0] == 0xF0 && |
| 4746 | (unsigned char)s[1] < 0x90) || |
| 4747 | ((unsigned char)s[0] == 0xF4 && |
| 4748 | (unsigned char)s[1] > 0x8F)) { |
| 4749 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4750 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4751 | endinpos = startinpos + 1; |
| 4752 | if ((s[1] & 0xC0) == 0x80) { |
| 4753 | endinpos++; |
| 4754 | if ((s[2] & 0xC0) == 0x80) |
| 4755 | endinpos++; |
| 4756 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4757 | goto utf8Error; |
| 4758 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4759 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4760 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4761 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4762 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4763 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4764 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | } |
| 4766 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4767 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4769 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4770 | if (unicode_decode_call_errorhandler( |
| 4771 | errors, &errorHandler, |
| 4772 | "utf8", errmsg, |
| 4773 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4774 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4775 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4776 | /* Update data because unicode_decode_call_errorhandler might have |
| 4777 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4778 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4779 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4780 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4781 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4782 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4783 | /* Adjust length and ready string when it contained errors and |
| 4784 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4785 | if (unicode_resize(&unicode, i) < 0) |
| 4786 | goto onError; |
| 4787 | unicode_adjust_maxchar(&unicode); |
| 4788 | if (unicode == NULL) |
| 4789 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4791 | Py_XDECREF(errorHandler); |
| 4792 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4793 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4794 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4796 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4797 | Py_XDECREF(errorHandler); |
| 4798 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4799 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4800 | return NULL; |
| 4801 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4802 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4803 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4804 | PyObject * |
| 4805 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4806 | Py_ssize_t size, |
| 4807 | const char *errors, |
| 4808 | Py_ssize_t *consumed) |
| 4809 | { |
| 4810 | Py_UCS4 maxchar = 0; |
| 4811 | Py_ssize_t unicode_size; |
| 4812 | int has_errors = 0; |
| 4813 | PyObject *unicode; |
| 4814 | int kind; |
| 4815 | void *data; |
| 4816 | const char *starts = s; |
| 4817 | const char *e; |
| 4818 | Py_ssize_t i; |
| 4819 | |
| 4820 | if (size == 0) { |
| 4821 | if (consumed) |
| 4822 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4823 | Py_INCREF(unicode_empty); |
| 4824 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4825 | } |
| 4826 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4827 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4828 | |
| 4829 | /* When the string is ASCII only, just use memcpy and return. |
| 4830 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4831 | sequence at the end of the ASCII block. */ |
| 4832 | if (maxchar < 128 && size == unicode_size) { |
| 4833 | if (consumed) |
| 4834 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4835 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4839 | if (!unicode) |
| 4840 | return NULL; |
| 4841 | kind = PyUnicode_KIND(unicode); |
| 4842 | data = PyUnicode_DATA(unicode); |
| 4843 | |
| 4844 | /* Unpack UTF-8 encoded data */ |
| 4845 | i = 0; |
| 4846 | e = starts + size; |
| 4847 | switch (kind) { |
| 4848 | case PyUnicode_1BYTE_KIND: |
| 4849 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4850 | break; |
| 4851 | case PyUnicode_2BYTE_KIND: |
| 4852 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4853 | break; |
| 4854 | case PyUnicode_4BYTE_KIND: |
| 4855 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4856 | break; |
| 4857 | } |
| 4858 | if (!has_errors) { |
| 4859 | /* Ensure the unicode size calculation was correct */ |
| 4860 | assert(i == unicode_size); |
| 4861 | assert(s == e); |
| 4862 | if (consumed) |
| 4863 | *consumed = size; |
| 4864 | return unicode; |
| 4865 | } |
| 4866 | |
| 4867 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4868 | code below refits and resizes as necessary. */ |
| 4869 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 4870 | } |
| 4871 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4872 | #ifdef __APPLE__ |
| 4873 | |
| 4874 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4875 | used to decode the command line arguments on Mac OS X. */ |
| 4876 | |
| 4877 | wchar_t* |
| 4878 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4879 | { |
| 4880 | int n; |
| 4881 | const char *e; |
| 4882 | wchar_t *unicode, *p; |
| 4883 | |
| 4884 | /* Note: size will always be longer than the resulting Unicode |
| 4885 | character count */ |
| 4886 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4887 | PyErr_NoMemory(); |
| 4888 | return NULL; |
| 4889 | } |
| 4890 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4891 | if (!unicode) |
| 4892 | return NULL; |
| 4893 | |
| 4894 | /* Unpack UTF-8 encoded data */ |
| 4895 | p = unicode; |
| 4896 | e = s + size; |
| 4897 | while (s < e) { |
| 4898 | Py_UCS4 ch = (unsigned char)*s; |
| 4899 | |
| 4900 | if (ch < 0x80) { |
| 4901 | *p++ = (wchar_t)ch; |
| 4902 | s++; |
| 4903 | continue; |
| 4904 | } |
| 4905 | |
| 4906 | n = utf8_code_length[ch]; |
| 4907 | if (s + n > e) { |
| 4908 | goto surrogateescape; |
| 4909 | } |
| 4910 | |
| 4911 | switch (n) { |
| 4912 | case 0: |
| 4913 | case 1: |
| 4914 | goto surrogateescape; |
| 4915 | |
| 4916 | case 2: |
| 4917 | if ((s[1] & 0xc0) != 0x80) |
| 4918 | goto surrogateescape; |
| 4919 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4920 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4921 | *p++ = (wchar_t)ch; |
| 4922 | break; |
| 4923 | |
| 4924 | case 3: |
| 4925 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4926 | will result in surrogates in range d800-dfff. Surrogates are |
| 4927 | not valid UTF-8 so they are rejected. |
| 4928 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4929 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4930 | if ((s[1] & 0xc0) != 0x80 || |
| 4931 | (s[2] & 0xc0) != 0x80 || |
| 4932 | ((unsigned char)s[0] == 0xE0 && |
| 4933 | (unsigned char)s[1] < 0xA0) || |
| 4934 | ((unsigned char)s[0] == 0xED && |
| 4935 | (unsigned char)s[1] > 0x9F)) { |
| 4936 | |
| 4937 | goto surrogateescape; |
| 4938 | } |
| 4939 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4940 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4941 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4942 | break; |
| 4943 | |
| 4944 | case 4: |
| 4945 | if ((s[1] & 0xc0) != 0x80 || |
| 4946 | (s[2] & 0xc0) != 0x80 || |
| 4947 | (s[3] & 0xc0) != 0x80 || |
| 4948 | ((unsigned char)s[0] == 0xF0 && |
| 4949 | (unsigned char)s[1] < 0x90) || |
| 4950 | ((unsigned char)s[0] == 0xF4 && |
| 4951 | (unsigned char)s[1] > 0x8F)) { |
| 4952 | goto surrogateescape; |
| 4953 | } |
| 4954 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4955 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4956 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4957 | |
| 4958 | #if SIZEOF_WCHAR_T == 4 |
| 4959 | *p++ = (wchar_t)ch; |
| 4960 | #else |
| 4961 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4962 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4963 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4964 | #endif |
| 4965 | break; |
| 4966 | } |
| 4967 | s += n; |
| 4968 | continue; |
| 4969 | |
| 4970 | surrogateescape: |
| 4971 | *p++ = 0xDC00 + ch; |
| 4972 | s++; |
| 4973 | } |
| 4974 | *p = L'\0'; |
| 4975 | return unicode; |
| 4976 | } |
| 4977 | |
| 4978 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4980 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4981 | |
| 4982 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4983 | and allocate exactly as much space needed at the end. Else allocate the |
| 4984 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4985 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4986 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4987 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4988 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4989 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4990 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4991 | void *data; |
| 4992 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4993 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4994 | if (!PyUnicode_Check(unicode)) { |
| 4995 | PyErr_BadArgument(); |
| 4996 | return NULL; |
| 4997 | } |
| 4998 | |
| 4999 | if (PyUnicode_READY(unicode) == -1) |
| 5000 | return NULL; |
| 5001 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5002 | if (PyUnicode_UTF8(unicode)) |
| 5003 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5004 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5005 | |
| 5006 | kind = PyUnicode_KIND(unicode); |
| 5007 | data = PyUnicode_DATA(unicode); |
| 5008 | size = PyUnicode_GET_LENGTH(unicode); |
| 5009 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5010 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5011 | default: |
| 5012 | assert(0); |
| 5013 | case PyUnicode_1BYTE_KIND: |
| 5014 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5015 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5016 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5017 | case PyUnicode_2BYTE_KIND: |
| 5018 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5019 | case PyUnicode_4BYTE_KIND: |
| 5020 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5021 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5024 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5025 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5026 | Py_ssize_t size, |
| 5027 | const char *errors) |
| 5028 | { |
| 5029 | PyObject *v, *unicode; |
| 5030 | |
| 5031 | unicode = PyUnicode_FromUnicode(s, size); |
| 5032 | if (unicode == NULL) |
| 5033 | return NULL; |
| 5034 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5035 | Py_DECREF(unicode); |
| 5036 | return v; |
| 5037 | } |
| 5038 | |
| 5039 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5040 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5041 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5042 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5045 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5046 | |
| 5047 | PyObject * |
| 5048 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | Py_ssize_t size, |
| 5050 | const char *errors, |
| 5051 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5052 | { |
| 5053 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5054 | } |
| 5055 | |
| 5056 | PyObject * |
| 5057 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5058 | Py_ssize_t size, |
| 5059 | const char *errors, |
| 5060 | int *byteorder, |
| 5061 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | { |
| 5063 | const char *starts = s; |
| 5064 | Py_ssize_t startinpos; |
| 5065 | Py_ssize_t endinpos; |
| 5066 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5067 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5068 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5069 | int bo = 0; /* assume native ordering by default */ |
| 5070 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5071 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5072 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5073 | int iorder[] = {0, 1, 2, 3}; |
| 5074 | #else |
| 5075 | int iorder[] = {3, 2, 1, 0}; |
| 5076 | #endif |
| 5077 | PyObject *errorHandler = NULL; |
| 5078 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5079 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | q = (unsigned char *)s; |
| 5081 | e = q + size; |
| 5082 | |
| 5083 | if (byteorder) |
| 5084 | bo = *byteorder; |
| 5085 | |
| 5086 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5087 | byte order setting accordingly. In native mode, the leading BOM |
| 5088 | mark is skipped, in all other modes, it is copied to the output |
| 5089 | stream as-is (giving a ZWNBSP character). */ |
| 5090 | if (bo == 0) { |
| 5091 | if (size >= 4) { |
| 5092 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5093 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5094 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | if (bom == 0x0000FEFF) { |
| 5096 | q += 4; |
| 5097 | bo = -1; |
| 5098 | } |
| 5099 | else if (bom == 0xFFFE0000) { |
| 5100 | q += 4; |
| 5101 | bo = 1; |
| 5102 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5103 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5104 | if (bom == 0x0000FEFF) { |
| 5105 | q += 4; |
| 5106 | bo = 1; |
| 5107 | } |
| 5108 | else if (bom == 0xFFFE0000) { |
| 5109 | q += 4; |
| 5110 | bo = -1; |
| 5111 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5112 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5113 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | if (bo == -1) { |
| 5117 | /* force LE */ |
| 5118 | iorder[0] = 0; |
| 5119 | iorder[1] = 1; |
| 5120 | iorder[2] = 2; |
| 5121 | iorder[3] = 3; |
| 5122 | } |
| 5123 | else if (bo == 1) { |
| 5124 | /* force BE */ |
| 5125 | iorder[0] = 3; |
| 5126 | iorder[1] = 2; |
| 5127 | iorder[2] = 1; |
| 5128 | iorder[3] = 0; |
| 5129 | } |
| 5130 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5131 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5132 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5133 | if (!unicode) |
| 5134 | return NULL; |
| 5135 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5136 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5137 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5138 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5139 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5140 | Py_UCS4 ch; |
| 5141 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5142 | if (e-q<4) { |
| 5143 | if (consumed) |
| 5144 | break; |
| 5145 | errmsg = "truncated data"; |
| 5146 | startinpos = ((const char *)q)-starts; |
| 5147 | endinpos = ((const char *)e)-starts; |
| 5148 | goto utf32Error; |
| 5149 | /* The remaining input chars are ignored if the callback |
| 5150 | chooses to skip the input */ |
| 5151 | } |
| 5152 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5153 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5154 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5155 | if (ch >= 0x110000) |
| 5156 | { |
| 5157 | errmsg = "codepoint not in range(0x110000)"; |
| 5158 | startinpos = ((const char *)q)-starts; |
| 5159 | endinpos = startinpos+4; |
| 5160 | goto utf32Error; |
| 5161 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5162 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5163 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5164 | q += 4; |
| 5165 | continue; |
| 5166 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5167 | if (unicode_decode_call_errorhandler( |
| 5168 | errors, &errorHandler, |
| 5169 | "utf32", errmsg, |
| 5170 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5171 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5172 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | if (byteorder) |
| 5176 | *byteorder = bo; |
| 5177 | |
| 5178 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5179 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5180 | |
| 5181 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5182 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5183 | goto onError; |
| 5184 | |
| 5185 | Py_XDECREF(errorHandler); |
| 5186 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5187 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5190 | Py_DECREF(unicode); |
| 5191 | Py_XDECREF(errorHandler); |
| 5192 | Py_XDECREF(exc); |
| 5193 | return NULL; |
| 5194 | } |
| 5195 | |
| 5196 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5197 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5198 | const char *errors, |
| 5199 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5200 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5201 | int kind; |
| 5202 | void *data; |
| 5203 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5204 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5205 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5206 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5207 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5208 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5209 | int iorder[] = {0, 1, 2, 3}; |
| 5210 | #else |
| 5211 | int iorder[] = {3, 2, 1, 0}; |
| 5212 | #endif |
| 5213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5214 | #define STORECHAR(CH) \ |
| 5215 | do { \ |
| 5216 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5217 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5218 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5219 | p[iorder[0]] = (CH) & 0xff; \ |
| 5220 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5221 | } while(0) |
| 5222 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5223 | if (!PyUnicode_Check(str)) { |
| 5224 | PyErr_BadArgument(); |
| 5225 | return NULL; |
| 5226 | } |
| 5227 | if (PyUnicode_READY(str) < 0) |
| 5228 | return NULL; |
| 5229 | kind = PyUnicode_KIND(str); |
| 5230 | data = PyUnicode_DATA(str); |
| 5231 | len = PyUnicode_GET_LENGTH(str); |
| 5232 | |
| 5233 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5234 | bytesize = nsize * 4; |
| 5235 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5237 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5238 | if (v == NULL) |
| 5239 | return NULL; |
| 5240 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5241 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5242 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5244 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5245 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5246 | |
| 5247 | if (byteorder == -1) { |
| 5248 | /* force LE */ |
| 5249 | iorder[0] = 0; |
| 5250 | iorder[1] = 1; |
| 5251 | iorder[2] = 2; |
| 5252 | iorder[3] = 3; |
| 5253 | } |
| 5254 | else if (byteorder == 1) { |
| 5255 | /* force BE */ |
| 5256 | iorder[0] = 3; |
| 5257 | iorder[1] = 2; |
| 5258 | iorder[2] = 1; |
| 5259 | iorder[3] = 0; |
| 5260 | } |
| 5261 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5262 | for (i = 0; i < len; i++) |
| 5263 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5264 | |
| 5265 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5266 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5267 | #undef STORECHAR |
| 5268 | } |
| 5269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5270 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5271 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5272 | Py_ssize_t size, |
| 5273 | const char *errors, |
| 5274 | int byteorder) |
| 5275 | { |
| 5276 | PyObject *result; |
| 5277 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5278 | if (tmp == NULL) |
| 5279 | return NULL; |
| 5280 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5281 | Py_DECREF(tmp); |
| 5282 | return result; |
| 5283 | } |
| 5284 | |
| 5285 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5286 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5287 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5288 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5291 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5292 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5293 | PyObject * |
| 5294 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5295 | Py_ssize_t size, |
| 5296 | const char *errors, |
| 5297 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5299 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5300 | } |
| 5301 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5302 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5303 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5304 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5305 | rare in most input. |
| 5306 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5307 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5309 | #if (SIZEOF_LONG == 8) |
| 5310 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5311 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5312 | #elif (SIZEOF_LONG == 4) |
| 5313 | # define FAST_CHAR_MASK 0x80008000L |
| 5314 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5315 | #else |
| 5316 | # error C 'long' size should be either 4 or 8! |
| 5317 | #endif |
| 5318 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5319 | PyObject * |
| 5320 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | Py_ssize_t size, |
| 5322 | const char *errors, |
| 5323 | int *byteorder, |
| 5324 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5325 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5326 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5327 | Py_ssize_t startinpos; |
| 5328 | Py_ssize_t endinpos; |
| 5329 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5330 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5331 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5332 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5333 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5334 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5335 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5336 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5337 | int ihi = 1, ilo = 0; |
| 5338 | #else |
| 5339 | int ihi = 0, ilo = 1; |
| 5340 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5341 | PyObject *errorHandler = NULL; |
| 5342 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5343 | |
| 5344 | /* Note: size will always be longer than the resulting Unicode |
| 5345 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5346 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5347 | if (!unicode) |
| 5348 | return NULL; |
| 5349 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5350 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5351 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5353 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5354 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | |
| 5356 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5357 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5359 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5360 | byte order setting accordingly. In native mode, the leading BOM |
| 5361 | mark is skipped, in all other modes, it is copied to the output |
| 5362 | stream as-is (giving a ZWNBSP character). */ |
| 5363 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5364 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5365 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5366 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | if (bom == 0xFEFF) { |
| 5368 | q += 2; |
| 5369 | bo = -1; |
| 5370 | } |
| 5371 | else if (bom == 0xFFFE) { |
| 5372 | q += 2; |
| 5373 | bo = 1; |
| 5374 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5375 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | if (bom == 0xFEFF) { |
| 5377 | q += 2; |
| 5378 | bo = 1; |
| 5379 | } |
| 5380 | else if (bom == 0xFFFE) { |
| 5381 | q += 2; |
| 5382 | bo = -1; |
| 5383 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5384 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5385 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5386 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5388 | if (bo == -1) { |
| 5389 | /* force LE */ |
| 5390 | ihi = 1; |
| 5391 | ilo = 0; |
| 5392 | } |
| 5393 | else if (bo == 1) { |
| 5394 | /* force BE */ |
| 5395 | ihi = 0; |
| 5396 | ilo = 1; |
| 5397 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5398 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5399 | native_ordering = ilo < ihi; |
| 5400 | #else |
| 5401 | native_ordering = ilo > ihi; |
| 5402 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5403 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5404 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5405 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5406 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5407 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5408 | reads are more expensive, better to defer to another iteration. */ |
| 5409 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5410 | /* Fast path for runs of non-surrogate chars. */ |
| 5411 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5412 | int kind = PyUnicode_KIND(unicode); |
| 5413 | void *data = PyUnicode_DATA(unicode); |
| 5414 | while (_q < aligned_end) { |
| 5415 | unsigned long block = * (unsigned long *) _q; |
| 5416 | unsigned short *pblock = (unsigned short*)█ |
| 5417 | Py_UCS4 maxch; |
| 5418 | if (native_ordering) { |
| 5419 | /* Can use buffer directly */ |
| 5420 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5421 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5422 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5423 | else { |
| 5424 | /* Need to byte-swap */ |
| 5425 | unsigned char *_p = (unsigned char*)pblock; |
| 5426 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5427 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5428 | _p[0] = _q[1]; |
| 5429 | _p[1] = _q[0]; |
| 5430 | _p[2] = _q[3]; |
| 5431 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5432 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5433 | _p[4] = _q[5]; |
| 5434 | _p[5] = _q[4]; |
| 5435 | _p[6] = _q[7]; |
| 5436 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5437 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5438 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5439 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5440 | #if SIZEOF_LONG == 8 |
| 5441 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5442 | #endif |
| 5443 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5444 | if (unicode_widen(&unicode, maxch) < 0) |
| 5445 | goto onError; |
| 5446 | kind = PyUnicode_KIND(unicode); |
| 5447 | data = PyUnicode_DATA(unicode); |
| 5448 | } |
| 5449 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5450 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5451 | #if SIZEOF_LONG == 8 |
| 5452 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5453 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5454 | #endif |
| 5455 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5456 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5457 | q = _q; |
| 5458 | if (q >= e) |
| 5459 | break; |
| 5460 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5462 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5463 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5464 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5465 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5466 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5467 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5468 | continue; |
| 5469 | } |
| 5470 | |
| 5471 | /* UTF-16 code pair: */ |
| 5472 | if (q > e) { |
| 5473 | errmsg = "unexpected end of data"; |
| 5474 | startinpos = (((const char *)q) - 2) - starts; |
| 5475 | endinpos = ((const char *)e) + 1 - starts; |
| 5476 | goto utf16Error; |
| 5477 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5478 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5479 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5480 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5481 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5482 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5483 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5484 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | continue; |
| 5486 | } |
| 5487 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5488 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5489 | startinpos = (((const char *)q)-4)-starts; |
| 5490 | endinpos = startinpos+2; |
| 5491 | goto utf16Error; |
| 5492 | } |
| 5493 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5494 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5495 | errmsg = "illegal encoding"; |
| 5496 | startinpos = (((const char *)q)-2)-starts; |
| 5497 | endinpos = startinpos+2; |
| 5498 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5499 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5500 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5502 | errors, |
| 5503 | &errorHandler, |
| 5504 | "utf16", errmsg, |
| 5505 | &starts, |
| 5506 | (const char **)&e, |
| 5507 | &startinpos, |
| 5508 | &endinpos, |
| 5509 | &exc, |
| 5510 | (const char **)&q, |
| 5511 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5512 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5513 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5515 | /* remaining byte at the end? (size should be even) */ |
| 5516 | if (e == q) { |
| 5517 | if (!consumed) { |
| 5518 | errmsg = "truncated data"; |
| 5519 | startinpos = ((const char *)q) - starts; |
| 5520 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5521 | if (unicode_decode_call_errorhandler( |
| 5522 | errors, |
| 5523 | &errorHandler, |
| 5524 | "utf16", errmsg, |
| 5525 | &starts, |
| 5526 | (const char **)&e, |
| 5527 | &startinpos, |
| 5528 | &endinpos, |
| 5529 | &exc, |
| 5530 | (const char **)&q, |
| 5531 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5532 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5533 | goto onError; |
| 5534 | /* The remaining input chars are ignored if the callback |
| 5535 | chooses to skip the input */ |
| 5536 | } |
| 5537 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | |
| 5539 | if (byteorder) |
| 5540 | *byteorder = bo; |
| 5541 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5542 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5543 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5546 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | goto onError; |
| 5548 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5549 | Py_XDECREF(errorHandler); |
| 5550 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5551 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5553 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5555 | Py_XDECREF(errorHandler); |
| 5556 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | return NULL; |
| 5558 | } |
| 5559 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5560 | #undef FAST_CHAR_MASK |
| 5561 | #undef SWAPPED_FAST_CHAR_MASK |
| 5562 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5563 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5564 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5565 | const char *errors, |
| 5566 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5568 | int kind; |
| 5569 | void *data; |
| 5570 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5571 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5572 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5573 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5574 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5575 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5576 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5577 | int ihi = 1, ilo = 0; |
| 5578 | #else |
| 5579 | int ihi = 0, ilo = 1; |
| 5580 | #endif |
| 5581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5582 | #define STORECHAR(CH) \ |
| 5583 | do { \ |
| 5584 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5585 | p[ilo] = (CH) & 0xff; \ |
| 5586 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5587 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5589 | if (!PyUnicode_Check(str)) { |
| 5590 | PyErr_BadArgument(); |
| 5591 | return NULL; |
| 5592 | } |
| 5593 | if (PyUnicode_READY(str) < 0) |
| 5594 | return NULL; |
| 5595 | kind = PyUnicode_KIND(str); |
| 5596 | data = PyUnicode_DATA(str); |
| 5597 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5598 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5599 | pairs = 0; |
| 5600 | if (kind == PyUnicode_4BYTE_KIND) |
| 5601 | for (i = 0; i < len; i++) |
| 5602 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5603 | pairs++; |
| 5604 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5605 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5606 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5607 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5608 | bytesize = nsize * 2; |
| 5609 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5610 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5611 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | if (v == NULL) |
| 5613 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5615 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5618 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5619 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5620 | |
| 5621 | if (byteorder == -1) { |
| 5622 | /* force LE */ |
| 5623 | ihi = 1; |
| 5624 | ilo = 0; |
| 5625 | } |
| 5626 | else if (byteorder == 1) { |
| 5627 | /* force BE */ |
| 5628 | ihi = 0; |
| 5629 | ilo = 1; |
| 5630 | } |
| 5631 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5632 | for (i = 0; i < len; i++) { |
| 5633 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5634 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5635 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5636 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5637 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5638 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5639 | STORECHAR(ch); |
| 5640 | if (ch2) |
| 5641 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5642 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5643 | |
| 5644 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5645 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5646 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5649 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5650 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5651 | Py_ssize_t size, |
| 5652 | const char *errors, |
| 5653 | int byteorder) |
| 5654 | { |
| 5655 | PyObject *result; |
| 5656 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5657 | if (tmp == NULL) |
| 5658 | return NULL; |
| 5659 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5660 | Py_DECREF(tmp); |
| 5661 | return result; |
| 5662 | } |
| 5663 | |
| 5664 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5665 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5667 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
| 5670 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5672 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5673 | if all the escapes in the string make it still a valid ASCII string. |
| 5674 | Returns -1 if any escapes were found which cause the string to |
| 5675 | pop out of ASCII range. Otherwise returns the length of the |
| 5676 | required buffer to hold the string. |
| 5677 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5678 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5679 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5680 | { |
| 5681 | const unsigned char *p = (const unsigned char *)s; |
| 5682 | const unsigned char *end = p + size; |
| 5683 | Py_ssize_t length = 0; |
| 5684 | |
| 5685 | if (size < 0) |
| 5686 | return -1; |
| 5687 | |
| 5688 | for (; p < end; ++p) { |
| 5689 | if (*p > 127) { |
| 5690 | /* Non-ASCII */ |
| 5691 | return -1; |
| 5692 | } |
| 5693 | else if (*p != '\\') { |
| 5694 | /* Normal character */ |
| 5695 | ++length; |
| 5696 | } |
| 5697 | else { |
| 5698 | /* Backslash-escape, check next char */ |
| 5699 | ++p; |
| 5700 | /* Escape sequence reaches till end of string or |
| 5701 | non-ASCII follow-up. */ |
| 5702 | if (p >= end || *p > 127) |
| 5703 | return -1; |
| 5704 | switch (*p) { |
| 5705 | case '\n': |
| 5706 | /* backslash + \n result in zero characters */ |
| 5707 | break; |
| 5708 | case '\\': case '\'': case '\"': |
| 5709 | case 'b': case 'f': case 't': |
| 5710 | case 'n': case 'r': case 'v': case 'a': |
| 5711 | ++length; |
| 5712 | break; |
| 5713 | case '0': case '1': case '2': case '3': |
| 5714 | case '4': case '5': case '6': case '7': |
| 5715 | case 'x': case 'u': case 'U': case 'N': |
| 5716 | /* these do not guarantee ASCII characters */ |
| 5717 | return -1; |
| 5718 | default: |
| 5719 | /* count the backslash + the other character */ |
| 5720 | length += 2; |
| 5721 | } |
| 5722 | } |
| 5723 | } |
| 5724 | return length; |
| 5725 | } |
| 5726 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5727 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5728 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5729 | PyObject * |
| 5730 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5731 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5732 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5734 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5735 | Py_ssize_t startinpos; |
| 5736 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5737 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5738 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5740 | char* message; |
| 5741 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | PyObject *errorHandler = NULL; |
| 5743 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5744 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5745 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5746 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5747 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5748 | |
| 5749 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5750 | either the string is pure ASCII with named escapes like \n, etc. |
| 5751 | and we determined it's exact size (common case) |
| 5752 | or it contains \x, \u, ... escape sequences. then we create a |
| 5753 | 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] | 5754 | if (len >= 0) { |
| 5755 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5756 | if (!v) |
| 5757 | goto onError; |
| 5758 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5759 | } |
| 5760 | else { |
| 5761 | /* Escaped strings will always be longer than the resulting |
| 5762 | Unicode string, so we start with size here and then reduce the |
| 5763 | length after conversion to the true value. |
| 5764 | (but if the error callback returns a long replacement string |
| 5765 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5766 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | if (!v) |
| 5768 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5769 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5770 | } |
| 5771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5773 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5774 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | while (s < end) { |
| 5778 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5779 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5781 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5782 | /* The only case in which i == ascii_length is a backslash |
| 5783 | followed by a newline. */ |
| 5784 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5787 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5788 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5789 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | continue; |
| 5791 | } |
| 5792 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5793 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | /* \ - Escapes */ |
| 5795 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5796 | c = *s++; |
| 5797 | if (s > end) |
| 5798 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5799 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5800 | /* The only case in which i == ascii_length is a backslash |
| 5801 | followed by a newline. */ |
| 5802 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5803 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5804 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5805 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5807 | #define WRITECHAR(ch) \ |
| 5808 | do { \ |
| 5809 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5810 | goto onError; \ |
| 5811 | }while(0) |
| 5812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5814 | case '\\': WRITECHAR('\\'); break; |
| 5815 | case '\'': WRITECHAR('\''); break; |
| 5816 | case '\"': WRITECHAR('\"'); break; |
| 5817 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5818 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5819 | case 'f': WRITECHAR('\014'); break; |
| 5820 | case 't': WRITECHAR('\t'); break; |
| 5821 | case 'n': WRITECHAR('\n'); break; |
| 5822 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5823 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5824 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5825 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5826 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 | case '0': case '1': case '2': case '3': |
| 5830 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5831 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5832 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5833 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5834 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5835 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5837 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | break; |
| 5839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5840 | /* hex escapes */ |
| 5841 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5842 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5843 | digits = 2; |
| 5844 | message = "truncated \\xXX escape"; |
| 5845 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5847 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5849 | digits = 4; |
| 5850 | message = "truncated \\uXXXX escape"; |
| 5851 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5853 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5854 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5855 | digits = 8; |
| 5856 | message = "truncated \\UXXXXXXXX escape"; |
| 5857 | hexescape: |
| 5858 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5859 | if (s+digits>end) { |
| 5860 | endinpos = size; |
| 5861 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5862 | errors, &errorHandler, |
| 5863 | "unicodeescape", "end of string in escape sequence", |
| 5864 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5865 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5866 | goto onError; |
| 5867 | goto nextByte; |
| 5868 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5869 | for (j = 0; j < digits; ++j) { |
| 5870 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5871 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5872 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5873 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5874 | errors, &errorHandler, |
| 5875 | "unicodeescape", message, |
| 5876 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5877 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5878 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5879 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5880 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5881 | } |
| 5882 | chr = (chr<<4) & ~0xF; |
| 5883 | if (c >= '0' && c <= '9') |
| 5884 | chr += c - '0'; |
| 5885 | else if (c >= 'a' && c <= 'f') |
| 5886 | chr += 10 + c - 'a'; |
| 5887 | else |
| 5888 | chr += 10 + c - 'A'; |
| 5889 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5890 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5891 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5892 | /* _decoding_error will have already written into the |
| 5893 | target buffer. */ |
| 5894 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5895 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5896 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5897 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5898 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5899 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5900 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5901 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5902 | errors, &errorHandler, |
| 5903 | "unicodeescape", "illegal Unicode character", |
| 5904 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5905 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5906 | goto onError; |
| 5907 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5908 | break; |
| 5909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5911 | case 'N': |
| 5912 | message = "malformed \\N character escape"; |
| 5913 | if (ucnhash_CAPI == NULL) { |
| 5914 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5915 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5916 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5917 | if (ucnhash_CAPI == NULL) |
| 5918 | goto ucnhashError; |
| 5919 | } |
| 5920 | if (*s == '{') { |
| 5921 | const char *start = s+1; |
| 5922 | /* look for the closing brace */ |
| 5923 | while (*s != '}' && s < end) |
| 5924 | s++; |
| 5925 | if (s > start && s < end && *s == '}') { |
| 5926 | /* found a name. look it up in the unicode database */ |
| 5927 | message = "unknown Unicode character name"; |
| 5928 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5929 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5930 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5931 | goto store; |
| 5932 | } |
| 5933 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5934 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5935 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | errors, &errorHandler, |
| 5937 | "unicodeescape", message, |
| 5938 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5939 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5940 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5941 | break; |
| 5942 | |
| 5943 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5944 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5945 | message = "\\ at end of string"; |
| 5946 | s--; |
| 5947 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5948 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | errors, &errorHandler, |
| 5950 | "unicodeescape", message, |
| 5951 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5952 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5953 | goto onError; |
| 5954 | } |
| 5955 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5956 | WRITECHAR('\\'); |
| 5957 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5958 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5959 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5961 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5962 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5964 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5965 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5966 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5967 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5968 | Py_XDECREF(errorHandler); |
| 5969 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5970 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5972 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5973 | PyErr_SetString( |
| 5974 | PyExc_UnicodeError, |
| 5975 | "\\N escapes not supported (can't load unicodedata module)" |
| 5976 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5977 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5978 | Py_XDECREF(errorHandler); |
| 5979 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5980 | return NULL; |
| 5981 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5982 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5984 | Py_XDECREF(errorHandler); |
| 5985 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5986 | return NULL; |
| 5987 | } |
| 5988 | |
| 5989 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5990 | |
| 5991 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5992 | appropriate. |
| 5993 | |
| 5994 | */ |
| 5995 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5996 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5997 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5998 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5999 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6000 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6002 | int kind; |
| 6003 | void *data; |
| 6004 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6006 | /* Initial allocation is based on the longest-possible unichr |
| 6007 | escape. |
| 6008 | |
| 6009 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6010 | unichr, so in this case it's the longest unichr escape. In |
| 6011 | narrow (UTF-16) builds this is five chars per source unichr |
| 6012 | since there are two unichrs in the surrogate pair, so in narrow |
| 6013 | (UTF-16) builds it's not the longest unichr escape. |
| 6014 | |
| 6015 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6016 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6017 | escape. |
| 6018 | */ |
| 6019 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6020 | if (!PyUnicode_Check(unicode)) { |
| 6021 | PyErr_BadArgument(); |
| 6022 | return NULL; |
| 6023 | } |
| 6024 | if (PyUnicode_READY(unicode) < 0) |
| 6025 | return NULL; |
| 6026 | len = PyUnicode_GET_LENGTH(unicode); |
| 6027 | kind = PyUnicode_KIND(unicode); |
| 6028 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6029 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6030 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6031 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6032 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6033 | } |
| 6034 | |
| 6035 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6036 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6037 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6038 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6039 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6040 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6041 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6043 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6044 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | if (repr == NULL) |
| 6046 | return NULL; |
| 6047 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6048 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6050 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6051 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6052 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6053 | /* Escape backslashes */ |
| 6054 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | *p++ = '\\'; |
| 6056 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6057 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6058 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6059 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6060 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6061 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6062 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6063 | *p++ = '\\'; |
| 6064 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6065 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6066 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6067 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6068 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6069 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6070 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6071 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6072 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6074 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6077 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | *p++ = '\\'; |
| 6079 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6080 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6081 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6082 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6083 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6085 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6086 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6087 | else if (ch == '\t') { |
| 6088 | *p++ = '\\'; |
| 6089 | *p++ = 't'; |
| 6090 | } |
| 6091 | else if (ch == '\n') { |
| 6092 | *p++ = '\\'; |
| 6093 | *p++ = 'n'; |
| 6094 | } |
| 6095 | else if (ch == '\r') { |
| 6096 | *p++ = '\\'; |
| 6097 | *p++ = 'r'; |
| 6098 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6099 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6100 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6101 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6103 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6104 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6105 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6106 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | /* Copy everything else as-is */ |
| 6109 | else |
| 6110 | *p++ = (char) ch; |
| 6111 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6113 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6114 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6115 | return NULL; |
| 6116 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6119 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6120 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6121 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6123 | PyObject *result; |
| 6124 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6125 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6127 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6128 | Py_DECREF(tmp); |
| 6129 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6133 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6134 | PyObject * |
| 6135 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6136 | Py_ssize_t size, |
| 6137 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6139 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6140 | Py_ssize_t startinpos; |
| 6141 | Py_ssize_t endinpos; |
| 6142 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6143 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | const char *end; |
| 6145 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6146 | PyObject *errorHandler = NULL; |
| 6147 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | /* Escaped strings will always be longer than the resulting |
| 6150 | 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] | 6151 | length after conversion to the true value. (But decoding error |
| 6152 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6153 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6157 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6158 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | end = s + size; |
| 6160 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6161 | unsigned char c; |
| 6162 | Py_UCS4 x; |
| 6163 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6164 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6166 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6167 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6168 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6169 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6170 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6171 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | startinpos = s-starts; |
| 6173 | |
| 6174 | /* \u-escapes are only interpreted iff the number of leading |
| 6175 | backslashes if odd */ |
| 6176 | bs = s; |
| 6177 | for (;s < end;) { |
| 6178 | if (*s != '\\') |
| 6179 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6180 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6181 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6182 | } |
| 6183 | if (((s - bs) & 1) == 0 || |
| 6184 | s >= end || |
| 6185 | (*s != 'u' && *s != 'U')) { |
| 6186 | continue; |
| 6187 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6188 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | count = *s=='u' ? 4 : 8; |
| 6190 | s++; |
| 6191 | |
| 6192 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6193 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6194 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6195 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6196 | endinpos = s-starts; |
| 6197 | if (unicode_decode_call_errorhandler( |
| 6198 | errors, &errorHandler, |
| 6199 | "rawunicodeescape", "truncated \\uXXXX", |
| 6200 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6201 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | goto onError; |
| 6203 | goto nextByte; |
| 6204 | } |
| 6205 | x = (x<<4) & ~0xF; |
| 6206 | if (c >= '0' && c <= '9') |
| 6207 | x += c - '0'; |
| 6208 | else if (c >= 'a' && c <= 'f') |
| 6209 | x += 10 + c - 'a'; |
| 6210 | else |
| 6211 | x += 10 + c - 'A'; |
| 6212 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6213 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6214 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6215 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6216 | } else { |
| 6217 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6218 | if (unicode_decode_call_errorhandler( |
| 6219 | errors, &errorHandler, |
| 6220 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6222 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6223 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6224 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | nextByte: |
| 6226 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6228 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6230 | Py_XDECREF(errorHandler); |
| 6231 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6232 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6234 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6236 | Py_XDECREF(errorHandler); |
| 6237 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | return NULL; |
| 6239 | } |
| 6240 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6241 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6242 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6243 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6245 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 | char *p; |
| 6247 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6248 | Py_ssize_t expandsize, pos; |
| 6249 | int kind; |
| 6250 | void *data; |
| 6251 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6253 | if (!PyUnicode_Check(unicode)) { |
| 6254 | PyErr_BadArgument(); |
| 6255 | return NULL; |
| 6256 | } |
| 6257 | if (PyUnicode_READY(unicode) < 0) |
| 6258 | return NULL; |
| 6259 | kind = PyUnicode_KIND(unicode); |
| 6260 | data = PyUnicode_DATA(unicode); |
| 6261 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6262 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6263 | bytes, and 1 byte characters 4. */ |
| 6264 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6265 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6266 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6268 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6269 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | if (repr == NULL) |
| 6271 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6272 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6273 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6275 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6276 | for (pos = 0; pos < len; pos++) { |
| 6277 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6279 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6280 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6281 | *p++ = '\\'; |
| 6282 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6283 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6284 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6285 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6286 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6287 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6288 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6289 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6290 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6291 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6293 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | *p++ = '\\'; |
| 6295 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6296 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6297 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6298 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6299 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6301 | /* Copy everything else as-is */ |
| 6302 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | *p++ = (char) ch; |
| 6304 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6305 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6306 | assert(p > q); |
| 6307 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6308 | return NULL; |
| 6309 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6312 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6313 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6314 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6315 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6316 | PyObject *result; |
| 6317 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6318 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6319 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6320 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6321 | Py_DECREF(tmp); |
| 6322 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6325 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6327 | PyObject * |
| 6328 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6329 | Py_ssize_t size, |
| 6330 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6331 | { |
| 6332 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6333 | Py_ssize_t startinpos; |
| 6334 | Py_ssize_t endinpos; |
| 6335 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6336 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6337 | const char *end; |
| 6338 | const char *reason; |
| 6339 | PyObject *errorHandler = NULL; |
| 6340 | PyObject *exc = NULL; |
| 6341 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6342 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6343 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6344 | 1)) |
| 6345 | return NULL; |
| 6346 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6347 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6348 | 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] | 6349 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6350 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6351 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6352 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6353 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6354 | end = s + size; |
| 6355 | |
| 6356 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6357 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6358 | Py_UCS4 ch; |
| 6359 | /* We copy the raw representation one byte at a time because the |
| 6360 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6361 | ((char *) &uch)[0] = s[0]; |
| 6362 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6363 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6364 | ((char *) &uch)[2] = s[2]; |
| 6365 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6366 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6367 | ch = uch; |
| 6368 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6369 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6370 | some malformed UCS-4 data. */ |
| 6371 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6373 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6375 | end-s < Py_UNICODE_SIZE |
| 6376 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6378 | startinpos = s - starts; |
| 6379 | if (end-s < Py_UNICODE_SIZE) { |
| 6380 | endinpos = end-starts; |
| 6381 | reason = "truncated input"; |
| 6382 | } |
| 6383 | else { |
| 6384 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6385 | reason = "illegal code point (> 0x10FFFF)"; |
| 6386 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6387 | if (unicode_decode_call_errorhandler( |
| 6388 | errors, &errorHandler, |
| 6389 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6390 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6391 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6392 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6393 | continue; |
| 6394 | } |
| 6395 | |
| 6396 | s += Py_UNICODE_SIZE; |
| 6397 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6398 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6399 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6400 | Py_UNICODE uch2; |
| 6401 | ((char *) &uch2)[0] = s[0]; |
| 6402 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6403 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6404 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6405 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6406 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6407 | } |
| 6408 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6409 | #endif |
| 6410 | |
| 6411 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6412 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6415 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6416 | goto onError; |
| 6417 | Py_XDECREF(errorHandler); |
| 6418 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6419 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6422 | Py_XDECREF(v); |
| 6423 | Py_XDECREF(errorHandler); |
| 6424 | Py_XDECREF(exc); |
| 6425 | return NULL; |
| 6426 | } |
| 6427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6429 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6430 | PyObject * |
| 6431 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6432 | Py_ssize_t size, |
| 6433 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6436 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6439 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6440 | static void |
| 6441 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6442 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6443 | PyObject *unicode, |
| 6444 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6445 | const char *reason) |
| 6446 | { |
| 6447 | if (*exceptionObject == NULL) { |
| 6448 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6449 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6450 | encoding, unicode, startpos, endpos, reason); |
| 6451 | } |
| 6452 | else { |
| 6453 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6454 | goto onError; |
| 6455 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6456 | goto onError; |
| 6457 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6458 | goto onError; |
| 6459 | return; |
| 6460 | onError: |
| 6461 | Py_DECREF(*exceptionObject); |
| 6462 | *exceptionObject = NULL; |
| 6463 | } |
| 6464 | } |
| 6465 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6466 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6467 | static void |
| 6468 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6469 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6470 | PyObject *unicode, |
| 6471 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6472 | const char *reason) |
| 6473 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6474 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6475 | encoding, unicode, startpos, endpos, reason); |
| 6476 | if (*exceptionObject != NULL) |
| 6477 | PyCodec_StrictErrors(*exceptionObject); |
| 6478 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | |
| 6480 | /* error handling callback helper: |
| 6481 | build arguments, call the callback and check the arguments, |
| 6482 | put the result into newpos and return the replacement string, which |
| 6483 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6484 | static PyObject * |
| 6485 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6486 | PyObject **errorHandler, |
| 6487 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6488 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6489 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6490 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6492 | 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] | 6493 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6494 | PyObject *restuple; |
| 6495 | PyObject *resunicode; |
| 6496 | |
| 6497 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6498 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6499 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6500 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6501 | } |
| 6502 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6503 | if (PyUnicode_READY(unicode) < 0) |
| 6504 | return NULL; |
| 6505 | len = PyUnicode_GET_LENGTH(unicode); |
| 6506 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6507 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6508 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6509 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6511 | |
| 6512 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6513 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6514 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6516 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6517 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6518 | Py_DECREF(restuple); |
| 6519 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6520 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6521 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | &resunicode, newpos)) { |
| 6523 | Py_DECREF(restuple); |
| 6524 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6525 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6526 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6527 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6528 | Py_DECREF(restuple); |
| 6529 | return NULL; |
| 6530 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6531 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6532 | *newpos = len + *newpos; |
| 6533 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6535 | Py_DECREF(restuple); |
| 6536 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6537 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6538 | Py_INCREF(resunicode); |
| 6539 | Py_DECREF(restuple); |
| 6540 | return resunicode; |
| 6541 | } |
| 6542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6543 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6544 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6545 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6546 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6547 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6548 | /* input state */ |
| 6549 | Py_ssize_t pos=0, size; |
| 6550 | int kind; |
| 6551 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6552 | /* output object */ |
| 6553 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6554 | /* pointer into the output */ |
| 6555 | char *str; |
| 6556 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6557 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6558 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6559 | 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] | 6560 | PyObject *errorHandler = NULL; |
| 6561 | PyObject *exc = NULL; |
| 6562 | /* the following variable is used for caching string comparisons |
| 6563 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6564 | int known_errorHandler = -1; |
| 6565 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6566 | if (PyUnicode_READY(unicode) < 0) |
| 6567 | return NULL; |
| 6568 | size = PyUnicode_GET_LENGTH(unicode); |
| 6569 | kind = PyUnicode_KIND(unicode); |
| 6570 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6571 | /* allocate enough for a simple encoding without |
| 6572 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6573 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6574 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6575 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6576 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6577 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6578 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6579 | ressize = size; |
| 6580 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6581 | while (pos < size) { |
| 6582 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6584 | /* can we encode this? */ |
| 6585 | if (c<limit) { |
| 6586 | /* no overflow check, because we know that the space is enough */ |
| 6587 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6588 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6589 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | Py_ssize_t requiredsize; |
| 6592 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6593 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6594 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6595 | Py_ssize_t collstart = pos; |
| 6596 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6597 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6598 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6599 | ++collend; |
| 6600 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6601 | if (known_errorHandler==-1) { |
| 6602 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6603 | known_errorHandler = 1; |
| 6604 | else if (!strcmp(errors, "replace")) |
| 6605 | known_errorHandler = 2; |
| 6606 | else if (!strcmp(errors, "ignore")) |
| 6607 | known_errorHandler = 3; |
| 6608 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6609 | known_errorHandler = 4; |
| 6610 | else |
| 6611 | known_errorHandler = 0; |
| 6612 | } |
| 6613 | switch (known_errorHandler) { |
| 6614 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6615 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | goto onError; |
| 6617 | case 2: /* replace */ |
| 6618 | while (collstart++<collend) |
| 6619 | *str++ = '?'; /* fall through */ |
| 6620 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6621 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6622 | break; |
| 6623 | case 4: /* xmlcharrefreplace */ |
| 6624 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6625 | /* determine replacement size */ |
| 6626 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6627 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6628 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6630 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6631 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6632 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6634 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6635 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6636 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6637 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6638 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6639 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6640 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6641 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6642 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6643 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6644 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6645 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6646 | if (requiredsize > ressize) { |
| 6647 | if (requiredsize<2*ressize) |
| 6648 | requiredsize = 2*ressize; |
| 6649 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6650 | goto onError; |
| 6651 | str = PyBytes_AS_STRING(res) + respos; |
| 6652 | ressize = requiredsize; |
| 6653 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6654 | /* generate replacement */ |
| 6655 | for (i = collstart; i < collend; ++i) { |
| 6656 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6657 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6658 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6659 | break; |
| 6660 | default: |
| 6661 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6662 | encoding, reason, unicode, &exc, |
| 6663 | collstart, collend, &newpos); |
| 6664 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6665 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6667 | if (PyBytes_Check(repunicode)) { |
| 6668 | /* Directly copy bytes result to output. */ |
| 6669 | repsize = PyBytes_Size(repunicode); |
| 6670 | if (repsize > 1) { |
| 6671 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6672 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6673 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6674 | Py_DECREF(repunicode); |
| 6675 | goto onError; |
| 6676 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6677 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6678 | ressize += repsize-1; |
| 6679 | } |
| 6680 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6681 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6682 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6683 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6684 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6685 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6686 | /* need more space? (at least enough for what we |
| 6687 | have+the replacement+the rest of the string, so |
| 6688 | we won't have to check space for encodable characters) */ |
| 6689 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6690 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6691 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6692 | if (requiredsize > ressize) { |
| 6693 | if (requiredsize<2*ressize) |
| 6694 | requiredsize = 2*ressize; |
| 6695 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6696 | Py_DECREF(repunicode); |
| 6697 | goto onError; |
| 6698 | } |
| 6699 | str = PyBytes_AS_STRING(res) + respos; |
| 6700 | ressize = requiredsize; |
| 6701 | } |
| 6702 | /* check if there is anything unencodable in the replacement |
| 6703 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6704 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6705 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6706 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6707 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6708 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | Py_DECREF(repunicode); |
| 6710 | goto onError; |
| 6711 | } |
| 6712 | *str = (char)c; |
| 6713 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6714 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6715 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6716 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6717 | } |
| 6718 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6719 | /* Resize if we allocated to much */ |
| 6720 | size = str - PyBytes_AS_STRING(res); |
| 6721 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6722 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6723 | if (_PyBytes_Resize(&res, size) < 0) |
| 6724 | goto onError; |
| 6725 | } |
| 6726 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6727 | Py_XDECREF(errorHandler); |
| 6728 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6729 | return res; |
| 6730 | |
| 6731 | onError: |
| 6732 | Py_XDECREF(res); |
| 6733 | Py_XDECREF(errorHandler); |
| 6734 | Py_XDECREF(exc); |
| 6735 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6738 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6739 | PyObject * |
| 6740 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6741 | Py_ssize_t size, |
| 6742 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6744 | PyObject *result; |
| 6745 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6746 | if (unicode == NULL) |
| 6747 | return NULL; |
| 6748 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6749 | Py_DECREF(unicode); |
| 6750 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6753 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6754 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | { |
| 6756 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6757 | PyErr_BadArgument(); |
| 6758 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6760 | if (PyUnicode_READY(unicode) == -1) |
| 6761 | return NULL; |
| 6762 | /* Fast path: if it is a one-byte string, construct |
| 6763 | bytes object directly. */ |
| 6764 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6765 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6766 | PyUnicode_GET_LENGTH(unicode)); |
| 6767 | /* Non-Latin-1 characters present. Defer to above function to |
| 6768 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6769 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | PyObject* |
| 6773 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6774 | { |
| 6775 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | } |
| 6777 | |
| 6778 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6780 | PyObject * |
| 6781 | PyUnicode_DecodeASCII(const char *s, |
| 6782 | Py_ssize_t size, |
| 6783 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6785 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6786 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6787 | int kind; |
| 6788 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6789 | Py_ssize_t startinpos; |
| 6790 | Py_ssize_t endinpos; |
| 6791 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6792 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6793 | int has_error; |
| 6794 | const unsigned char *p = (const unsigned char *)s; |
| 6795 | const unsigned char *end = p + size; |
| 6796 | 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] | 6797 | PyObject *errorHandler = NULL; |
| 6798 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6799 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6800 | if (size == 0) { |
| 6801 | Py_INCREF(unicode_empty); |
| 6802 | return unicode_empty; |
| 6803 | } |
| 6804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6806 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6807 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6808 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6809 | has_error = 0; |
| 6810 | while (p < end && !has_error) { |
| 6811 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6812 | an explanation. */ |
| 6813 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6814 | /* Help register allocation */ |
| 6815 | register const unsigned char *_p = p; |
| 6816 | while (_p < aligned_end) { |
| 6817 | unsigned long value = *(unsigned long *) _p; |
| 6818 | if (value & ASCII_CHAR_MASK) { |
| 6819 | has_error = 1; |
| 6820 | break; |
| 6821 | } |
| 6822 | _p += SIZEOF_LONG; |
| 6823 | } |
| 6824 | if (_p == end) |
| 6825 | break; |
| 6826 | if (has_error) |
| 6827 | break; |
| 6828 | p = _p; |
| 6829 | } |
| 6830 | if (*p & 0x80) { |
| 6831 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6832 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6833 | } |
| 6834 | else { |
| 6835 | ++p; |
| 6836 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6837 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6838 | if (!has_error) |
| 6839 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6840 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6841 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6843 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6845 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6846 | kind = PyUnicode_KIND(v); |
| 6847 | data = PyUnicode_DATA(v); |
| 6848 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6849 | e = s + size; |
| 6850 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6851 | register unsigned char c = (unsigned char)*s; |
| 6852 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6853 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6854 | ++s; |
| 6855 | } |
| 6856 | else { |
| 6857 | startinpos = s-starts; |
| 6858 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6859 | if (unicode_decode_call_errorhandler( |
| 6860 | errors, &errorHandler, |
| 6861 | "ascii", "ordinal not in range(128)", |
| 6862 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6863 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6864 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6865 | kind = PyUnicode_KIND(v); |
| 6866 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6867 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6869 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6870 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6871 | Py_XDECREF(errorHandler); |
| 6872 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6873 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6874 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6875 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6877 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6878 | Py_XDECREF(errorHandler); |
| 6879 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | return NULL; |
| 6881 | } |
| 6882 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6883 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6884 | PyObject * |
| 6885 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6886 | Py_ssize_t size, |
| 6887 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6889 | PyObject *result; |
| 6890 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6891 | if (unicode == NULL) |
| 6892 | return NULL; |
| 6893 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6894 | Py_DECREF(unicode); |
| 6895 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6898 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6899 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | { |
| 6901 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6902 | PyErr_BadArgument(); |
| 6903 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6905 | if (PyUnicode_READY(unicode) == -1) |
| 6906 | return NULL; |
| 6907 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6908 | directly. Else defer to above function to raise the exception. */ |
| 6909 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6910 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6911 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6912 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6913 | } |
| 6914 | |
| 6915 | PyObject * |
| 6916 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6917 | { |
| 6918 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6921 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6922 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6923 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6924 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6925 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6926 | #define NEED_RETRY |
| 6927 | #endif |
| 6928 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6929 | #ifndef WC_ERR_INVALID_CHARS |
| 6930 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6931 | #endif |
| 6932 | |
| 6933 | static char* |
| 6934 | code_page_name(UINT code_page, PyObject **obj) |
| 6935 | { |
| 6936 | *obj = NULL; |
| 6937 | if (code_page == CP_ACP) |
| 6938 | return "mbcs"; |
| 6939 | if (code_page == CP_UTF7) |
| 6940 | return "CP_UTF7"; |
| 6941 | if (code_page == CP_UTF8) |
| 6942 | return "CP_UTF8"; |
| 6943 | |
| 6944 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6945 | if (*obj == NULL) |
| 6946 | return NULL; |
| 6947 | return PyBytes_AS_STRING(*obj); |
| 6948 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6950 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6951 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6952 | { |
| 6953 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6954 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6955 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6956 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6957 | return 0; |
| 6958 | |
| 6959 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6960 | if (prev == curr) |
| 6961 | return 1; |
| 6962 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6963 | as it assumes an incomplete character consists of a single |
| 6964 | byte. */ |
| 6965 | if (curr - prev == 2) |
| 6966 | return 1; |
| 6967 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6968 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | return 0; |
| 6970 | } |
| 6971 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6972 | static DWORD |
| 6973 | decode_code_page_flags(UINT code_page) |
| 6974 | { |
| 6975 | if (code_page == CP_UTF7) { |
| 6976 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6977 | return 0; |
| 6978 | } |
| 6979 | else |
| 6980 | return MB_ERR_INVALID_CHARS; |
| 6981 | } |
| 6982 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6983 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6985 | * mode. |
| 6986 | * |
| 6987 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6988 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6989 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6990 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6991 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6992 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6993 | const char *in, |
| 6994 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6995 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6996 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6997 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6999 | |
| 7000 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7001 | assert(insize > 0); |
| 7002 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7003 | if (outsize <= 0) |
| 7004 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7005 | |
| 7006 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7007 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7008 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7009 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7010 | if (*v == NULL) |
| 7011 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7012 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7013 | } |
| 7014 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7015 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7016 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7017 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7018 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7019 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7023 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7024 | if (outsize <= 0) |
| 7025 | goto error; |
| 7026 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7027 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7028 | error: |
| 7029 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7030 | return -2; |
| 7031 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7032 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7035 | /* |
| 7036 | * Decode a byte string from a code page into unicode object with an error |
| 7037 | * handler. |
| 7038 | * |
| 7039 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7040 | * UnicodeDecodeError exception and returns -1 on error. |
| 7041 | */ |
| 7042 | static int |
| 7043 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7044 | PyObject **v, |
| 7045 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | const char *errors) |
| 7047 | { |
| 7048 | const char *startin = in; |
| 7049 | const char *endin = in + size; |
| 7050 | const DWORD flags = decode_code_page_flags(code_page); |
| 7051 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7052 | 2000 English version of the message. */ |
| 7053 | const char *reason = "No mapping for the Unicode character exists " |
| 7054 | "in the target code page."; |
| 7055 | /* each step cannot decode more than 1 character, but a character can be |
| 7056 | represented as a surrogate pair */ |
| 7057 | wchar_t buffer[2], *startout, *out; |
| 7058 | int insize, outsize; |
| 7059 | PyObject *errorHandler = NULL; |
| 7060 | PyObject *exc = NULL; |
| 7061 | PyObject *encoding_obj = NULL; |
| 7062 | char *encoding; |
| 7063 | DWORD err; |
| 7064 | int ret = -1; |
| 7065 | |
| 7066 | assert(size > 0); |
| 7067 | |
| 7068 | encoding = code_page_name(code_page, &encoding_obj); |
| 7069 | if (encoding == NULL) |
| 7070 | return -1; |
| 7071 | |
| 7072 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7073 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7074 | UnicodeDecodeError. */ |
| 7075 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7076 | if (exc != NULL) { |
| 7077 | PyCodec_StrictErrors(exc); |
| 7078 | Py_CLEAR(exc); |
| 7079 | } |
| 7080 | goto error; |
| 7081 | } |
| 7082 | |
| 7083 | if (*v == NULL) { |
| 7084 | /* Create unicode object */ |
| 7085 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7086 | PyErr_NoMemory(); |
| 7087 | goto error; |
| 7088 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7089 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7090 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7091 | if (*v == NULL) |
| 7092 | goto error; |
| 7093 | startout = PyUnicode_AS_UNICODE(*v); |
| 7094 | } |
| 7095 | else { |
| 7096 | /* Extend unicode object */ |
| 7097 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7098 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7099 | PyErr_NoMemory(); |
| 7100 | goto error; |
| 7101 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7102 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7103 | goto error; |
| 7104 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7105 | } |
| 7106 | |
| 7107 | /* Decode the byte string character per character */ |
| 7108 | out = startout; |
| 7109 | while (in < endin) |
| 7110 | { |
| 7111 | /* Decode a character */ |
| 7112 | insize = 1; |
| 7113 | do |
| 7114 | { |
| 7115 | outsize = MultiByteToWideChar(code_page, flags, |
| 7116 | in, insize, |
| 7117 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7118 | if (outsize > 0) |
| 7119 | break; |
| 7120 | err = GetLastError(); |
| 7121 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7122 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7123 | { |
| 7124 | PyErr_SetFromWindowsErr(0); |
| 7125 | goto error; |
| 7126 | } |
| 7127 | insize++; |
| 7128 | } |
| 7129 | /* 4=maximum length of a UTF-8 sequence */ |
| 7130 | while (insize <= 4 && (in + insize) <= endin); |
| 7131 | |
| 7132 | if (outsize <= 0) { |
| 7133 | Py_ssize_t startinpos, endinpos, outpos; |
| 7134 | |
| 7135 | startinpos = in - startin; |
| 7136 | endinpos = startinpos + 1; |
| 7137 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7138 | if (unicode_decode_call_errorhandler( |
| 7139 | errors, &errorHandler, |
| 7140 | encoding, reason, |
| 7141 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7142 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7143 | { |
| 7144 | goto error; |
| 7145 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7146 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7147 | } |
| 7148 | else { |
| 7149 | in += insize; |
| 7150 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7151 | out += outsize; |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | /* write a NUL character at the end */ |
| 7156 | *out = 0; |
| 7157 | |
| 7158 | /* Extend unicode object */ |
| 7159 | outsize = out - startout; |
| 7160 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7161 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7162 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7163 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7164 | |
| 7165 | error: |
| 7166 | Py_XDECREF(encoding_obj); |
| 7167 | Py_XDECREF(errorHandler); |
| 7168 | Py_XDECREF(exc); |
| 7169 | return ret; |
| 7170 | } |
| 7171 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | static PyObject * |
| 7173 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7174 | const char *s, Py_ssize_t size, |
| 7175 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7176 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7177 | PyObject *v = NULL; |
| 7178 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7179 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | if (code_page < 0) { |
| 7181 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7182 | return NULL; |
| 7183 | } |
| 7184 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7185 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7186 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7187 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7188 | do |
| 7189 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7190 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7191 | if (size > INT_MAX) { |
| 7192 | chunk_size = INT_MAX; |
| 7193 | final = 0; |
| 7194 | done = 0; |
| 7195 | } |
| 7196 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7197 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7198 | { |
| 7199 | chunk_size = (int)size; |
| 7200 | final = (consumed == NULL); |
| 7201 | done = 1; |
| 7202 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7203 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7204 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7205 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7206 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7207 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7208 | if (chunk_size == 0 && done) { |
| 7209 | if (v != NULL) |
| 7210 | break; |
| 7211 | Py_INCREF(unicode_empty); |
| 7212 | return unicode_empty; |
| 7213 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7214 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7215 | |
| 7216 | converted = decode_code_page_strict(code_page, &v, |
| 7217 | s, chunk_size); |
| 7218 | if (converted == -2) |
| 7219 | converted = decode_code_page_errors(code_page, &v, |
| 7220 | s, chunk_size, |
| 7221 | errors); |
| 7222 | assert(converted != 0); |
| 7223 | |
| 7224 | if (converted < 0) { |
| 7225 | Py_XDECREF(v); |
| 7226 | return NULL; |
| 7227 | } |
| 7228 | |
| 7229 | if (consumed) |
| 7230 | *consumed += converted; |
| 7231 | |
| 7232 | s += converted; |
| 7233 | size -= converted; |
| 7234 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7235 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7236 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7239 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7240 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7241 | const char *s, |
| 7242 | Py_ssize_t size, |
| 7243 | const char *errors, |
| 7244 | Py_ssize_t *consumed) |
| 7245 | { |
| 7246 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7247 | } |
| 7248 | |
| 7249 | PyObject * |
| 7250 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7251 | Py_ssize_t size, |
| 7252 | const char *errors, |
| 7253 | Py_ssize_t *consumed) |
| 7254 | { |
| 7255 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7256 | } |
| 7257 | |
| 7258 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7259 | PyUnicode_DecodeMBCS(const char *s, |
| 7260 | Py_ssize_t size, |
| 7261 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7262 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7263 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7264 | } |
| 7265 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | static DWORD |
| 7267 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7268 | { |
| 7269 | if (code_page == CP_UTF8) { |
| 7270 | if (winver.dwMajorVersion >= 6) |
| 7271 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7272 | and later */ |
| 7273 | return WC_ERR_INVALID_CHARS; |
| 7274 | else |
| 7275 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7276 | return 0; |
| 7277 | } |
| 7278 | else if (code_page == CP_UTF7) { |
| 7279 | /* CP_UTF7 only supports flags=0 */ |
| 7280 | return 0; |
| 7281 | } |
| 7282 | else { |
| 7283 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7284 | return 0; |
| 7285 | else |
| 7286 | return WC_NO_BEST_FIT_CHARS; |
| 7287 | } |
| 7288 | } |
| 7289 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7290 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7292 | * mode. |
| 7293 | * |
| 7294 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7295 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7296 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7297 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7298 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7299 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7300 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7301 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7302 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7303 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7304 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7305 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7306 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7307 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7308 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7309 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7310 | /* Create a substring so that we can get the UTF-16 representation |
| 7311 | of just the slice under consideration. */ |
| 7312 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7313 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7314 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7315 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7317 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7318 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7319 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7320 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7321 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7322 | if (substring == NULL) |
| 7323 | return -1; |
| 7324 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7325 | if (p == NULL) { |
| 7326 | Py_DECREF(substring); |
| 7327 | return -1; |
| 7328 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7329 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7330 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7331 | outsize = WideCharToMultiByte(code_page, flags, |
| 7332 | p, size, |
| 7333 | NULL, 0, |
| 7334 | NULL, pusedDefaultChar); |
| 7335 | if (outsize <= 0) |
| 7336 | goto error; |
| 7337 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7338 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7339 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7340 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7341 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7342 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7345 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7346 | if (*outbytes == NULL) { |
| 7347 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7348 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7349 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7350 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7351 | } |
| 7352 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7353 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7354 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7355 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7356 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7357 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7358 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7359 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7360 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7361 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7362 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7363 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7364 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7368 | outsize = WideCharToMultiByte(code_page, flags, |
| 7369 | p, size, |
| 7370 | out, outsize, |
| 7371 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7372 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7373 | if (outsize <= 0) |
| 7374 | goto error; |
| 7375 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7376 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7377 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7378 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7379 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7380 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7381 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7382 | return -2; |
| 7383 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7384 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7385 | } |
| 7386 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7387 | /* |
| 7388 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7389 | * error handler. |
| 7390 | * |
| 7391 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7392 | * -1 on other error. |
| 7393 | */ |
| 7394 | static int |
| 7395 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7396 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7397 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7398 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7400 | Py_ssize_t pos = unicode_offset; |
| 7401 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7402 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7403 | 2000 English version of the message. */ |
| 7404 | const char *reason = "invalid character"; |
| 7405 | /* 4=maximum length of a UTF-8 sequence */ |
| 7406 | char buffer[4]; |
| 7407 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7408 | Py_ssize_t outsize; |
| 7409 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7410 | PyObject *errorHandler = NULL; |
| 7411 | PyObject *exc = NULL; |
| 7412 | PyObject *encoding_obj = NULL; |
| 7413 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7414 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7415 | PyObject *rep; |
| 7416 | int ret = -1; |
| 7417 | |
| 7418 | assert(insize > 0); |
| 7419 | |
| 7420 | encoding = code_page_name(code_page, &encoding_obj); |
| 7421 | if (encoding == NULL) |
| 7422 | return -1; |
| 7423 | |
| 7424 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7425 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7426 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7427 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7428 | if (exc != NULL) { |
| 7429 | PyCodec_StrictErrors(exc); |
| 7430 | Py_DECREF(exc); |
| 7431 | } |
| 7432 | Py_XDECREF(encoding_obj); |
| 7433 | return -1; |
| 7434 | } |
| 7435 | |
| 7436 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7437 | pusedDefaultChar = &usedDefaultChar; |
| 7438 | else |
| 7439 | pusedDefaultChar = NULL; |
| 7440 | |
| 7441 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7442 | PyErr_NoMemory(); |
| 7443 | goto error; |
| 7444 | } |
| 7445 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7446 | |
| 7447 | if (*outbytes == NULL) { |
| 7448 | /* Create string object */ |
| 7449 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7450 | if (*outbytes == NULL) |
| 7451 | goto error; |
| 7452 | out = PyBytes_AS_STRING(*outbytes); |
| 7453 | } |
| 7454 | else { |
| 7455 | /* Extend string object */ |
| 7456 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7457 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7458 | PyErr_NoMemory(); |
| 7459 | goto error; |
| 7460 | } |
| 7461 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7462 | goto error; |
| 7463 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7464 | } |
| 7465 | |
| 7466 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7467 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7468 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7469 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7470 | wchar_t chars[2]; |
| 7471 | int charsize; |
| 7472 | if (ch < 0x10000) { |
| 7473 | chars[0] = (wchar_t)ch; |
| 7474 | charsize = 1; |
| 7475 | } |
| 7476 | else { |
| 7477 | ch -= 0x10000; |
| 7478 | chars[0] = 0xd800 + (ch >> 10); |
| 7479 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7480 | charsize = 2; |
| 7481 | } |
| 7482 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7483 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7484 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7485 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7486 | NULL, pusedDefaultChar); |
| 7487 | if (outsize > 0) { |
| 7488 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7489 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7490 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7491 | memcpy(out, buffer, outsize); |
| 7492 | out += outsize; |
| 7493 | continue; |
| 7494 | } |
| 7495 | } |
| 7496 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7497 | PyErr_SetFromWindowsErr(0); |
| 7498 | goto error; |
| 7499 | } |
| 7500 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7501 | rep = unicode_encode_call_errorhandler( |
| 7502 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7503 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7504 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7505 | if (rep == NULL) |
| 7506 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7507 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7508 | |
| 7509 | if (PyBytes_Check(rep)) { |
| 7510 | outsize = PyBytes_GET_SIZE(rep); |
| 7511 | if (outsize != 1) { |
| 7512 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7513 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7514 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7515 | Py_DECREF(rep); |
| 7516 | goto error; |
| 7517 | } |
| 7518 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7519 | } |
| 7520 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7521 | out += outsize; |
| 7522 | } |
| 7523 | else { |
| 7524 | Py_ssize_t i; |
| 7525 | enum PyUnicode_Kind kind; |
| 7526 | void *data; |
| 7527 | |
| 7528 | if (PyUnicode_READY(rep) < 0) { |
| 7529 | Py_DECREF(rep); |
| 7530 | goto error; |
| 7531 | } |
| 7532 | |
| 7533 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7534 | if (outsize != 1) { |
| 7535 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7536 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7537 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7538 | Py_DECREF(rep); |
| 7539 | goto error; |
| 7540 | } |
| 7541 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7542 | } |
| 7543 | kind = PyUnicode_KIND(rep); |
| 7544 | data = PyUnicode_DATA(rep); |
| 7545 | for (i=0; i < outsize; i++) { |
| 7546 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7547 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7548 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7549 | encoding, unicode, |
| 7550 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7551 | "unable to encode error handler result to ASCII"); |
| 7552 | Py_DECREF(rep); |
| 7553 | goto error; |
| 7554 | } |
| 7555 | *out = (unsigned char)ch; |
| 7556 | out++; |
| 7557 | } |
| 7558 | } |
| 7559 | Py_DECREF(rep); |
| 7560 | } |
| 7561 | /* write a NUL byte */ |
| 7562 | *out = 0; |
| 7563 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7564 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7565 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7566 | goto error; |
| 7567 | ret = 0; |
| 7568 | |
| 7569 | error: |
| 7570 | Py_XDECREF(encoding_obj); |
| 7571 | Py_XDECREF(errorHandler); |
| 7572 | Py_XDECREF(exc); |
| 7573 | return ret; |
| 7574 | } |
| 7575 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7576 | static PyObject * |
| 7577 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7578 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7579 | const char *errors) |
| 7580 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7581 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7582 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7583 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7584 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7585 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7586 | if (PyUnicode_READY(unicode) < 0) |
| 7587 | return NULL; |
| 7588 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7589 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7590 | if (code_page < 0) { |
| 7591 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7592 | return NULL; |
| 7593 | } |
| 7594 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7595 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7596 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7597 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7598 | offset = 0; |
| 7599 | do |
| 7600 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7601 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7602 | /* 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] | 7603 | chunks. */ |
| 7604 | if (len > INT_MAX/2) { |
| 7605 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7606 | done = 0; |
| 7607 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7608 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7609 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7610 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7611 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7612 | done = 1; |
| 7613 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7614 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7615 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7616 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7617 | errors); |
| 7618 | if (ret == -2) |
| 7619 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7620 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7621 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7622 | if (ret < 0) { |
| 7623 | Py_XDECREF(outbytes); |
| 7624 | return NULL; |
| 7625 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7626 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7627 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7628 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7629 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7630 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7631 | return outbytes; |
| 7632 | } |
| 7633 | |
| 7634 | PyObject * |
| 7635 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7636 | Py_ssize_t size, |
| 7637 | const char *errors) |
| 7638 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7639 | PyObject *unicode, *res; |
| 7640 | unicode = PyUnicode_FromUnicode(p, size); |
| 7641 | if (unicode == NULL) |
| 7642 | return NULL; |
| 7643 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7644 | Py_DECREF(unicode); |
| 7645 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7646 | } |
| 7647 | |
| 7648 | PyObject * |
| 7649 | PyUnicode_EncodeCodePage(int code_page, |
| 7650 | PyObject *unicode, |
| 7651 | const char *errors) |
| 7652 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7653 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7654 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7655 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7656 | PyObject * |
| 7657 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7658 | { |
| 7659 | if (!PyUnicode_Check(unicode)) { |
| 7660 | PyErr_BadArgument(); |
| 7661 | return NULL; |
| 7662 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7663 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7666 | #undef NEED_RETRY |
| 7667 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7668 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7670 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7671 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7672 | PyObject * |
| 7673 | PyUnicode_DecodeCharmap(const char *s, |
| 7674 | Py_ssize_t size, |
| 7675 | PyObject *mapping, |
| 7676 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7677 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7678 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7679 | Py_ssize_t startinpos; |
| 7680 | Py_ssize_t endinpos; |
| 7681 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7682 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7683 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7684 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7685 | PyObject *errorHandler = NULL; |
| 7686 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | /* Default to Latin-1 */ |
| 7689 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7690 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7691 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7692 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7696 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7697 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7698 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7699 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7700 | Py_ssize_t maplen; |
| 7701 | enum PyUnicode_Kind kind; |
| 7702 | void *data; |
| 7703 | Py_UCS4 x; |
| 7704 | |
| 7705 | if (PyUnicode_READY(mapping) < 0) |
| 7706 | return NULL; |
| 7707 | |
| 7708 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7709 | data = PyUnicode_DATA(mapping); |
| 7710 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7711 | while (s < e) { |
| 7712 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7715 | x = PyUnicode_READ(kind, data, ch); |
| 7716 | else |
| 7717 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7718 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7719 | if (x == 0xfffe) |
| 7720 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | startinpos = s-starts; |
| 7723 | endinpos = startinpos+1; |
| 7724 | if (unicode_decode_call_errorhandler( |
| 7725 | errors, &errorHandler, |
| 7726 | "charmap", "character maps to <undefined>", |
| 7727 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7728 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7729 | goto onError; |
| 7730 | } |
| 7731 | continue; |
| 7732 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7733 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7734 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7735 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7736 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7737 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7738 | } |
| 7739 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7740 | while (s < e) { |
| 7741 | unsigned char ch = *s; |
| 7742 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7743 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7745 | w = PyLong_FromLong((long)ch); |
| 7746 | if (w == NULL) |
| 7747 | goto onError; |
| 7748 | x = PyObject_GetItem(mapping, w); |
| 7749 | Py_DECREF(w); |
| 7750 | if (x == NULL) { |
| 7751 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7752 | /* No mapping found means: mapping is undefined. */ |
| 7753 | PyErr_Clear(); |
| 7754 | x = Py_None; |
| 7755 | Py_INCREF(x); |
| 7756 | } else |
| 7757 | goto onError; |
| 7758 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7759 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7760 | /* Apply mapping */ |
| 7761 | if (PyLong_Check(x)) { |
| 7762 | long value = PyLong_AS_LONG(x); |
| 7763 | if (value < 0 || value > 65535) { |
| 7764 | PyErr_SetString(PyExc_TypeError, |
| 7765 | "character mapping must be in range(65536)"); |
| 7766 | Py_DECREF(x); |
| 7767 | goto onError; |
| 7768 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7769 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7770 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7771 | } |
| 7772 | else if (x == Py_None) { |
| 7773 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7774 | startinpos = s-starts; |
| 7775 | endinpos = startinpos+1; |
| 7776 | if (unicode_decode_call_errorhandler( |
| 7777 | errors, &errorHandler, |
| 7778 | "charmap", "character maps to <undefined>", |
| 7779 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7780 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | Py_DECREF(x); |
| 7782 | goto onError; |
| 7783 | } |
| 7784 | Py_DECREF(x); |
| 7785 | continue; |
| 7786 | } |
| 7787 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7788 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7789 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7790 | if (PyUnicode_READY(x) < 0) |
| 7791 | goto onError; |
| 7792 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7793 | |
| 7794 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7796 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7797 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7798 | goto onError; |
| 7799 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | else if (targetsize > 1) { |
| 7801 | /* 1-n mapping */ |
| 7802 | if (targetsize > extrachars) { |
| 7803 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7805 | (targetsize << 2); |
| 7806 | extrachars += needed; |
| 7807 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7808 | if (unicode_resize(&v, |
| 7809 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7810 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | Py_DECREF(x); |
| 7812 | goto onError; |
| 7813 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7815 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7816 | goto onError; |
| 7817 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7818 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | extrachars -= targetsize; |
| 7820 | } |
| 7821 | /* 1-0 mapping: skip the character */ |
| 7822 | } |
| 7823 | else { |
| 7824 | /* wrong return value */ |
| 7825 | PyErr_SetString(PyExc_TypeError, |
| 7826 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7827 | Py_DECREF(x); |
| 7828 | goto onError; |
| 7829 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | Py_DECREF(x); |
| 7831 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7832 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7833 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7834 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7835 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7836 | Py_XDECREF(errorHandler); |
| 7837 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7838 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7840 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7841 | Py_XDECREF(errorHandler); |
| 7842 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | Py_XDECREF(v); |
| 7844 | return NULL; |
| 7845 | } |
| 7846 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7847 | /* Charmap encoding: the lookup table */ |
| 7848 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7849 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | PyObject_HEAD |
| 7851 | unsigned char level1[32]; |
| 7852 | int count2, count3; |
| 7853 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | }; |
| 7855 | |
| 7856 | static PyObject* |
| 7857 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7858 | { |
| 7859 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7860 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7865 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7866 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7867 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7868 | }; |
| 7869 | |
| 7870 | static void |
| 7871 | encoding_map_dealloc(PyObject* o) |
| 7872 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7873 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7877 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7878 | "EncodingMap", /*tp_name*/ |
| 7879 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7880 | 0, /*tp_itemsize*/ |
| 7881 | /* methods */ |
| 7882 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7883 | 0, /*tp_print*/ |
| 7884 | 0, /*tp_getattr*/ |
| 7885 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7886 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | 0, /*tp_repr*/ |
| 7888 | 0, /*tp_as_number*/ |
| 7889 | 0, /*tp_as_sequence*/ |
| 7890 | 0, /*tp_as_mapping*/ |
| 7891 | 0, /*tp_hash*/ |
| 7892 | 0, /*tp_call*/ |
| 7893 | 0, /*tp_str*/ |
| 7894 | 0, /*tp_getattro*/ |
| 7895 | 0, /*tp_setattro*/ |
| 7896 | 0, /*tp_as_buffer*/ |
| 7897 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7898 | 0, /*tp_doc*/ |
| 7899 | 0, /*tp_traverse*/ |
| 7900 | 0, /*tp_clear*/ |
| 7901 | 0, /*tp_richcompare*/ |
| 7902 | 0, /*tp_weaklistoffset*/ |
| 7903 | 0, /*tp_iter*/ |
| 7904 | 0, /*tp_iternext*/ |
| 7905 | encoding_map_methods, /*tp_methods*/ |
| 7906 | 0, /*tp_members*/ |
| 7907 | 0, /*tp_getset*/ |
| 7908 | 0, /*tp_base*/ |
| 7909 | 0, /*tp_dict*/ |
| 7910 | 0, /*tp_descr_get*/ |
| 7911 | 0, /*tp_descr_set*/ |
| 7912 | 0, /*tp_dictoffset*/ |
| 7913 | 0, /*tp_init*/ |
| 7914 | 0, /*tp_alloc*/ |
| 7915 | 0, /*tp_new*/ |
| 7916 | 0, /*tp_free*/ |
| 7917 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7918 | }; |
| 7919 | |
| 7920 | PyObject* |
| 7921 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7922 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7923 | PyObject *result; |
| 7924 | struct encoding_map *mresult; |
| 7925 | int i; |
| 7926 | int need_dict = 0; |
| 7927 | unsigned char level1[32]; |
| 7928 | unsigned char level2[512]; |
| 7929 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7930 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7931 | int kind; |
| 7932 | void *data; |
| 7933 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7934 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7935 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7936 | PyErr_BadArgument(); |
| 7937 | return NULL; |
| 7938 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7939 | kind = PyUnicode_KIND(string); |
| 7940 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7941 | memset(level1, 0xFF, sizeof level1); |
| 7942 | memset(level2, 0xFF, sizeof level2); |
| 7943 | |
| 7944 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7945 | or if there are non-BMP characters, we need to use |
| 7946 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7947 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7948 | need_dict = 1; |
| 7949 | for (i = 1; i < 256; i++) { |
| 7950 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7951 | ch = PyUnicode_READ(kind, data, i); |
| 7952 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7953 | need_dict = 1; |
| 7954 | break; |
| 7955 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7956 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7957 | /* unmapped character */ |
| 7958 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7959 | l1 = ch >> 11; |
| 7960 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7961 | if (level1[l1] == 0xFF) |
| 7962 | level1[l1] = count2++; |
| 7963 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7964 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7968 | need_dict = 1; |
| 7969 | |
| 7970 | if (need_dict) { |
| 7971 | PyObject *result = PyDict_New(); |
| 7972 | PyObject *key, *value; |
| 7973 | if (!result) |
| 7974 | return NULL; |
| 7975 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7976 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7977 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7978 | if (!key || !value) |
| 7979 | goto failed1; |
| 7980 | if (PyDict_SetItem(result, key, value) == -1) |
| 7981 | goto failed1; |
| 7982 | Py_DECREF(key); |
| 7983 | Py_DECREF(value); |
| 7984 | } |
| 7985 | return result; |
| 7986 | failed1: |
| 7987 | Py_XDECREF(key); |
| 7988 | Py_XDECREF(value); |
| 7989 | Py_DECREF(result); |
| 7990 | return NULL; |
| 7991 | } |
| 7992 | |
| 7993 | /* Create a three-level trie */ |
| 7994 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7995 | 16*count2 + 128*count3 - 1); |
| 7996 | if (!result) |
| 7997 | return PyErr_NoMemory(); |
| 7998 | PyObject_Init(result, &EncodingMapType); |
| 7999 | mresult = (struct encoding_map*)result; |
| 8000 | mresult->count2 = count2; |
| 8001 | mresult->count3 = count3; |
| 8002 | mlevel1 = mresult->level1; |
| 8003 | mlevel2 = mresult->level23; |
| 8004 | mlevel3 = mresult->level23 + 16*count2; |
| 8005 | memcpy(mlevel1, level1, 32); |
| 8006 | memset(mlevel2, 0xFF, 16*count2); |
| 8007 | memset(mlevel3, 0, 128*count3); |
| 8008 | count3 = 0; |
| 8009 | for (i = 1; i < 256; i++) { |
| 8010 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8011 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8012 | /* unmapped character */ |
| 8013 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8014 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8015 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8016 | i2 = 16*mlevel1[o1] + o2; |
| 8017 | if (mlevel2[i2] == 0xFF) |
| 8018 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8019 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8020 | i3 = 128*mlevel2[i2] + o3; |
| 8021 | mlevel3[i3] = i; |
| 8022 | } |
| 8023 | return result; |
| 8024 | } |
| 8025 | |
| 8026 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8027 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8028 | { |
| 8029 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8030 | int l1 = c>>11; |
| 8031 | int l2 = (c>>7) & 0xF; |
| 8032 | int l3 = c & 0x7F; |
| 8033 | int i; |
| 8034 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8035 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8037 | if (c == 0) |
| 8038 | return 0; |
| 8039 | /* level 1*/ |
| 8040 | i = map->level1[l1]; |
| 8041 | if (i == 0xFF) { |
| 8042 | return -1; |
| 8043 | } |
| 8044 | /* level 2*/ |
| 8045 | i = map->level23[16*i+l2]; |
| 8046 | if (i == 0xFF) { |
| 8047 | return -1; |
| 8048 | } |
| 8049 | /* level 3 */ |
| 8050 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8051 | if (i == 0) { |
| 8052 | return -1; |
| 8053 | } |
| 8054 | return i; |
| 8055 | } |
| 8056 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8057 | /* Lookup the character ch in the mapping. If the character |
| 8058 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8059 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8060 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8061 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8062 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8063 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8064 | PyObject *x; |
| 8065 | |
| 8066 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8068 | x = PyObject_GetItem(mapping, w); |
| 8069 | Py_DECREF(w); |
| 8070 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8072 | /* No mapping found means: mapping is undefined. */ |
| 8073 | PyErr_Clear(); |
| 8074 | x = Py_None; |
| 8075 | Py_INCREF(x); |
| 8076 | return x; |
| 8077 | } else |
| 8078 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8080 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8081 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8082 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | long value = PyLong_AS_LONG(x); |
| 8084 | if (value < 0 || value > 255) { |
| 8085 | PyErr_SetString(PyExc_TypeError, |
| 8086 | "character mapping must be in range(256)"); |
| 8087 | Py_DECREF(x); |
| 8088 | return NULL; |
| 8089 | } |
| 8090 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8091 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8092 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8094 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | /* wrong return value */ |
| 8096 | PyErr_Format(PyExc_TypeError, |
| 8097 | "character mapping must return integer, bytes or None, not %.400s", |
| 8098 | x->ob_type->tp_name); |
| 8099 | Py_DECREF(x); |
| 8100 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | } |
| 8102 | } |
| 8103 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8104 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8105 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8106 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8107 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8108 | /* exponentially overallocate to minimize reallocations */ |
| 8109 | if (requiredsize < 2*outsize) |
| 8110 | requiredsize = 2*outsize; |
| 8111 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8112 | return -1; |
| 8113 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8116 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8118 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8119 | /* 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] | 8120 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | space is available. Return a new reference to the object that |
| 8122 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8123 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8124 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8125 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8126 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8127 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8128 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8129 | PyObject *rep; |
| 8130 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8131 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8132 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8133 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8134 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8135 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8136 | if (res == -1) |
| 8137 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 | if (outsize<requiredsize) |
| 8139 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8140 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8141 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | outstart[(*outpos)++] = (char)res; |
| 8143 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8147 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8149 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8150 | Py_DECREF(rep); |
| 8151 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8152 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | if (PyLong_Check(rep)) { |
| 8154 | Py_ssize_t requiredsize = *outpos+1; |
| 8155 | if (outsize<requiredsize) |
| 8156 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8157 | Py_DECREF(rep); |
| 8158 | return enc_EXCEPTION; |
| 8159 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8160 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8161 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8162 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8163 | else { |
| 8164 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8165 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8166 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8167 | if (outsize<requiredsize) |
| 8168 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8169 | Py_DECREF(rep); |
| 8170 | return enc_EXCEPTION; |
| 8171 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8172 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | memcpy(outstart + *outpos, repchars, repsize); |
| 8174 | *outpos += repsize; |
| 8175 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8176 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8177 | Py_DECREF(rep); |
| 8178 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
| 8181 | /* handle an error in PyUnicode_EncodeCharmap |
| 8182 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8183 | static int |
| 8184 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8185 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8186 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8187 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8188 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8189 | { |
| 8190 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8191 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8192 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8193 | enum PyUnicode_Kind kind; |
| 8194 | void *data; |
| 8195 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8196 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8197 | Py_ssize_t collstartpos = *inpos; |
| 8198 | Py_ssize_t collendpos = *inpos+1; |
| 8199 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8200 | char *encoding = "charmap"; |
| 8201 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8202 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8203 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8204 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8206 | if (PyUnicode_READY(unicode) < 0) |
| 8207 | return -1; |
| 8208 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8209 | /* find all unencodable characters */ |
| 8210 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8211 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8212 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8213 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8214 | val = encoding_map_lookup(ch, mapping); |
| 8215 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | break; |
| 8217 | ++collendpos; |
| 8218 | continue; |
| 8219 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8220 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8221 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8222 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | if (rep==NULL) |
| 8224 | return -1; |
| 8225 | else if (rep!=Py_None) { |
| 8226 | Py_DECREF(rep); |
| 8227 | break; |
| 8228 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8229 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8231 | } |
| 8232 | /* cache callback name lookup |
| 8233 | * (if not done yet, i.e. it's the first error) */ |
| 8234 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8236 | *known_errorHandler = 1; |
| 8237 | else if (!strcmp(errors, "replace")) |
| 8238 | *known_errorHandler = 2; |
| 8239 | else if (!strcmp(errors, "ignore")) |
| 8240 | *known_errorHandler = 3; |
| 8241 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8242 | *known_errorHandler = 4; |
| 8243 | else |
| 8244 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8245 | } |
| 8246 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8247 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8248 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8249 | return -1; |
| 8250 | case 2: /* replace */ |
| 8251 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | x = charmapencode_output('?', mapping, res, respos); |
| 8253 | if (x==enc_EXCEPTION) { |
| 8254 | return -1; |
| 8255 | } |
| 8256 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8257 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | return -1; |
| 8259 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8260 | } |
| 8261 | /* fall through */ |
| 8262 | case 3: /* ignore */ |
| 8263 | *inpos = collendpos; |
| 8264 | break; |
| 8265 | case 4: /* xmlcharrefreplace */ |
| 8266 | /* generate replacement (temporarily (mis)uses p) */ |
| 8267 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | char buffer[2+29+1+1]; |
| 8269 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8270 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | for (cp = buffer; *cp; ++cp) { |
| 8272 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8273 | if (x==enc_EXCEPTION) |
| 8274 | return -1; |
| 8275 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8276 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | return -1; |
| 8278 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8279 | } |
| 8280 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8281 | *inpos = collendpos; |
| 8282 | break; |
| 8283 | default: |
| 8284 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8285 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8286 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8287 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8288 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8289 | if (PyBytes_Check(repunicode)) { |
| 8290 | /* Directly copy bytes result to output. */ |
| 8291 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8292 | Py_ssize_t requiredsize; |
| 8293 | repsize = PyBytes_Size(repunicode); |
| 8294 | requiredsize = *respos + repsize; |
| 8295 | if (requiredsize > outsize) |
| 8296 | /* Make room for all additional bytes. */ |
| 8297 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8298 | Py_DECREF(repunicode); |
| 8299 | return -1; |
| 8300 | } |
| 8301 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8302 | PyBytes_AsString(repunicode), repsize); |
| 8303 | *respos += repsize; |
| 8304 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8305 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8306 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8307 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8308 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8309 | if (PyUnicode_READY(repunicode) < 0) { |
| 8310 | Py_DECREF(repunicode); |
| 8311 | return -1; |
| 8312 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8313 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8314 | data = PyUnicode_DATA(repunicode); |
| 8315 | kind = PyUnicode_KIND(repunicode); |
| 8316 | for (index = 0; index < repsize; index++) { |
| 8317 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8318 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8320 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | return -1; |
| 8322 | } |
| 8323 | else if (x==enc_FAILED) { |
| 8324 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8325 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | return -1; |
| 8327 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8328 | } |
| 8329 | *inpos = newpos; |
| 8330 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8331 | } |
| 8332 | return 0; |
| 8333 | } |
| 8334 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8335 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8336 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8337 | PyObject *mapping, |
| 8338 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8339 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8340 | /* output object */ |
| 8341 | PyObject *res = NULL; |
| 8342 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8343 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8344 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8345 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8346 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8347 | PyObject *errorHandler = NULL; |
| 8348 | PyObject *exc = NULL; |
| 8349 | /* the following variable is used for caching string comparisons |
| 8350 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8351 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8352 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8354 | if (PyUnicode_READY(unicode) < 0) |
| 8355 | return NULL; |
| 8356 | size = PyUnicode_GET_LENGTH(unicode); |
| 8357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | /* Default to Latin-1 */ |
| 8359 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8360 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | /* allocate enough for a simple encoding without |
| 8363 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8364 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8365 | if (res == NULL) |
| 8366 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8367 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8371 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8372 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8373 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | if (x==enc_EXCEPTION) /* error */ |
| 8375 | goto onError; |
| 8376 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8377 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | &exc, |
| 8379 | &known_errorHandler, &errorHandler, errors, |
| 8380 | &res, &respos)) { |
| 8381 | goto onError; |
| 8382 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8383 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | else |
| 8385 | /* done with this character => adjust input position */ |
| 8386 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8388 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8390 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8391 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8392 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8393 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | Py_XDECREF(exc); |
| 8395 | Py_XDECREF(errorHandler); |
| 8396 | return res; |
| 8397 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8398 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8399 | Py_XDECREF(res); |
| 8400 | Py_XDECREF(exc); |
| 8401 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8402 | return NULL; |
| 8403 | } |
| 8404 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8405 | /* Deprecated */ |
| 8406 | PyObject * |
| 8407 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8408 | Py_ssize_t size, |
| 8409 | PyObject *mapping, |
| 8410 | const char *errors) |
| 8411 | { |
| 8412 | PyObject *result; |
| 8413 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8414 | if (unicode == NULL) |
| 8415 | return NULL; |
| 8416 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8417 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8418 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8419 | } |
| 8420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8421 | PyObject * |
| 8422 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8423 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8424 | { |
| 8425 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8426 | PyErr_BadArgument(); |
| 8427 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8429 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | } |
| 8431 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8432 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8433 | static void |
| 8434 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8435 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8436 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8437 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8439 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8441 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8442 | } |
| 8443 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8445 | goto onError; |
| 8446 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8447 | goto onError; |
| 8448 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8449 | goto onError; |
| 8450 | return; |
| 8451 | onError: |
| 8452 | Py_DECREF(*exceptionObject); |
| 8453 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8454 | } |
| 8455 | } |
| 8456 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8458 | static void |
| 8459 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8461 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8462 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8463 | { |
| 8464 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8465 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8466 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8467 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8468 | } |
| 8469 | |
| 8470 | /* error handling callback helper: |
| 8471 | build arguments, call the callback and check the arguments, |
| 8472 | put the result into newpos and return the replacement string, which |
| 8473 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8474 | static PyObject * |
| 8475 | unicode_translate_call_errorhandler(const char *errors, |
| 8476 | PyObject **errorHandler, |
| 8477 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8479 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8480 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8481 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8482 | 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] | 8483 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8484 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8485 | PyObject *restuple; |
| 8486 | PyObject *resunicode; |
| 8487 | |
| 8488 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8489 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8490 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8495 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8496 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8498 | |
| 8499 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8501 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8503 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8504 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8505 | Py_DECREF(restuple); |
| 8506 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8507 | } |
| 8508 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | &resunicode, &i_newpos)) { |
| 8510 | Py_DECREF(restuple); |
| 8511 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8512 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8513 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8514 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8515 | else |
| 8516 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8519 | Py_DECREF(restuple); |
| 8520 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8521 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8522 | Py_INCREF(resunicode); |
| 8523 | Py_DECREF(restuple); |
| 8524 | return resunicode; |
| 8525 | } |
| 8526 | |
| 8527 | /* Lookup the character ch in the mapping and put the result in result, |
| 8528 | which must be decrefed by the caller. |
| 8529 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8530 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8533 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8534 | PyObject *x; |
| 8535 | |
| 8536 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8538 | x = PyObject_GetItem(mapping, w); |
| 8539 | Py_DECREF(w); |
| 8540 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8541 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8542 | /* No mapping found means: use 1:1 mapping. */ |
| 8543 | PyErr_Clear(); |
| 8544 | *result = NULL; |
| 8545 | return 0; |
| 8546 | } else |
| 8547 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8548 | } |
| 8549 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | *result = x; |
| 8551 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8552 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8553 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | long value = PyLong_AS_LONG(x); |
| 8555 | long max = PyUnicode_GetMax(); |
| 8556 | if (value < 0 || value > max) { |
| 8557 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8558 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | Py_DECREF(x); |
| 8560 | return -1; |
| 8561 | } |
| 8562 | *result = x; |
| 8563 | return 0; |
| 8564 | } |
| 8565 | else if (PyUnicode_Check(x)) { |
| 8566 | *result = x; |
| 8567 | return 0; |
| 8568 | } |
| 8569 | else { |
| 8570 | /* wrong return value */ |
| 8571 | PyErr_SetString(PyExc_TypeError, |
| 8572 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8573 | Py_DECREF(x); |
| 8574 | return -1; |
| 8575 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8576 | } |
| 8577 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8578 | if not reallocate and adjust various state variables. |
| 8579 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8580 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8581 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8584 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8585 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8586 | /* exponentially overallocate to minimize reallocations */ |
| 8587 | if (requiredsize < 2 * oldsize) |
| 8588 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8589 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8590 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8593 | } |
| 8594 | return 0; |
| 8595 | } |
| 8596 | /* lookup the character, put the result in the output string and adjust |
| 8597 | various state variables. Return a new reference to the object that |
| 8598 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8599 | undefined (in which case no character was written). |
| 8600 | The called must decref result. |
| 8601 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8602 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8604 | PyObject *mapping, Py_UCS4 **output, |
| 8605 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8606 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8607 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8609 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8611 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8614 | } |
| 8615 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8617 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8618 | /* 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] | 8619 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8620 | } |
| 8621 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | Py_ssize_t repsize; |
| 8623 | if (PyUnicode_READY(*res) == -1) |
| 8624 | return -1; |
| 8625 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8626 | if (repsize==1) { |
| 8627 | /* 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] | 8628 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | } |
| 8630 | else if (repsize!=0) { |
| 8631 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | Py_ssize_t requiredsize = *opos + |
| 8633 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8634 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8635 | Py_ssize_t i; |
| 8636 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8638 | for(i = 0; i < repsize; i++) |
| 8639 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8640 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8641 | } |
| 8642 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8643 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8644 | return 0; |
| 8645 | } |
| 8646 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8647 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8648 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8649 | PyObject *mapping, |
| 8650 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | /* input object */ |
| 8653 | char *idata; |
| 8654 | Py_ssize_t size, i; |
| 8655 | int kind; |
| 8656 | /* output buffer */ |
| 8657 | Py_UCS4 *output = NULL; |
| 8658 | Py_ssize_t osize; |
| 8659 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8660 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8662 | char *reason = "character maps to <undefined>"; |
| 8663 | PyObject *errorHandler = NULL; |
| 8664 | PyObject *exc = NULL; |
| 8665 | /* the following variable is used for caching string comparisons |
| 8666 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8667 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8668 | int known_errorHandler = -1; |
| 8669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8671 | PyErr_BadArgument(); |
| 8672 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8675 | if (PyUnicode_READY(input) == -1) |
| 8676 | return NULL; |
| 8677 | idata = (char*)PyUnicode_DATA(input); |
| 8678 | kind = PyUnicode_KIND(input); |
| 8679 | size = PyUnicode_GET_LENGTH(input); |
| 8680 | i = 0; |
| 8681 | |
| 8682 | if (size == 0) { |
| 8683 | Py_INCREF(input); |
| 8684 | return input; |
| 8685 | } |
| 8686 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | /* allocate enough for a simple 1:1 translation without |
| 8688 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | osize = size; |
| 8690 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8691 | opos = 0; |
| 8692 | if (output == NULL) { |
| 8693 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8695 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8698 | /* try to encode it */ |
| 8699 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8700 | if (charmaptranslate_output(input, i, mapping, |
| 8701 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8702 | Py_XDECREF(x); |
| 8703 | goto onError; |
| 8704 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8705 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8706 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8708 | else { /* untranslatable character */ |
| 8709 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8710 | Py_ssize_t repsize; |
| 8711 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8712 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8713 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | Py_ssize_t collstart = i; |
| 8715 | Py_ssize_t collend = i+1; |
| 8716 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8719 | while (collend < size) { |
| 8720 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | goto onError; |
| 8722 | Py_XDECREF(x); |
| 8723 | if (x!=Py_None) |
| 8724 | break; |
| 8725 | ++collend; |
| 8726 | } |
| 8727 | /* cache callback name lookup |
| 8728 | * (if not done yet, i.e. it's the first error) */ |
| 8729 | if (known_errorHandler==-1) { |
| 8730 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8731 | known_errorHandler = 1; |
| 8732 | else if (!strcmp(errors, "replace")) |
| 8733 | known_errorHandler = 2; |
| 8734 | else if (!strcmp(errors, "ignore")) |
| 8735 | known_errorHandler = 3; |
| 8736 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8737 | known_errorHandler = 4; |
| 8738 | else |
| 8739 | known_errorHandler = 0; |
| 8740 | } |
| 8741 | switch (known_errorHandler) { |
| 8742 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8743 | raise_translate_exception(&exc, input, collstart, |
| 8744 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8745 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8746 | case 2: /* replace */ |
| 8747 | /* 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] | 8748 | for (coll = collstart; coll<collend; coll++) |
| 8749 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8750 | /* fall through */ |
| 8751 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8753 | break; |
| 8754 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8755 | /* generate replacement (temporarily (mis)uses i) */ |
| 8756 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8757 | char buffer[2+29+1+1]; |
| 8758 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8760 | if (charmaptranslate_makespace(&output, &osize, |
| 8761 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8762 | goto onError; |
| 8763 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8766 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8767 | break; |
| 8768 | default: |
| 8769 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | reason, input, &exc, |
| 8771 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8772 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8774 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8775 | Py_DECREF(repunicode); |
| 8776 | goto onError; |
| 8777 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8778 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8779 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8780 | if (charmaptranslate_makespace(&output, &osize, |
| 8781 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | Py_DECREF(repunicode); |
| 8783 | goto onError; |
| 8784 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8785 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8786 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8787 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8788 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8789 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8790 | } |
| 8791 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8793 | if (!res) |
| 8794 | goto onError; |
| 8795 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8796 | Py_XDECREF(exc); |
| 8797 | Py_XDECREF(errorHandler); |
| 8798 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8800 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8801 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8802 | Py_XDECREF(exc); |
| 8803 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | return NULL; |
| 8805 | } |
| 8806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8807 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8808 | PyObject * |
| 8809 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8810 | Py_ssize_t size, |
| 8811 | PyObject *mapping, |
| 8812 | const char *errors) |
| 8813 | { |
| 8814 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8815 | if (!unicode) |
| 8816 | return NULL; |
| 8817 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8818 | } |
| 8819 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8820 | PyObject * |
| 8821 | PyUnicode_Translate(PyObject *str, |
| 8822 | PyObject *mapping, |
| 8823 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8824 | { |
| 8825 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8826 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8827 | str = PyUnicode_FromObject(str); |
| 8828 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8829 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8830 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8831 | Py_DECREF(str); |
| 8832 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8833 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8834 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | Py_XDECREF(str); |
| 8836 | return NULL; |
| 8837 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8838 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8839 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8840 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8841 | { |
| 8842 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8843 | called as a callback from fixup() which does it already. */ |
| 8844 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8845 | const int kind = PyUnicode_KIND(self); |
| 8846 | void *data = PyUnicode_DATA(self); |
| 8847 | Py_UCS4 maxchar = 0, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame^] | 8848 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8849 | Py_ssize_t i; |
| 8850 | |
| 8851 | for (i = 0; i < len; ++i) { |
| 8852 | ch = PyUnicode_READ(kind, data, i); |
| 8853 | fixed = 0; |
| 8854 | if (ch > 127) { |
| 8855 | if (Py_UNICODE_ISSPACE(ch)) |
| 8856 | fixed = ' '; |
| 8857 | else { |
| 8858 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8859 | if (decimal >= 0) |
| 8860 | fixed = '0' + decimal; |
| 8861 | } |
| 8862 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame^] | 8863 | modified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | if (fixed > maxchar) |
| 8865 | maxchar = fixed; |
| 8866 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8867 | } |
| 8868 | else if (ch > maxchar) |
| 8869 | maxchar = ch; |
| 8870 | } |
| 8871 | else if (ch > maxchar) |
| 8872 | maxchar = ch; |
| 8873 | } |
| 8874 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame^] | 8875 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | } |
| 8877 | |
| 8878 | PyObject * |
| 8879 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8880 | { |
| 8881 | if (!PyUnicode_Check(unicode)) { |
| 8882 | PyErr_BadInternalCall(); |
| 8883 | return NULL; |
| 8884 | } |
| 8885 | if (PyUnicode_READY(unicode) == -1) |
| 8886 | return NULL; |
| 8887 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8888 | /* If the string is already ASCII, just return the same string */ |
| 8889 | Py_INCREF(unicode); |
| 8890 | return unicode; |
| 8891 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8892 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | } |
| 8894 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8895 | PyObject * |
| 8896 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8897 | Py_ssize_t length) |
| 8898 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8899 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8900 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8901 | Py_UCS4 maxchar; |
| 8902 | enum PyUnicode_Kind kind; |
| 8903 | void *data; |
| 8904 | |
| 8905 | maxchar = 0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8906 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8907 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8908 | if (ch > 127) { |
| 8909 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8910 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8911 | ch = '0' + decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8912 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8913 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8914 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8915 | |
| 8916 | /* Copy to a new string */ |
| 8917 | decimal = PyUnicode_New(length, maxchar); |
| 8918 | if (decimal == NULL) |
| 8919 | return decimal; |
| 8920 | kind = PyUnicode_KIND(decimal); |
| 8921 | data = PyUnicode_DATA(decimal); |
| 8922 | /* Iterate over code points */ |
| 8923 | for (i = 0; i < length; i++) { |
| 8924 | Py_UNICODE ch = s[i]; |
| 8925 | if (ch > 127) { |
| 8926 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8927 | if (decimal >= 0) |
| 8928 | ch = '0' + decimal; |
| 8929 | } |
| 8930 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8931 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8932 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8933 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8934 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8935 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8936 | int |
| 8937 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8938 | Py_ssize_t length, |
| 8939 | char *output, |
| 8940 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8941 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8942 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8943 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8944 | enum PyUnicode_Kind kind; |
| 8945 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8946 | |
| 8947 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8948 | PyErr_BadArgument(); |
| 8949 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8950 | } |
| 8951 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8952 | unicode = PyUnicode_FromUnicode(s, length); |
| 8953 | if (unicode == NULL) |
| 8954 | return -1; |
| 8955 | |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8956 | if (PyUnicode_READY(unicode) < 0) { |
| 8957 | Py_DECREF(unicode); |
| 8958 | return -1; |
| 8959 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8960 | kind = PyUnicode_KIND(unicode); |
| 8961 | data = PyUnicode_DATA(unicode); |
| 8962 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8963 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8964 | PyObject *exc; |
| 8965 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8966 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8967 | Py_ssize_t startpos; |
| 8968 | |
| 8969 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8971 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8972 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8973 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8974 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8975 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8976 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8977 | if (decimal >= 0) { |
| 8978 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8979 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8980 | continue; |
| 8981 | } |
| 8982 | if (0 < ch && ch < 256) { |
| 8983 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8984 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8985 | continue; |
| 8986 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8987 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8988 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8989 | exc = NULL; |
| 8990 | raise_encode_exception(&exc, "decimal", unicode, |
| 8991 | startpos, startpos+1, |
| 8992 | "invalid decimal Unicode string"); |
| 8993 | Py_XDECREF(exc); |
| 8994 | Py_DECREF(unicode); |
| 8995 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8996 | } |
| 8997 | /* 0-terminate the output string */ |
| 8998 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8999 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9000 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9001 | } |
| 9002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | /* --- Helpers ------------------------------------------------------------ */ |
| 9004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9006 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | Py_ssize_t start, |
| 9008 | Py_ssize_t end) |
| 9009 | { |
| 9010 | int kind1, kind2, kind; |
| 9011 | void *buf1, *buf2; |
| 9012 | Py_ssize_t len1, len2, result; |
| 9013 | |
| 9014 | kind1 = PyUnicode_KIND(s1); |
| 9015 | kind2 = PyUnicode_KIND(s2); |
| 9016 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9017 | buf1 = PyUnicode_DATA(s1); |
| 9018 | buf2 = PyUnicode_DATA(s2); |
| 9019 | if (kind1 != kind) |
| 9020 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9021 | if (!buf1) |
| 9022 | return -2; |
| 9023 | if (kind2 != kind) |
| 9024 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9025 | if (!buf2) { |
| 9026 | if (kind1 != kind) PyMem_Free(buf1); |
| 9027 | return -2; |
| 9028 | } |
| 9029 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9030 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9031 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9032 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9033 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9034 | case PyUnicode_1BYTE_KIND: |
| 9035 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9036 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9037 | else |
| 9038 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9039 | break; |
| 9040 | case PyUnicode_2BYTE_KIND: |
| 9041 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9042 | break; |
| 9043 | case PyUnicode_4BYTE_KIND: |
| 9044 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9045 | break; |
| 9046 | default: |
| 9047 | assert(0); result = -2; |
| 9048 | } |
| 9049 | } |
| 9050 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9051 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9052 | case PyUnicode_1BYTE_KIND: |
| 9053 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9054 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9055 | else |
| 9056 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9057 | break; |
| 9058 | case PyUnicode_2BYTE_KIND: |
| 9059 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9060 | break; |
| 9061 | case PyUnicode_4BYTE_KIND: |
| 9062 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9063 | break; |
| 9064 | default: |
| 9065 | assert(0); result = -2; |
| 9066 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | } |
| 9068 | |
| 9069 | if (kind1 != kind) |
| 9070 | PyMem_Free(buf1); |
| 9071 | if (kind2 != kind) |
| 9072 | PyMem_Free(buf2); |
| 9073 | |
| 9074 | return result; |
| 9075 | } |
| 9076 | |
| 9077 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9078 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9079 | Py_ssize_t n_buffer, |
| 9080 | void *digits, Py_ssize_t n_digits, |
| 9081 | Py_ssize_t min_width, |
| 9082 | const char *grouping, |
| 9083 | const char *thousands_sep) |
| 9084 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9085 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9087 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9088 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9089 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9090 | min_width, grouping, thousands_sep); |
| 9091 | else |
| 9092 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9093 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9094 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9095 | case PyUnicode_2BYTE_KIND: |
| 9096 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9097 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9098 | min_width, grouping, thousands_sep); |
| 9099 | case PyUnicode_4BYTE_KIND: |
| 9100 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9101 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9102 | min_width, grouping, thousands_sep); |
| 9103 | } |
| 9104 | assert(0); |
| 9105 | return -1; |
| 9106 | } |
| 9107 | |
| 9108 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9109 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9110 | #define ADJUST_INDICES(start, end, len) \ |
| 9111 | if (end > len) \ |
| 9112 | end = len; \ |
| 9113 | else if (end < 0) { \ |
| 9114 | end += len; \ |
| 9115 | if (end < 0) \ |
| 9116 | end = 0; \ |
| 9117 | } \ |
| 9118 | if (start < 0) { \ |
| 9119 | start += len; \ |
| 9120 | if (start < 0) \ |
| 9121 | start = 0; \ |
| 9122 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9124 | Py_ssize_t |
| 9125 | PyUnicode_Count(PyObject *str, |
| 9126 | PyObject *substr, |
| 9127 | Py_ssize_t start, |
| 9128 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9130 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9131 | PyObject* str_obj; |
| 9132 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 | int kind1, kind2, kind; |
| 9134 | void *buf1 = NULL, *buf2 = NULL; |
| 9135 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9136 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9137 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9138 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9139 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9140 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9141 | if (!sub_obj) { |
| 9142 | Py_DECREF(str_obj); |
| 9143 | return -1; |
| 9144 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9145 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9146 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | Py_DECREF(str_obj); |
| 9148 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9149 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9150 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9151 | kind1 = PyUnicode_KIND(str_obj); |
| 9152 | kind2 = PyUnicode_KIND(sub_obj); |
| 9153 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9154 | buf1 = PyUnicode_DATA(str_obj); |
| 9155 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9156 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9157 | if (!buf1) |
| 9158 | goto onError; |
| 9159 | buf2 = PyUnicode_DATA(sub_obj); |
| 9160 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9161 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9162 | if (!buf2) |
| 9163 | goto onError; |
| 9164 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9165 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9166 | |
| 9167 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9168 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9169 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9170 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9171 | result = asciilib_count( |
| 9172 | ((Py_UCS1*)buf1) + start, end - start, |
| 9173 | buf2, len2, PY_SSIZE_T_MAX |
| 9174 | ); |
| 9175 | else |
| 9176 | result = ucs1lib_count( |
| 9177 | ((Py_UCS1*)buf1) + start, end - start, |
| 9178 | buf2, len2, PY_SSIZE_T_MAX |
| 9179 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9180 | break; |
| 9181 | case PyUnicode_2BYTE_KIND: |
| 9182 | result = ucs2lib_count( |
| 9183 | ((Py_UCS2*)buf1) + start, end - start, |
| 9184 | buf2, len2, PY_SSIZE_T_MAX |
| 9185 | ); |
| 9186 | break; |
| 9187 | case PyUnicode_4BYTE_KIND: |
| 9188 | result = ucs4lib_count( |
| 9189 | ((Py_UCS4*)buf1) + start, end - start, |
| 9190 | buf2, len2, PY_SSIZE_T_MAX |
| 9191 | ); |
| 9192 | break; |
| 9193 | default: |
| 9194 | assert(0); result = 0; |
| 9195 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9196 | |
| 9197 | Py_DECREF(sub_obj); |
| 9198 | Py_DECREF(str_obj); |
| 9199 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9200 | if (kind1 != kind) |
| 9201 | PyMem_Free(buf1); |
| 9202 | if (kind2 != kind) |
| 9203 | PyMem_Free(buf2); |
| 9204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9206 | onError: |
| 9207 | Py_DECREF(sub_obj); |
| 9208 | Py_DECREF(str_obj); |
| 9209 | if (kind1 != kind && buf1) |
| 9210 | PyMem_Free(buf1); |
| 9211 | if (kind2 != kind && buf2) |
| 9212 | PyMem_Free(buf2); |
| 9213 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9214 | } |
| 9215 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9216 | Py_ssize_t |
| 9217 | PyUnicode_Find(PyObject *str, |
| 9218 | PyObject *sub, |
| 9219 | Py_ssize_t start, |
| 9220 | Py_ssize_t end, |
| 9221 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9222 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9223 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9226 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9227 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9228 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9229 | if (!sub) { |
| 9230 | Py_DECREF(str); |
| 9231 | return -2; |
| 9232 | } |
| 9233 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9234 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9235 | Py_DECREF(str); |
| 9236 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9237 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9238 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9239 | result = any_find_slice(direction, |
| 9240 | str, sub, start, end |
| 9241 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9243 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9244 | Py_DECREF(sub); |
| 9245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9246 | return result; |
| 9247 | } |
| 9248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9249 | Py_ssize_t |
| 9250 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9251 | Py_ssize_t start, Py_ssize_t end, |
| 9252 | int direction) |
| 9253 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9254 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9255 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | if (PyUnicode_READY(str) == -1) |
| 9257 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9258 | if (start < 0 || end < 0) { |
| 9259 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9260 | return -2; |
| 9261 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9262 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9263 | end = PyUnicode_GET_LENGTH(str); |
| 9264 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9265 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9266 | kind, end-start, ch, direction); |
| 9267 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9268 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9269 | else |
| 9270 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | } |
| 9272 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9273 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9274 | tailmatch(PyObject *self, |
| 9275 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9276 | Py_ssize_t start, |
| 9277 | Py_ssize_t end, |
| 9278 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9279 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9280 | int kind_self; |
| 9281 | int kind_sub; |
| 9282 | void *data_self; |
| 9283 | void *data_sub; |
| 9284 | Py_ssize_t offset; |
| 9285 | Py_ssize_t i; |
| 9286 | Py_ssize_t end_sub; |
| 9287 | |
| 9288 | if (PyUnicode_READY(self) == -1 || |
| 9289 | PyUnicode_READY(substring) == -1) |
| 9290 | return 0; |
| 9291 | |
| 9292 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | return 1; |
| 9294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9296 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9298 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | kind_self = PyUnicode_KIND(self); |
| 9301 | data_self = PyUnicode_DATA(self); |
| 9302 | kind_sub = PyUnicode_KIND(substring); |
| 9303 | data_sub = PyUnicode_DATA(substring); |
| 9304 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9305 | |
| 9306 | if (direction > 0) |
| 9307 | offset = end; |
| 9308 | else |
| 9309 | offset = start; |
| 9310 | |
| 9311 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9312 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9313 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9314 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9315 | /* If both are of the same kind, memcmp is sufficient */ |
| 9316 | if (kind_self == kind_sub) { |
| 9317 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9318 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | data_sub, |
| 9320 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9321 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9322 | } |
| 9323 | /* otherwise we have to compare each character by first accesing it */ |
| 9324 | else { |
| 9325 | /* We do not need to compare 0 and len(substring)-1 because |
| 9326 | the if statement above ensured already that they are equal |
| 9327 | when we end up here. */ |
| 9328 | // TODO: honor direction and do a forward or backwards search |
| 9329 | for (i = 1; i < end_sub; ++i) { |
| 9330 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9331 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9332 | return 0; |
| 9333 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9334 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9335 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | return 0; |
| 9339 | } |
| 9340 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9341 | Py_ssize_t |
| 9342 | PyUnicode_Tailmatch(PyObject *str, |
| 9343 | PyObject *substr, |
| 9344 | Py_ssize_t start, |
| 9345 | Py_ssize_t end, |
| 9346 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9347 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9348 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | str = PyUnicode_FromObject(str); |
| 9351 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | substr = PyUnicode_FromObject(substr); |
| 9354 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9355 | Py_DECREF(str); |
| 9356 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9357 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9358 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9359 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9360 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | Py_DECREF(str); |
| 9362 | Py_DECREF(substr); |
| 9363 | return result; |
| 9364 | } |
| 9365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9366 | /* Apply fixfct filter to the Unicode object self and return a |
| 9367 | reference to the modified object */ |
| 9368 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9369 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9370 | fixup(PyObject *self, |
| 9371 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9373 | PyObject *u; |
| 9374 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9375 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9376 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9377 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9378 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9379 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9380 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | /* fix functions return the new maximum character in a string, |
| 9383 | if the kind of the resulting unicode object does not change, |
| 9384 | everything is fine. Otherwise we need to change the string kind |
| 9385 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9386 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9387 | |
| 9388 | if (maxchar_new == 0) { |
| 9389 | /* no changes */; |
| 9390 | if (PyUnicode_CheckExact(self)) { |
| 9391 | Py_DECREF(u); |
| 9392 | Py_INCREF(self); |
| 9393 | return self; |
| 9394 | } |
| 9395 | else |
| 9396 | return u; |
| 9397 | } |
| 9398 | |
| 9399 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | maxchar_new = 127; |
| 9401 | else if (maxchar_new <= 255) |
| 9402 | maxchar_new = 255; |
| 9403 | else if (maxchar_new <= 65535) |
| 9404 | maxchar_new = 65535; |
| 9405 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9406 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9408 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9409 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9410 | |
| 9411 | /* In case the maximum character changed, we need to |
| 9412 | convert the string to the new category. */ |
| 9413 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9414 | if (v == NULL) { |
| 9415 | Py_DECREF(u); |
| 9416 | return NULL; |
| 9417 | } |
| 9418 | if (maxchar_new > maxchar_old) { |
| 9419 | /* If the maxchar increased so that the kind changed, not all |
| 9420 | characters are representable anymore and we need to fix the |
| 9421 | string again. This only happens in very few cases. */ |
| 9422 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9423 | maxchar_old = fixfct(v); |
| 9424 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | } |
| 9426 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9427 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9428 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9429 | Py_DECREF(u); |
| 9430 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9431 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9434 | static PyObject * |
| 9435 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9436 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9437 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9438 | char *resdata, *data = PyUnicode_DATA(self); |
| 9439 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9440 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9441 | res = PyUnicode_New(len, 127); |
| 9442 | if (res == NULL) |
| 9443 | return NULL; |
| 9444 | resdata = PyUnicode_DATA(res); |
| 9445 | if (lower) |
| 9446 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9448 | _Py_bytes_upper(resdata, data, len); |
| 9449 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9450 | } |
| 9451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9453 | 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] | 9454 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9455 | Py_ssize_t j; |
| 9456 | int final_sigma; |
| 9457 | Py_UCS4 c; |
| 9458 | /* 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] | 9459 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9460 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9461 | |
| 9462 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9463 | */ |
| 9464 | for (j = i - 1; j >= 0; j--) { |
| 9465 | c = PyUnicode_READ(kind, data, j); |
| 9466 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9467 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9468 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9469 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9470 | if (final_sigma) { |
| 9471 | for (j = i + 1; j < length; j++) { |
| 9472 | c = PyUnicode_READ(kind, data, j); |
| 9473 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9474 | break; |
| 9475 | } |
| 9476 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9477 | } |
| 9478 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | } |
| 9480 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9481 | static int |
| 9482 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9483 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9485 | /* Obscure special case. */ |
| 9486 | if (c == 0x3A3) { |
| 9487 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9488 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9490 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9491 | } |
| 9492 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9493 | static Py_ssize_t |
| 9494 | 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] | 9495 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9496 | Py_ssize_t i, k = 0; |
| 9497 | int n_res, j; |
| 9498 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9499 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9500 | c = PyUnicode_READ(kind, data, 0); |
| 9501 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9502 | for (j = 0; j < n_res; j++) { |
| 9503 | if (mapped[j] > *maxchar) |
| 9504 | *maxchar = mapped[j]; |
| 9505 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9507 | for (i = 1; i < length; i++) { |
| 9508 | c = PyUnicode_READ(kind, data, i); |
| 9509 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9510 | for (j = 0; j < n_res; j++) { |
| 9511 | if (mapped[j] > *maxchar) |
| 9512 | *maxchar = mapped[j]; |
| 9513 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9514 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9515 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9516 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9517 | } |
| 9518 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9519 | static Py_ssize_t |
| 9520 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9521 | Py_ssize_t i, k = 0; |
| 9522 | |
| 9523 | for (i = 0; i < length; i++) { |
| 9524 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9525 | int n_res, j; |
| 9526 | if (Py_UNICODE_ISUPPER(c)) { |
| 9527 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9528 | } |
| 9529 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9530 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9531 | } |
| 9532 | else { |
| 9533 | n_res = 1; |
| 9534 | mapped[0] = c; |
| 9535 | } |
| 9536 | for (j = 0; j < n_res; j++) { |
| 9537 | if (mapped[j] > *maxchar) |
| 9538 | *maxchar = mapped[j]; |
| 9539 | res[k++] = mapped[j]; |
| 9540 | } |
| 9541 | } |
| 9542 | return k; |
| 9543 | } |
| 9544 | |
| 9545 | static Py_ssize_t |
| 9546 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9547 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9549 | Py_ssize_t i, k = 0; |
| 9550 | |
| 9551 | for (i = 0; i < length; i++) { |
| 9552 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9553 | int n_res, j; |
| 9554 | if (lower) |
| 9555 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9556 | else |
| 9557 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9558 | for (j = 0; j < n_res; j++) { |
| 9559 | if (mapped[j] > *maxchar) |
| 9560 | *maxchar = mapped[j]; |
| 9561 | res[k++] = mapped[j]; |
| 9562 | } |
| 9563 | } |
| 9564 | return k; |
| 9565 | } |
| 9566 | |
| 9567 | static Py_ssize_t |
| 9568 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9569 | { |
| 9570 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9571 | } |
| 9572 | |
| 9573 | static Py_ssize_t |
| 9574 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9575 | { |
| 9576 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9577 | } |
| 9578 | |
| 9579 | static PyObject * |
| 9580 | case_operation(PyObject *self, |
| 9581 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9582 | { |
| 9583 | PyObject *res = NULL; |
| 9584 | Py_ssize_t length, newlength = 0; |
| 9585 | int kind, outkind; |
| 9586 | void *data, *outdata; |
| 9587 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9588 | |
| 9589 | if (PyUnicode_READY(self) == -1) |
| 9590 | return NULL; |
| 9591 | |
| 9592 | kind = PyUnicode_KIND(self); |
| 9593 | data = PyUnicode_DATA(self); |
| 9594 | length = PyUnicode_GET_LENGTH(self); |
| 9595 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9596 | if (tmp == NULL) |
| 9597 | return PyErr_NoMemory(); |
| 9598 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9599 | res = PyUnicode_New(newlength, maxchar); |
| 9600 | if (res == NULL) |
| 9601 | goto leave; |
| 9602 | tmpend = tmp + newlength; |
| 9603 | outdata = PyUnicode_DATA(res); |
| 9604 | outkind = PyUnicode_KIND(res); |
| 9605 | switch (outkind) { |
| 9606 | case PyUnicode_1BYTE_KIND: |
| 9607 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9608 | break; |
| 9609 | case PyUnicode_2BYTE_KIND: |
| 9610 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9611 | break; |
| 9612 | case PyUnicode_4BYTE_KIND: |
| 9613 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9614 | break; |
| 9615 | default: |
| 9616 | assert(0); |
| 9617 | break; |
| 9618 | } |
| 9619 | leave: |
| 9620 | PyMem_FREE(tmp); |
| 9621 | return res; |
| 9622 | } |
| 9623 | |
| 9624 | static Py_ssize_t |
| 9625 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9626 | { |
| 9627 | Py_ssize_t i, k = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9628 | int previous_is_cased; |
| 9629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9630 | previous_is_cased = 0; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9631 | for (i = 0; i < length; i++) { |
| 9632 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9633 | Py_UCS4 mapped[3]; |
| 9634 | int n_res, j; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9635 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9636 | if (previous_is_cased) |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9637 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9638 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9639 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9640 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9641 | for (j = 0; j < n_res; j++) { |
| 9642 | if (mapped[j] > *maxchar) |
| 9643 | *maxchar = mapped[j]; |
| 9644 | res[k++] = mapped[j]; |
| 9645 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9646 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9647 | previous_is_cased = _PyUnicode_IsCased(c); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9649 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9650 | } |
| 9651 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9652 | PyObject * |
| 9653 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9655 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9656 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9658 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9659 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9660 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9661 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9662 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9663 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9664 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9665 | int use_memcpy; |
| 9666 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9667 | PyObject *last_obj; |
| 9668 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9669 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9670 | fseq = PySequence_Fast(seq, ""); |
| 9671 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9672 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9673 | } |
| 9674 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9675 | /* NOTE: the following code can't call back into Python code, |
| 9676 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9677 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9678 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9679 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9680 | /* If empty sequence, return u"". */ |
| 9681 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9682 | Py_DECREF(fseq); |
| 9683 | Py_INCREF(unicode_empty); |
| 9684 | res = unicode_empty; |
| 9685 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9686 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9687 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9688 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9689 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9690 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9691 | if (seqlen == 1) { |
| 9692 | if (PyUnicode_CheckExact(items[0])) { |
| 9693 | res = items[0]; |
| 9694 | Py_INCREF(res); |
| 9695 | Py_DECREF(fseq); |
| 9696 | return res; |
| 9697 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9698 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9699 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9700 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9701 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9702 | /* Set up sep and seplen */ |
| 9703 | if (separator == NULL) { |
| 9704 | /* fall back to a blank space separator */ |
| 9705 | sep = PyUnicode_FromOrdinal(' '); |
| 9706 | if (!sep) |
| 9707 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9708 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9709 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9710 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9711 | else { |
| 9712 | if (!PyUnicode_Check(separator)) { |
| 9713 | PyErr_Format(PyExc_TypeError, |
| 9714 | "separator: expected str instance," |
| 9715 | " %.80s found", |
| 9716 | Py_TYPE(separator)->tp_name); |
| 9717 | goto onError; |
| 9718 | } |
| 9719 | if (PyUnicode_READY(separator)) |
| 9720 | goto onError; |
| 9721 | sep = separator; |
| 9722 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9723 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9724 | /* inc refcount to keep this code path symmetric with the |
| 9725 | above case of a blank separator */ |
| 9726 | Py_INCREF(sep); |
| 9727 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9728 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9729 | } |
| 9730 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9731 | /* There are at least two things to join, or else we have a subclass |
| 9732 | * of str in the sequence. |
| 9733 | * Do a pre-pass to figure out the total amount of space we'll |
| 9734 | * need (sz), and see whether all argument are strings. |
| 9735 | */ |
| 9736 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9737 | #ifdef Py_DEBUG |
| 9738 | use_memcpy = 0; |
| 9739 | #else |
| 9740 | use_memcpy = 1; |
| 9741 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9742 | for (i = 0; i < seqlen; i++) { |
| 9743 | const Py_ssize_t old_sz = sz; |
| 9744 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9745 | if (!PyUnicode_Check(item)) { |
| 9746 | PyErr_Format(PyExc_TypeError, |
| 9747 | "sequence item %zd: expected str instance," |
| 9748 | " %.80s found", |
| 9749 | i, Py_TYPE(item)->tp_name); |
| 9750 | goto onError; |
| 9751 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9752 | if (PyUnicode_READY(item) == -1) |
| 9753 | goto onError; |
| 9754 | sz += PyUnicode_GET_LENGTH(item); |
| 9755 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9756 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9757 | if (i != 0) |
| 9758 | sz += seplen; |
| 9759 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9760 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9761 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9762 | goto onError; |
| 9763 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9764 | if (use_memcpy && last_obj != NULL) { |
| 9765 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9766 | use_memcpy = 0; |
| 9767 | } |
| 9768 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9769 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9770 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9771 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9772 | if (res == NULL) |
| 9773 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9774 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9775 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9776 | #ifdef Py_DEBUG |
| 9777 | use_memcpy = 0; |
| 9778 | #else |
| 9779 | if (use_memcpy) { |
| 9780 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9781 | kind = PyUnicode_KIND(res); |
| 9782 | if (seplen != 0) |
| 9783 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9784 | } |
| 9785 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9786 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9787 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9788 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9789 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9790 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9791 | if (use_memcpy) { |
| 9792 | Py_MEMCPY(res_data, |
| 9793 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9794 | kind * seplen); |
| 9795 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9796 | } |
| 9797 | else { |
| 9798 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9799 | res_offset += seplen; |
| 9800 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9801 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9802 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9803 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9804 | if (use_memcpy) { |
| 9805 | Py_MEMCPY(res_data, |
| 9806 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9807 | kind * itemlen); |
| 9808 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9809 | } |
| 9810 | else { |
| 9811 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9812 | res_offset += itemlen; |
| 9813 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9814 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9815 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9816 | if (use_memcpy) |
| 9817 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9818 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9819 | else |
| 9820 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9821 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9822 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9823 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9824 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9827 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9828 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9830 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9831 | return NULL; |
| 9832 | } |
| 9833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9834 | #define FILL(kind, data, value, start, length) \ |
| 9835 | do { \ |
| 9836 | Py_ssize_t i_ = 0; \ |
| 9837 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9838 | switch ((kind)) { \ |
| 9839 | case PyUnicode_1BYTE_KIND: { \ |
| 9840 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9841 | memset(to_, (unsigned char)value, length); \ |
| 9842 | break; \ |
| 9843 | } \ |
| 9844 | case PyUnicode_2BYTE_KIND: { \ |
| 9845 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9846 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9847 | break; \ |
| 9848 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9849 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9851 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9852 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9853 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9854 | } \ |
| 9855 | } \ |
| 9856 | } while (0) |
| 9857 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9858 | Py_ssize_t |
| 9859 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9860 | Py_UCS4 fill_char) |
| 9861 | { |
| 9862 | Py_ssize_t maxlen; |
| 9863 | enum PyUnicode_Kind kind; |
| 9864 | void *data; |
| 9865 | |
| 9866 | if (!PyUnicode_Check(unicode)) { |
| 9867 | PyErr_BadInternalCall(); |
| 9868 | return -1; |
| 9869 | } |
| 9870 | if (PyUnicode_READY(unicode) == -1) |
| 9871 | return -1; |
| 9872 | if (unicode_check_modifiable(unicode)) |
| 9873 | return -1; |
| 9874 | |
| 9875 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9876 | PyErr_SetString(PyExc_ValueError, |
| 9877 | "fill character is bigger than " |
| 9878 | "the string maximum character"); |
| 9879 | return -1; |
| 9880 | } |
| 9881 | |
| 9882 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9883 | length = Py_MIN(maxlen, length); |
| 9884 | if (length <= 0) |
| 9885 | return 0; |
| 9886 | |
| 9887 | kind = PyUnicode_KIND(unicode); |
| 9888 | data = PyUnicode_DATA(unicode); |
| 9889 | FILL(kind, data, fill_char, start, length); |
| 9890 | return length; |
| 9891 | } |
| 9892 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9893 | static PyObject * |
| 9894 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9895 | Py_ssize_t left, |
| 9896 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9898 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | PyObject *u; |
| 9900 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9901 | int kind; |
| 9902 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9903 | |
| 9904 | if (left < 0) |
| 9905 | left = 0; |
| 9906 | if (right < 0) |
| 9907 | right = 0; |
| 9908 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9909 | if (left == 0 && right == 0) |
| 9910 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9913 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9914 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9915 | return NULL; |
| 9916 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9918 | if (fill > maxchar) |
| 9919 | maxchar = fill; |
| 9920 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9921 | if (!u) |
| 9922 | return NULL; |
| 9923 | |
| 9924 | kind = PyUnicode_KIND(u); |
| 9925 | data = PyUnicode_DATA(u); |
| 9926 | if (left) |
| 9927 | FILL(kind, data, fill, 0, left); |
| 9928 | if (right) |
| 9929 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9930 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9931 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9932 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9933 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9935 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9936 | PyObject * |
| 9937 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9938 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9939 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | |
| 9941 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9942 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9943 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9944 | if (PyUnicode_READY(string) == -1) { |
| 9945 | Py_DECREF(string); |
| 9946 | return NULL; |
| 9947 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9949 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9951 | if (PyUnicode_IS_ASCII(string)) |
| 9952 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9953 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9954 | PyUnicode_GET_LENGTH(string), keepends); |
| 9955 | else |
| 9956 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9957 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9958 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | break; |
| 9960 | case PyUnicode_2BYTE_KIND: |
| 9961 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9962 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | PyUnicode_GET_LENGTH(string), keepends); |
| 9964 | break; |
| 9965 | case PyUnicode_4BYTE_KIND: |
| 9966 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9967 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9968 | PyUnicode_GET_LENGTH(string), keepends); |
| 9969 | break; |
| 9970 | default: |
| 9971 | assert(0); |
| 9972 | list = 0; |
| 9973 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9974 | Py_DECREF(string); |
| 9975 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | } |
| 9977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9978 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9979 | split(PyObject *self, |
| 9980 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9981 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9982 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | int kind1, kind2, kind; |
| 9984 | void *buf1, *buf2; |
| 9985 | Py_ssize_t len1, len2; |
| 9986 | PyObject* out; |
| 9987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9988 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9989 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | if (PyUnicode_READY(self) == -1) |
| 9992 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9993 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9994 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9995 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9996 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9997 | if (PyUnicode_IS_ASCII(self)) |
| 9998 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9999 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10000 | PyUnicode_GET_LENGTH(self), maxcount |
| 10001 | ); |
| 10002 | else |
| 10003 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10004 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10005 | PyUnicode_GET_LENGTH(self), maxcount |
| 10006 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | case PyUnicode_2BYTE_KIND: |
| 10008 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10009 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10010 | PyUnicode_GET_LENGTH(self), maxcount |
| 10011 | ); |
| 10012 | case PyUnicode_4BYTE_KIND: |
| 10013 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10014 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | PyUnicode_GET_LENGTH(self), maxcount |
| 10016 | ); |
| 10017 | default: |
| 10018 | assert(0); |
| 10019 | return NULL; |
| 10020 | } |
| 10021 | |
| 10022 | if (PyUnicode_READY(substring) == -1) |
| 10023 | return NULL; |
| 10024 | |
| 10025 | kind1 = PyUnicode_KIND(self); |
| 10026 | kind2 = PyUnicode_KIND(substring); |
| 10027 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10028 | buf1 = PyUnicode_DATA(self); |
| 10029 | buf2 = PyUnicode_DATA(substring); |
| 10030 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10031 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | if (!buf1) |
| 10033 | return NULL; |
| 10034 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10035 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10036 | if (!buf2) { |
| 10037 | if (kind1 != kind) PyMem_Free(buf1); |
| 10038 | return NULL; |
| 10039 | } |
| 10040 | len1 = PyUnicode_GET_LENGTH(self); |
| 10041 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10042 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10043 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10045 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10046 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10047 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10048 | else |
| 10049 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10050 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | break; |
| 10052 | case PyUnicode_2BYTE_KIND: |
| 10053 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10054 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10055 | break; |
| 10056 | case PyUnicode_4BYTE_KIND: |
| 10057 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10058 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | break; |
| 10060 | default: |
| 10061 | out = NULL; |
| 10062 | } |
| 10063 | if (kind1 != kind) |
| 10064 | PyMem_Free(buf1); |
| 10065 | if (kind2 != kind) |
| 10066 | PyMem_Free(buf2); |
| 10067 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10068 | } |
| 10069 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10070 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10071 | rsplit(PyObject *self, |
| 10072 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10073 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10074 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | int kind1, kind2, kind; |
| 10076 | void *buf1, *buf2; |
| 10077 | Py_ssize_t len1, len2; |
| 10078 | PyObject* out; |
| 10079 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10080 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10081 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10083 | if (PyUnicode_READY(self) == -1) |
| 10084 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10085 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10087 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10089 | if (PyUnicode_IS_ASCII(self)) |
| 10090 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10091 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10092 | PyUnicode_GET_LENGTH(self), maxcount |
| 10093 | ); |
| 10094 | else |
| 10095 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10096 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10097 | PyUnicode_GET_LENGTH(self), maxcount |
| 10098 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10099 | case PyUnicode_2BYTE_KIND: |
| 10100 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10101 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | PyUnicode_GET_LENGTH(self), maxcount |
| 10103 | ); |
| 10104 | case PyUnicode_4BYTE_KIND: |
| 10105 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10106 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | PyUnicode_GET_LENGTH(self), maxcount |
| 10108 | ); |
| 10109 | default: |
| 10110 | assert(0); |
| 10111 | return NULL; |
| 10112 | } |
| 10113 | |
| 10114 | if (PyUnicode_READY(substring) == -1) |
| 10115 | return NULL; |
| 10116 | |
| 10117 | kind1 = PyUnicode_KIND(self); |
| 10118 | kind2 = PyUnicode_KIND(substring); |
| 10119 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10120 | buf1 = PyUnicode_DATA(self); |
| 10121 | buf2 = PyUnicode_DATA(substring); |
| 10122 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10123 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10124 | if (!buf1) |
| 10125 | return NULL; |
| 10126 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10127 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | if (!buf2) { |
| 10129 | if (kind1 != kind) PyMem_Free(buf1); |
| 10130 | return NULL; |
| 10131 | } |
| 10132 | len1 = PyUnicode_GET_LENGTH(self); |
| 10133 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10134 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10135 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10137 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10138 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10139 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10140 | else |
| 10141 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10142 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | break; |
| 10144 | case PyUnicode_2BYTE_KIND: |
| 10145 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10146 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10147 | break; |
| 10148 | case PyUnicode_4BYTE_KIND: |
| 10149 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10150 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | break; |
| 10152 | default: |
| 10153 | out = NULL; |
| 10154 | } |
| 10155 | if (kind1 != kind) |
| 10156 | PyMem_Free(buf1); |
| 10157 | if (kind2 != kind) |
| 10158 | PyMem_Free(buf2); |
| 10159 | return out; |
| 10160 | } |
| 10161 | |
| 10162 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10163 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10164 | 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] | 10165 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10166 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10167 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10168 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10169 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10170 | else |
| 10171 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | case PyUnicode_2BYTE_KIND: |
| 10173 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10174 | case PyUnicode_4BYTE_KIND: |
| 10175 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10176 | } |
| 10177 | assert(0); |
| 10178 | return -1; |
| 10179 | } |
| 10180 | |
| 10181 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10182 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10183 | 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] | 10184 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10185 | switch (kind) { |
| 10186 | case PyUnicode_1BYTE_KIND: |
| 10187 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10188 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10189 | else |
| 10190 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10191 | case PyUnicode_2BYTE_KIND: |
| 10192 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10193 | case PyUnicode_4BYTE_KIND: |
| 10194 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10195 | } |
| 10196 | assert(0); |
| 10197 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10198 | } |
| 10199 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10200 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10201 | replace(PyObject *self, PyObject *str1, |
| 10202 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10203 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10204 | PyObject *u; |
| 10205 | char *sbuf = PyUnicode_DATA(self); |
| 10206 | char *buf1 = PyUnicode_DATA(str1); |
| 10207 | char *buf2 = PyUnicode_DATA(str2); |
| 10208 | int srelease = 0, release1 = 0, release2 = 0; |
| 10209 | int skind = PyUnicode_KIND(self); |
| 10210 | int kind1 = PyUnicode_KIND(str1); |
| 10211 | int kind2 = PyUnicode_KIND(str2); |
| 10212 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10213 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10214 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10215 | int mayshrink; |
| 10216 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10217 | |
| 10218 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10219 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10221 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10223 | if (str1 == str2) |
| 10224 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | if (skind < kind1) |
| 10226 | /* substring too wide to be present */ |
| 10227 | goto nothing; |
| 10228 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10229 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10230 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10231 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10232 | result string. */ |
| 10233 | mayshrink = (maxchar_str2 < maxchar); |
| 10234 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10235 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10237 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10239 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10240 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10241 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10242 | Py_UCS4 u1, u2; |
| 10243 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10244 | Py_ssize_t index, pos; |
| 10245 | char *src; |
| 10246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10248 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10249 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10250 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10252 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10253 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10254 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10255 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10256 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10257 | |
| 10258 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10259 | index = 0; |
| 10260 | src = sbuf; |
| 10261 | while (--maxcount) |
| 10262 | { |
| 10263 | pos++; |
| 10264 | src += pos * PyUnicode_KIND(self); |
| 10265 | slen -= pos; |
| 10266 | index += pos; |
| 10267 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10268 | if (pos < 0) |
| 10269 | break; |
| 10270 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10271 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10272 | } |
| 10273 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10274 | int rkind = skind; |
| 10275 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10276 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10278 | if (kind1 < rkind) { |
| 10279 | /* widen substring */ |
| 10280 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10281 | if (!buf1) goto error; |
| 10282 | release1 = 1; |
| 10283 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10284 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10285 | if (i < 0) |
| 10286 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | if (rkind > kind2) { |
| 10288 | /* widen replacement */ |
| 10289 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10290 | if (!buf2) goto error; |
| 10291 | release2 = 1; |
| 10292 | } |
| 10293 | else if (rkind < kind2) { |
| 10294 | /* widen self and buf1 */ |
| 10295 | rkind = kind2; |
| 10296 | if (release1) PyMem_Free(buf1); |
| 10297 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10298 | if (!sbuf) goto error; |
| 10299 | srelease = 1; |
| 10300 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10301 | if (!buf1) goto error; |
| 10302 | release1 = 1; |
| 10303 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10304 | u = PyUnicode_New(slen, maxchar); |
| 10305 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10306 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10307 | assert(PyUnicode_KIND(u) == rkind); |
| 10308 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10309 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10310 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10311 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10312 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10313 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10314 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10316 | |
| 10317 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10318 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10319 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10320 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10321 | if (i == -1) |
| 10322 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10323 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10325 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10326 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10327 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10328 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10329 | } |
| 10330 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | Py_ssize_t n, i, j, ires; |
| 10332 | Py_ssize_t product, new_size; |
| 10333 | int rkind = skind; |
| 10334 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10336 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10337 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10339 | if (!buf1) goto error; |
| 10340 | release1 = 1; |
| 10341 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10342 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10343 | if (n == 0) |
| 10344 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10345 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10346 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10348 | if (!buf2) goto error; |
| 10349 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10350 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10352 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | rkind = kind2; |
| 10354 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10355 | if (!sbuf) goto error; |
| 10356 | srelease = 1; |
| 10357 | if (release1) PyMem_Free(buf1); |
| 10358 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10359 | if (!buf1) goto error; |
| 10360 | release1 = 1; |
| 10361 | } |
| 10362 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10363 | PyUnicode_GET_LENGTH(str1))); */ |
| 10364 | product = n * (len2-len1); |
| 10365 | if ((product / (len2-len1)) != n) { |
| 10366 | PyErr_SetString(PyExc_OverflowError, |
| 10367 | "replace string is too long"); |
| 10368 | goto error; |
| 10369 | } |
| 10370 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10371 | if (new_size == 0) { |
| 10372 | Py_INCREF(unicode_empty); |
| 10373 | u = unicode_empty; |
| 10374 | goto done; |
| 10375 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10376 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10377 | PyErr_SetString(PyExc_OverflowError, |
| 10378 | "replace string is too long"); |
| 10379 | goto error; |
| 10380 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10381 | u = PyUnicode_New(new_size, maxchar); |
| 10382 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10384 | assert(PyUnicode_KIND(u) == rkind); |
| 10385 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10386 | ires = i = 0; |
| 10387 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10388 | while (n-- > 0) { |
| 10389 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10390 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10391 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10392 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10393 | if (j == -1) |
| 10394 | break; |
| 10395 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10396 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10397 | memcpy(res + rkind * ires, |
| 10398 | sbuf + rkind * i, |
| 10399 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10400 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10401 | } |
| 10402 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10403 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10404 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10405 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10406 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10408 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10409 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10410 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10411 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10412 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10413 | memcpy(res + rkind * ires, |
| 10414 | sbuf + rkind * i, |
| 10415 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10416 | } |
| 10417 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10418 | /* interleave */ |
| 10419 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10420 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10422 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10423 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10424 | if (--n <= 0) |
| 10425 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10426 | memcpy(res + rkind * ires, |
| 10427 | sbuf + rkind * i, |
| 10428 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10429 | ires++; |
| 10430 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10431 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10432 | memcpy(res + rkind * ires, |
| 10433 | sbuf + rkind * i, |
| 10434 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10435 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10436 | } |
| 10437 | |
| 10438 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10439 | unicode_adjust_maxchar(&u); |
| 10440 | if (u == NULL) |
| 10441 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10443 | |
| 10444 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | if (srelease) |
| 10446 | PyMem_FREE(sbuf); |
| 10447 | if (release1) |
| 10448 | PyMem_FREE(buf1); |
| 10449 | if (release2) |
| 10450 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10451 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10453 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10454 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10455 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10456 | if (srelease) |
| 10457 | PyMem_FREE(sbuf); |
| 10458 | if (release1) |
| 10459 | PyMem_FREE(buf1); |
| 10460 | if (release2) |
| 10461 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10462 | return unicode_result_unchanged(self); |
| 10463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10464 | error: |
| 10465 | if (srelease && sbuf) |
| 10466 | PyMem_FREE(sbuf); |
| 10467 | if (release1 && buf1) |
| 10468 | PyMem_FREE(buf1); |
| 10469 | if (release2 && buf2) |
| 10470 | PyMem_FREE(buf2); |
| 10471 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | } |
| 10473 | |
| 10474 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10475 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10476 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10477 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | \n\ |
| 10479 | 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] | 10480 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10481 | |
| 10482 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10483 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10485 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10486 | } |
| 10487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10488 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10489 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | \n\ |
| 10491 | 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] | 10492 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | |
| 10494 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10495 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10496 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10497 | if (PyUnicode_READY(self) == -1) |
| 10498 | return NULL; |
| 10499 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10500 | return unicode_result_unchanged(self); |
| 10501 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10502 | } |
| 10503 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10504 | /* Argument converter. Coerces to a single unicode character */ |
| 10505 | |
| 10506 | static int |
| 10507 | convert_uc(PyObject *obj, void *addr) |
| 10508 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10510 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10511 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10512 | uniobj = PyUnicode_FromObject(obj); |
| 10513 | if (uniobj == NULL) { |
| 10514 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10515 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10516 | return 0; |
| 10517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10518 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10519 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10520 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10521 | Py_DECREF(uniobj); |
| 10522 | return 0; |
| 10523 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10524 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10525 | Py_DECREF(uniobj); |
| 10526 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10529 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10530 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10531 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10532 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10533 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10534 | |
| 10535 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10536 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10537 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10538 | Py_ssize_t marg, left; |
| 10539 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10540 | Py_UCS4 fillchar = ' '; |
| 10541 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10542 | 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] | 10543 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10544 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10545 | if (PyUnicode_READY(self) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10546 | return NULL; |
| 10547 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10548 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10549 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10550 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10551 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | left = marg / 2 + (marg & width & 1); |
| 10553 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10554 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10555 | } |
| 10556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10557 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10558 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10559 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10560 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | int kind1, kind2; |
| 10563 | void *data1, *data2; |
| 10564 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | kind1 = PyUnicode_KIND(str1); |
| 10567 | kind2 = PyUnicode_KIND(str2); |
| 10568 | data1 = PyUnicode_DATA(str1); |
| 10569 | data2 = PyUnicode_DATA(str2); |
| 10570 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10571 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10572 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10574 | Py_UCS4 c1, c2; |
| 10575 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10576 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10577 | |
| 10578 | if (c1 != c2) |
| 10579 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10580 | } |
| 10581 | |
| 10582 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10583 | } |
| 10584 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10585 | int |
| 10586 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10587 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10588 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10589 | if (PyUnicode_READY(left) == -1 || |
| 10590 | PyUnicode_READY(right) == -1) |
| 10591 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10592 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10593 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10594 | PyErr_Format(PyExc_TypeError, |
| 10595 | "Can't compare %.100s and %.100s", |
| 10596 | left->ob_type->tp_name, |
| 10597 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | return -1; |
| 10599 | } |
| 10600 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10601 | int |
| 10602 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10603 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10604 | Py_ssize_t i; |
| 10605 | int kind; |
| 10606 | void *data; |
| 10607 | Py_UCS4 chr; |
| 10608 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10609 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10610 | if (PyUnicode_READY(uni) == -1) |
| 10611 | return -1; |
| 10612 | kind = PyUnicode_KIND(uni); |
| 10613 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10614 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10616 | if (chr != str[i]) |
| 10617 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10618 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10619 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10620 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10621 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10622 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10623 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10624 | return 0; |
| 10625 | } |
| 10626 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10628 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10629 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10631 | PyObject * |
| 10632 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10633 | { |
| 10634 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10635 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10636 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10637 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | if (PyUnicode_READY(left) == -1 || |
| 10639 | PyUnicode_READY(right) == -1) |
| 10640 | return NULL; |
| 10641 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10642 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10643 | if (op == Py_EQ) { |
| 10644 | Py_INCREF(Py_False); |
| 10645 | return Py_False; |
| 10646 | } |
| 10647 | if (op == Py_NE) { |
| 10648 | Py_INCREF(Py_True); |
| 10649 | return Py_True; |
| 10650 | } |
| 10651 | } |
| 10652 | if (left == right) |
| 10653 | result = 0; |
| 10654 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10655 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10656 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10657 | /* Convert the return value to a Boolean */ |
| 10658 | switch (op) { |
| 10659 | case Py_EQ: |
| 10660 | v = TEST_COND(result == 0); |
| 10661 | break; |
| 10662 | case Py_NE: |
| 10663 | v = TEST_COND(result != 0); |
| 10664 | break; |
| 10665 | case Py_LE: |
| 10666 | v = TEST_COND(result <= 0); |
| 10667 | break; |
| 10668 | case Py_GE: |
| 10669 | v = TEST_COND(result >= 0); |
| 10670 | break; |
| 10671 | case Py_LT: |
| 10672 | v = TEST_COND(result == -1); |
| 10673 | break; |
| 10674 | case Py_GT: |
| 10675 | v = TEST_COND(result == 1); |
| 10676 | break; |
| 10677 | default: |
| 10678 | PyErr_BadArgument(); |
| 10679 | return NULL; |
| 10680 | } |
| 10681 | Py_INCREF(v); |
| 10682 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10683 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10684 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10685 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10686 | } |
| 10687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10688 | int |
| 10689 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10690 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10691 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10692 | int kind1, kind2, kind; |
| 10693 | void *buf1, *buf2; |
| 10694 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10695 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10696 | |
| 10697 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10698 | sub = PyUnicode_FromObject(element); |
| 10699 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10700 | PyErr_Format(PyExc_TypeError, |
| 10701 | "'in <string>' requires string as left operand, not %s", |
| 10702 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10703 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10704 | } |
| 10705 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10706 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10707 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10708 | Py_DECREF(sub); |
| 10709 | return -1; |
| 10710 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10711 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10712 | Py_DECREF(sub); |
| 10713 | Py_DECREF(str); |
| 10714 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10716 | kind1 = PyUnicode_KIND(str); |
| 10717 | kind2 = PyUnicode_KIND(sub); |
| 10718 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10719 | buf1 = PyUnicode_DATA(str); |
| 10720 | buf2 = PyUnicode_DATA(sub); |
| 10721 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10722 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10723 | if (!buf1) { |
| 10724 | Py_DECREF(sub); |
| 10725 | return -1; |
| 10726 | } |
| 10727 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10728 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10729 | if (!buf2) { |
| 10730 | Py_DECREF(sub); |
| 10731 | if (kind1 != kind) PyMem_Free(buf1); |
| 10732 | return -1; |
| 10733 | } |
| 10734 | len1 = PyUnicode_GET_LENGTH(str); |
| 10735 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10736 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10737 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | case PyUnicode_1BYTE_KIND: |
| 10739 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10740 | break; |
| 10741 | case PyUnicode_2BYTE_KIND: |
| 10742 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10743 | break; |
| 10744 | case PyUnicode_4BYTE_KIND: |
| 10745 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10746 | break; |
| 10747 | default: |
| 10748 | result = -1; |
| 10749 | assert(0); |
| 10750 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10751 | |
| 10752 | Py_DECREF(str); |
| 10753 | Py_DECREF(sub); |
| 10754 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10755 | if (kind1 != kind) |
| 10756 | PyMem_Free(buf1); |
| 10757 | if (kind2 != kind) |
| 10758 | PyMem_Free(buf2); |
| 10759 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10760 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10761 | } |
| 10762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10764 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10765 | PyObject * |
| 10766 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10769 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10770 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | |
| 10772 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10774 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10775 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10776 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10777 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10778 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10779 | |
| 10780 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10781 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10782 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10785 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10786 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10787 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10788 | } |
| 10789 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10790 | u_len = PyUnicode_GET_LENGTH(u); |
| 10791 | v_len = PyUnicode_GET_LENGTH(v); |
| 10792 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10793 | PyErr_SetString(PyExc_OverflowError, |
| 10794 | "strings are too large to concat"); |
| 10795 | goto onError; |
| 10796 | } |
| 10797 | new_len = u_len + v_len; |
| 10798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10799 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10800 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10801 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10803 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10804 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10805 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10806 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10807 | copy_characters(w, 0, u, 0, u_len); |
| 10808 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10809 | Py_DECREF(u); |
| 10810 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10811 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10812 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10814 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | Py_XDECREF(u); |
| 10816 | Py_XDECREF(v); |
| 10817 | return NULL; |
| 10818 | } |
| 10819 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10820 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10821 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10822 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10823 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10824 | Py_UCS4 maxchar, maxchar2; |
| 10825 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10826 | |
| 10827 | if (p_left == NULL) { |
| 10828 | if (!PyErr_Occurred()) |
| 10829 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10830 | return; |
| 10831 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10832 | left = *p_left; |
| 10833 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10834 | if (!PyErr_Occurred()) |
| 10835 | PyErr_BadInternalCall(); |
| 10836 | goto error; |
| 10837 | } |
| 10838 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10839 | if (PyUnicode_READY(left)) |
| 10840 | goto error; |
| 10841 | if (PyUnicode_READY(right)) |
| 10842 | goto error; |
| 10843 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10844 | /* Shortcuts */ |
| 10845 | if (left == unicode_empty) { |
| 10846 | Py_DECREF(left); |
| 10847 | Py_INCREF(right); |
| 10848 | *p_left = right; |
| 10849 | return; |
| 10850 | } |
| 10851 | if (right == unicode_empty) |
| 10852 | return; |
| 10853 | |
| 10854 | left_len = PyUnicode_GET_LENGTH(left); |
| 10855 | right_len = PyUnicode_GET_LENGTH(right); |
| 10856 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10857 | PyErr_SetString(PyExc_OverflowError, |
| 10858 | "strings are too large to concat"); |
| 10859 | goto error; |
| 10860 | } |
| 10861 | new_len = left_len + right_len; |
| 10862 | |
| 10863 | if (unicode_modifiable(left) |
| 10864 | && PyUnicode_CheckExact(right) |
| 10865 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10866 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10867 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10868 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10869 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10870 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10871 | { |
| 10872 | /* append inplace */ |
| 10873 | if (unicode_resize(p_left, new_len) != 0) { |
| 10874 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10875 | * deallocated so it cannot be put back into |
| 10876 | * 'variable'. The MemoryError is raised when there |
| 10877 | * is no value in 'variable', which might (very |
| 10878 | * remotely) be a cause of incompatibilities. |
| 10879 | */ |
| 10880 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10881 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10882 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10883 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10884 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10885 | else { |
| 10886 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10887 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 10888 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10889 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10890 | /* Concat the two Unicode strings */ |
| 10891 | res = PyUnicode_New(new_len, maxchar); |
| 10892 | if (res == NULL) |
| 10893 | goto error; |
| 10894 | copy_characters(res, 0, left, 0, left_len); |
| 10895 | copy_characters(res, left_len, right, 0, right_len); |
| 10896 | Py_DECREF(left); |
| 10897 | *p_left = res; |
| 10898 | } |
| 10899 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10900 | return; |
| 10901 | |
| 10902 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10903 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10904 | } |
| 10905 | |
| 10906 | void |
| 10907 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10908 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10909 | PyUnicode_Append(pleft, right); |
| 10910 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10911 | } |
| 10912 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10913 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10914 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10915 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10916 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10917 | 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] | 10918 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10919 | |
| 10920 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10921 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10923 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10924 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10925 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10927 | int kind1, kind2, kind; |
| 10928 | void *buf1, *buf2; |
| 10929 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10930 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10931 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10932 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10933 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10934 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10935 | kind1 = PyUnicode_KIND(self); |
| 10936 | kind2 = PyUnicode_KIND(substring); |
| 10937 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10938 | buf1 = PyUnicode_DATA(self); |
| 10939 | buf2 = PyUnicode_DATA(substring); |
| 10940 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10941 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10942 | if (!buf1) { |
| 10943 | Py_DECREF(substring); |
| 10944 | return NULL; |
| 10945 | } |
| 10946 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10947 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10948 | if (!buf2) { |
| 10949 | Py_DECREF(substring); |
| 10950 | if (kind1 != kind) PyMem_Free(buf1); |
| 10951 | return NULL; |
| 10952 | } |
| 10953 | len1 = PyUnicode_GET_LENGTH(self); |
| 10954 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10955 | |
| 10956 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10957 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10958 | case PyUnicode_1BYTE_KIND: |
| 10959 | iresult = ucs1lib_count( |
| 10960 | ((Py_UCS1*)buf1) + start, end - start, |
| 10961 | buf2, len2, PY_SSIZE_T_MAX |
| 10962 | ); |
| 10963 | break; |
| 10964 | case PyUnicode_2BYTE_KIND: |
| 10965 | iresult = ucs2lib_count( |
| 10966 | ((Py_UCS2*)buf1) + start, end - start, |
| 10967 | buf2, len2, PY_SSIZE_T_MAX |
| 10968 | ); |
| 10969 | break; |
| 10970 | case PyUnicode_4BYTE_KIND: |
| 10971 | iresult = ucs4lib_count( |
| 10972 | ((Py_UCS4*)buf1) + start, end - start, |
| 10973 | buf2, len2, PY_SSIZE_T_MAX |
| 10974 | ); |
| 10975 | break; |
| 10976 | default: |
| 10977 | assert(0); iresult = 0; |
| 10978 | } |
| 10979 | |
| 10980 | result = PyLong_FromSsize_t(iresult); |
| 10981 | |
| 10982 | if (kind1 != kind) |
| 10983 | PyMem_Free(buf1); |
| 10984 | if (kind2 != kind) |
| 10985 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | |
| 10987 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | return result; |
| 10990 | } |
| 10991 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10992 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10993 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10995 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10996 | 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] | 10997 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10998 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10999 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11000 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | |
| 11002 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11003 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11005 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | char *encoding = NULL; |
| 11007 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11008 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11009 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11010 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11011 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11012 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11013 | } |
| 11014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11015 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | \n\ |
| 11018 | 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] | 11019 | 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] | 11020 | |
| 11021 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11022 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11024 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11025 | Py_UCS4 ch; |
| 11026 | PyObject *u; |
| 11027 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11029 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11030 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | |
| 11032 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11033 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11035 | if (PyUnicode_READY(self) == -1) |
| 11036 | return NULL; |
| 11037 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11038 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11039 | src_len = PyUnicode_GET_LENGTH(self); |
| 11040 | i = j = line_pos = 0; |
| 11041 | kind = PyUnicode_KIND(self); |
| 11042 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11043 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11044 | for (; i < src_len; i++) { |
| 11045 | ch = PyUnicode_READ(kind, src_data, i); |
| 11046 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11047 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11048 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11049 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11050 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11051 | goto overflow; |
| 11052 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11053 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11054 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11055 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11056 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11057 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11058 | goto overflow; |
| 11059 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11061 | if (ch == '\n' || ch == '\r') |
| 11062 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11064 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11065 | if (!found) |
| 11066 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11069 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | if (!u) |
| 11071 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11072 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11073 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11074 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11075 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11076 | for (; i < src_len; i++) { |
| 11077 | ch = PyUnicode_READ(kind, src_data, i); |
| 11078 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11079 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11080 | incr = tabsize - (line_pos % tabsize); |
| 11081 | line_pos += incr; |
| 11082 | while (incr--) { |
| 11083 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11084 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11085 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11086 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11087 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11088 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11089 | line_pos++; |
| 11090 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11091 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11092 | if (ch == '\n' || ch == '\r') |
| 11093 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11095 | } |
| 11096 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11097 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11098 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11099 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11100 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11101 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11102 | } |
| 11103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11104 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11105 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11106 | \n\ |
| 11107 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11108 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | arguments start and end are interpreted as in slice notation.\n\ |
| 11110 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11111 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11112 | |
| 11113 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11114 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11116 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11117 | Py_ssize_t start; |
| 11118 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11119 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11121 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11122 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11124 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11125 | if (PyUnicode_READY(self) == -1) |
| 11126 | return NULL; |
| 11127 | if (PyUnicode_READY(substring) == -1) |
| 11128 | return NULL; |
| 11129 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11130 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11131 | |
| 11132 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | if (result == -2) |
| 11135 | return NULL; |
| 11136 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11137 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | } |
| 11139 | |
| 11140 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11141 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11143 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11144 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11146 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11147 | } |
| 11148 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11149 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11150 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11151 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11152 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11153 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11154 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11155 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11156 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11157 | if (_PyUnicode_HASH(self) != -1) |
| 11158 | return _PyUnicode_HASH(self); |
| 11159 | if (PyUnicode_READY(self) == -1) |
| 11160 | return -1; |
| 11161 | len = PyUnicode_GET_LENGTH(self); |
| 11162 | |
| 11163 | /* The hash function as a macro, gets expanded three times below. */ |
| 11164 | #define HASH(P) \ |
| 11165 | x = (Py_uhash_t)*P << 7; \ |
| 11166 | while (--len >= 0) \ |
| 11167 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11168 | |
| 11169 | switch (PyUnicode_KIND(self)) { |
| 11170 | case PyUnicode_1BYTE_KIND: { |
| 11171 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11172 | HASH(c); |
| 11173 | break; |
| 11174 | } |
| 11175 | case PyUnicode_2BYTE_KIND: { |
| 11176 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11177 | HASH(s); |
| 11178 | break; |
| 11179 | } |
| 11180 | default: { |
| 11181 | Py_UCS4 *l; |
| 11182 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11183 | "Impossible switch case in unicode_hash"); |
| 11184 | l = PyUnicode_4BYTE_DATA(self); |
| 11185 | HASH(l); |
| 11186 | break; |
| 11187 | } |
| 11188 | } |
| 11189 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11190 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11191 | if (x == -1) |
| 11192 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11194 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11198 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11199 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11201 | 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] | 11202 | |
| 11203 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11204 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11206 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11207 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11208 | Py_ssize_t start; |
| 11209 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11211 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11212 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11214 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11215 | if (PyUnicode_READY(self) == -1) |
| 11216 | return NULL; |
| 11217 | if (PyUnicode_READY(substring) == -1) |
| 11218 | return NULL; |
| 11219 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11220 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
| 11222 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11223 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11224 | if (result == -2) |
| 11225 | return NULL; |
| 11226 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | if (result < 0) { |
| 11228 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11229 | return NULL; |
| 11230 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11231 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11232 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | } |
| 11234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11235 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11236 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11237 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11238 | 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] | 11239 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | |
| 11241 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11242 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11244 | Py_ssize_t i, length; |
| 11245 | int kind; |
| 11246 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | int cased; |
| 11248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11249 | if (PyUnicode_READY(self) == -1) |
| 11250 | return NULL; |
| 11251 | length = PyUnicode_GET_LENGTH(self); |
| 11252 | kind = PyUnicode_KIND(self); |
| 11253 | data = PyUnicode_DATA(self); |
| 11254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | if (length == 1) |
| 11257 | return PyBool_FromLong( |
| 11258 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11260 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11261 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11265 | for (i = 0; i < length; i++) { |
| 11266 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11267 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11268 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11269 | return PyBool_FromLong(0); |
| 11270 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11271 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11273 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11274 | } |
| 11275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11276 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11277 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11279 | 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] | 11280 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | |
| 11282 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11283 | unicode_isupper(PyObject *self) |
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 | Py_ssize_t i, length; |
| 11286 | int kind; |
| 11287 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11288 | int cased; |
| 11289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11290 | if (PyUnicode_READY(self) == -1) |
| 11291 | return NULL; |
| 11292 | length = PyUnicode_GET_LENGTH(self); |
| 11293 | kind = PyUnicode_KIND(self); |
| 11294 | data = PyUnicode_DATA(self); |
| 11295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | if (length == 1) |
| 11298 | return PyBool_FromLong( |
| 11299 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11301 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11305 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | for (i = 0; i < length; i++) { |
| 11307 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11308 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11309 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11310 | return PyBool_FromLong(0); |
| 11311 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11312 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11314 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | } |
| 11316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11317 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11318 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11320 | Return True if S is a titlecased string and there is at least one\n\ |
| 11321 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11322 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11323 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | |
| 11325 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11326 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11327 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11328 | Py_ssize_t i, length; |
| 11329 | int kind; |
| 11330 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | int cased, previous_is_cased; |
| 11332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | if (PyUnicode_READY(self) == -1) |
| 11334 | return NULL; |
| 11335 | length = PyUnicode_GET_LENGTH(self); |
| 11336 | kind = PyUnicode_KIND(self); |
| 11337 | data = PyUnicode_DATA(self); |
| 11338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11340 | if (length == 1) { |
| 11341 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11342 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11343 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11344 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11346 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11347 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11348 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | cased = 0; |
| 11351 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | for (i = 0; i < length; i++) { |
| 11353 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11355 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11356 | if (previous_is_cased) |
| 11357 | return PyBool_FromLong(0); |
| 11358 | previous_is_cased = 1; |
| 11359 | cased = 1; |
| 11360 | } |
| 11361 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11362 | if (!previous_is_cased) |
| 11363 | return PyBool_FromLong(0); |
| 11364 | previous_is_cased = 1; |
| 11365 | cased = 1; |
| 11366 | } |
| 11367 | else |
| 11368 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11370 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | } |
| 11372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11373 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11374 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11376 | Return True if all characters in S are whitespace\n\ |
| 11377 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11378 | |
| 11379 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11380 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11382 | Py_ssize_t i, length; |
| 11383 | int kind; |
| 11384 | void *data; |
| 11385 | |
| 11386 | if (PyUnicode_READY(self) == -1) |
| 11387 | return NULL; |
| 11388 | length = PyUnicode_GET_LENGTH(self); |
| 11389 | kind = PyUnicode_KIND(self); |
| 11390 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | if (length == 1) |
| 11394 | return PyBool_FromLong( |
| 11395 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11397 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11398 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11399 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | for (i = 0; i < length; i++) { |
| 11402 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11403 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11404 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11406 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | } |
| 11408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11409 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11410 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11411 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11412 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11413 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11414 | |
| 11415 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11416 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11417 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11418 | Py_ssize_t i, length; |
| 11419 | int kind; |
| 11420 | void *data; |
| 11421 | |
| 11422 | if (PyUnicode_READY(self) == -1) |
| 11423 | return NULL; |
| 11424 | length = PyUnicode_GET_LENGTH(self); |
| 11425 | kind = PyUnicode_KIND(self); |
| 11426 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11427 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11428 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11429 | if (length == 1) |
| 11430 | return PyBool_FromLong( |
| 11431 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11432 | |
| 11433 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11434 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11435 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11437 | for (i = 0; i < length; i++) { |
| 11438 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11440 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11441 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11442 | } |
| 11443 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11444 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11445 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11446 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11447 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11448 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11449 | |
| 11450 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11451 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11452 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | int kind; |
| 11454 | void *data; |
| 11455 | Py_ssize_t len, i; |
| 11456 | |
| 11457 | if (PyUnicode_READY(self) == -1) |
| 11458 | return NULL; |
| 11459 | |
| 11460 | kind = PyUnicode_KIND(self); |
| 11461 | data = PyUnicode_DATA(self); |
| 11462 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11463 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11464 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | if (len == 1) { |
| 11466 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11467 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11468 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11469 | |
| 11470 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11472 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | for (i = 0; i < len; i++) { |
| 11475 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11476 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11477 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11478 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11479 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11480 | } |
| 11481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11482 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11483 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11485 | 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] | 11486 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | |
| 11488 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11489 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11491 | Py_ssize_t i, length; |
| 11492 | int kind; |
| 11493 | void *data; |
| 11494 | |
| 11495 | if (PyUnicode_READY(self) == -1) |
| 11496 | return NULL; |
| 11497 | length = PyUnicode_GET_LENGTH(self); |
| 11498 | kind = PyUnicode_KIND(self); |
| 11499 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11502 | if (length == 1) |
| 11503 | return PyBool_FromLong( |
| 11504 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11506 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11510 | for (i = 0; i < length; i++) { |
| 11511 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11512 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11514 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | } |
| 11516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11517 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11518 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11520 | Return True if all characters in S are digits\n\ |
| 11521 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11522 | |
| 11523 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11524 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11526 | Py_ssize_t i, length; |
| 11527 | int kind; |
| 11528 | void *data; |
| 11529 | |
| 11530 | if (PyUnicode_READY(self) == -1) |
| 11531 | return NULL; |
| 11532 | length = PyUnicode_GET_LENGTH(self); |
| 11533 | kind = PyUnicode_KIND(self); |
| 11534 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | if (length == 1) { |
| 11538 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11539 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11540 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11542 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11544 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11545 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11546 | for (i = 0; i < length; i++) { |
| 11547 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11548 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11550 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | } |
| 11552 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11553 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11554 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11556 | 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] | 11557 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | |
| 11559 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11560 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | Py_ssize_t i, length; |
| 11563 | int kind; |
| 11564 | void *data; |
| 11565 | |
| 11566 | if (PyUnicode_READY(self) == -1) |
| 11567 | return NULL; |
| 11568 | length = PyUnicode_GET_LENGTH(self); |
| 11569 | kind = PyUnicode_KIND(self); |
| 11570 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | if (length == 1) |
| 11574 | return PyBool_FromLong( |
| 11575 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11577 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11579 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11580 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11581 | for (i = 0; i < length; i++) { |
| 11582 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11583 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11585 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11586 | } |
| 11587 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11588 | int |
| 11589 | PyUnicode_IsIdentifier(PyObject *self) |
| 11590 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11591 | int kind; |
| 11592 | void *data; |
| 11593 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11594 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11595 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11596 | if (PyUnicode_READY(self) == -1) { |
| 11597 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11598 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11599 | } |
| 11600 | |
| 11601 | /* Special case for empty strings */ |
| 11602 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11603 | return 0; |
| 11604 | kind = PyUnicode_KIND(self); |
| 11605 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11606 | |
| 11607 | /* PEP 3131 says that the first character must be in |
| 11608 | XID_Start and subsequent characters in XID_Continue, |
| 11609 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11610 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11611 | letters, digits, underscore). However, given the current |
| 11612 | definition of XID_Start and XID_Continue, it is sufficient |
| 11613 | to check just for these, except that _ must be allowed |
| 11614 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11615 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11616 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11617 | return 0; |
| 11618 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11619 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11621 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11622 | return 1; |
| 11623 | } |
| 11624 | |
| 11625 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11626 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11627 | \n\ |
| 11628 | Return True if S is a valid identifier according\n\ |
| 11629 | to the language definition."); |
| 11630 | |
| 11631 | static PyObject* |
| 11632 | unicode_isidentifier(PyObject *self) |
| 11633 | { |
| 11634 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11635 | } |
| 11636 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11637 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11638 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11639 | \n\ |
| 11640 | Return True if all characters in S are considered\n\ |
| 11641 | printable in repr() or S is empty, False otherwise."); |
| 11642 | |
| 11643 | static PyObject* |
| 11644 | unicode_isprintable(PyObject *self) |
| 11645 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11646 | Py_ssize_t i, length; |
| 11647 | int kind; |
| 11648 | void *data; |
| 11649 | |
| 11650 | if (PyUnicode_READY(self) == -1) |
| 11651 | return NULL; |
| 11652 | length = PyUnicode_GET_LENGTH(self); |
| 11653 | kind = PyUnicode_KIND(self); |
| 11654 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11655 | |
| 11656 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | if (length == 1) |
| 11658 | return PyBool_FromLong( |
| 11659 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11661 | for (i = 0; i < length; i++) { |
| 11662 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11663 | Py_RETURN_FALSE; |
| 11664 | } |
| 11665 | } |
| 11666 | Py_RETURN_TRUE; |
| 11667 | } |
| 11668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11669 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11670 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | \n\ |
| 11672 | 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] | 11673 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11674 | |
| 11675 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11676 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11678 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | } |
| 11680 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11681 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11682 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11684 | if (PyUnicode_READY(self) == -1) |
| 11685 | return -1; |
| 11686 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11687 | } |
| 11688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11689 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11690 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11691 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11692 | 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] | 11693 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11694 | |
| 11695 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11696 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11697 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11698 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | Py_UCS4 fillchar = ' '; |
| 11700 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11701 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | return NULL; |
| 11703 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11704 | if (PyUnicode_READY(self) < 0) |
| 11705 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11707 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11708 | return unicode_result_unchanged(self); |
| 11709 | |
| 11710 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11711 | } |
| 11712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11713 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11714 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11715 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11716 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | |
| 11718 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11719 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11721 | if (PyUnicode_READY(self) == -1) |
| 11722 | return NULL; |
| 11723 | if (PyUnicode_IS_ASCII(self)) |
| 11724 | return ascii_upper_or_lower(self, 1); |
| 11725 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | } |
| 11727 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11728 | #define LEFTSTRIP 0 |
| 11729 | #define RIGHTSTRIP 1 |
| 11730 | #define BOTHSTRIP 2 |
| 11731 | |
| 11732 | /* Arrays indexed by above */ |
| 11733 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11734 | |
| 11735 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11736 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11737 | /* externally visible for str.strip(unicode) */ |
| 11738 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11739 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11740 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11741 | void *data; |
| 11742 | int kind; |
| 11743 | Py_ssize_t i, j, len; |
| 11744 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11745 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11746 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11747 | return NULL; |
| 11748 | |
| 11749 | kind = PyUnicode_KIND(self); |
| 11750 | data = PyUnicode_DATA(self); |
| 11751 | len = PyUnicode_GET_LENGTH(self); |
| 11752 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11753 | PyUnicode_DATA(sepobj), |
| 11754 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11756 | i = 0; |
| 11757 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11758 | while (i < len && |
| 11759 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11760 | i++; |
| 11761 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11762 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11763 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11764 | j = len; |
| 11765 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | do { |
| 11767 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11768 | } while (j >= i && |
| 11769 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11770 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11771 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11772 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11773 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11774 | } |
| 11775 | |
| 11776 | PyObject* |
| 11777 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11778 | { |
| 11779 | unsigned char *data; |
| 11780 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11781 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11782 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11783 | if (PyUnicode_READY(self) == -1) |
| 11784 | return NULL; |
| 11785 | |
| 11786 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11787 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11788 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11789 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11790 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11791 | length = end - start; |
| 11792 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11793 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11794 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11795 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11796 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11797 | return NULL; |
| 11798 | } |
| 11799 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11800 | if (PyUnicode_IS_ASCII(self)) { |
| 11801 | kind = PyUnicode_KIND(self); |
| 11802 | data = PyUnicode_1BYTE_DATA(self); |
| 11803 | return unicode_fromascii(data + start, length); |
| 11804 | } |
| 11805 | else { |
| 11806 | kind = PyUnicode_KIND(self); |
| 11807 | data = PyUnicode_1BYTE_DATA(self); |
| 11808 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11809 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11810 | length); |
| 11811 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11812 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11813 | |
| 11814 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11815 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | int kind; |
| 11818 | void *data; |
| 11819 | Py_ssize_t len, i, j; |
| 11820 | |
| 11821 | if (PyUnicode_READY(self) == -1) |
| 11822 | return NULL; |
| 11823 | |
| 11824 | kind = PyUnicode_KIND(self); |
| 11825 | data = PyUnicode_DATA(self); |
| 11826 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11827 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11828 | i = 0; |
| 11829 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11831 | i++; |
| 11832 | } |
| 11833 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11834 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11835 | j = len; |
| 11836 | if (striptype != LEFTSTRIP) { |
| 11837 | do { |
| 11838 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11839 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11840 | j++; |
| 11841 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11842 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11843 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11844 | } |
| 11845 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11846 | |
| 11847 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11848 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11850 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11851 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11852 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11853 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11854 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11855 | if (sep != NULL && sep != Py_None) { |
| 11856 | if (PyUnicode_Check(sep)) |
| 11857 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11858 | else { |
| 11859 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11860 | "%s arg must be None or str", |
| 11861 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11862 | return NULL; |
| 11863 | } |
| 11864 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11866 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
| 11869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11870 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11871 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11872 | \n\ |
| 11873 | Return a copy of the string S with leading and trailing\n\ |
| 11874 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11875 | 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] | 11876 | |
| 11877 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11878 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11879 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11880 | if (PyTuple_GET_SIZE(args) == 0) |
| 11881 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11882 | else |
| 11883 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11884 | } |
| 11885 | |
| 11886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11887 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11888 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11889 | \n\ |
| 11890 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11891 | 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] | 11892 | |
| 11893 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11894 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11895 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11896 | if (PyTuple_GET_SIZE(args) == 0) |
| 11897 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11898 | else |
| 11899 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11900 | } |
| 11901 | |
| 11902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11903 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11904 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11905 | \n\ |
| 11906 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11907 | 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] | 11908 | |
| 11909 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11910 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11911 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11912 | if (PyTuple_GET_SIZE(args) == 0) |
| 11913 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11914 | else |
| 11915 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
| 11918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11920 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11921 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11922 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11925 | if (len < 1) { |
| 11926 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11927 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11928 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11929 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11930 | /* no repeat, return original string */ |
| 11931 | if (len == 1) |
| 11932 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11933 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11934 | if (PyUnicode_READY(str) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11935 | return NULL; |
| 11936 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11937 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11938 | PyErr_SetString(PyExc_OverflowError, |
| 11939 | "repeated string is too long"); |
| 11940 | return NULL; |
| 11941 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11942 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11943 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11944 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11945 | if (!u) |
| 11946 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11947 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11948 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11950 | const int kind = PyUnicode_KIND(str); |
| 11951 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11952 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11953 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11954 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11955 | } |
| 11956 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11957 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11958 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11959 | ucs2[n] = fill_char; |
| 11960 | } else { |
| 11961 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11962 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11963 | for (n = 0; n < len; ++n) |
| 11964 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11965 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11966 | } |
| 11967 | else { |
| 11968 | /* number of characters copied this far */ |
| 11969 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11970 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11971 | char *to = (char *) PyUnicode_DATA(u); |
| 11972 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11973 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11974 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11975 | n = (done <= nchars-done) ? done : nchars-done; |
| 11976 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11977 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11978 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | } |
| 11980 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11981 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11982 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11983 | } |
| 11984 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11985 | PyObject * |
| 11986 | PyUnicode_Replace(PyObject *obj, |
| 11987 | PyObject *subobj, |
| 11988 | PyObject *replobj, |
| 11989 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11990 | { |
| 11991 | PyObject *self; |
| 11992 | PyObject *str1; |
| 11993 | PyObject *str2; |
| 11994 | PyObject *result; |
| 11995 | |
| 11996 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11997 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11998 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11999 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12000 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12001 | Py_DECREF(self); |
| 12002 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12003 | } |
| 12004 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12005 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12006 | Py_DECREF(self); |
| 12007 | Py_DECREF(str1); |
| 12008 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12009 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12010 | if (PyUnicode_READY(self) == -1 || |
| 12011 | PyUnicode_READY(str1) == -1 || |
| 12012 | PyUnicode_READY(str2) == -1) |
| 12013 | result = NULL; |
| 12014 | else |
| 12015 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12016 | Py_DECREF(self); |
| 12017 | Py_DECREF(str1); |
| 12018 | Py_DECREF(str2); |
| 12019 | return result; |
| 12020 | } |
| 12021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12022 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12023 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12024 | \n\ |
| 12025 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12026 | old replaced by new. If the optional argument count is\n\ |
| 12027 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12028 | |
| 12029 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12031 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12032 | PyObject *str1; |
| 12033 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12034 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12035 | PyObject *result; |
| 12036 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12037 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12039 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12040 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12042 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12043 | return NULL; |
| 12044 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12045 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12046 | Py_DECREF(str1); |
| 12047 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12048 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12049 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12050 | result = NULL; |
| 12051 | else |
| 12052 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12053 | |
| 12054 | Py_DECREF(str1); |
| 12055 | Py_DECREF(str2); |
| 12056 | return result; |
| 12057 | } |
| 12058 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12059 | static PyObject * |
| 12060 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12061 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12062 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | Py_ssize_t isize; |
| 12064 | Py_ssize_t osize, squote, dquote, i, o; |
| 12065 | Py_UCS4 max, quote; |
| 12066 | int ikind, okind; |
| 12067 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12070 | return NULL; |
| 12071 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12072 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12073 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12075 | /* Compute length of output, quote characters, and |
| 12076 | maximum character */ |
| 12077 | osize = 2; /* quotes */ |
| 12078 | max = 127; |
| 12079 | squote = dquote = 0; |
| 12080 | ikind = PyUnicode_KIND(unicode); |
| 12081 | for (i = 0; i < isize; i++) { |
| 12082 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12083 | switch (ch) { |
| 12084 | case '\'': squote++; osize++; break; |
| 12085 | case '"': dquote++; osize++; break; |
| 12086 | case '\\': case '\t': case '\r': case '\n': |
| 12087 | osize += 2; break; |
| 12088 | default: |
| 12089 | /* Fast-path ASCII */ |
| 12090 | if (ch < ' ' || ch == 0x7f) |
| 12091 | osize += 4; /* \xHH */ |
| 12092 | else if (ch < 0x7f) |
| 12093 | osize++; |
| 12094 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12095 | osize++; |
| 12096 | max = ch > max ? ch : max; |
| 12097 | } |
| 12098 | else if (ch < 0x100) |
| 12099 | osize += 4; /* \xHH */ |
| 12100 | else if (ch < 0x10000) |
| 12101 | osize += 6; /* \uHHHH */ |
| 12102 | else |
| 12103 | osize += 10; /* \uHHHHHHHH */ |
| 12104 | } |
| 12105 | } |
| 12106 | |
| 12107 | quote = '\''; |
| 12108 | if (squote) { |
| 12109 | if (dquote) |
| 12110 | /* Both squote and dquote present. Use squote, |
| 12111 | and escape them */ |
| 12112 | osize += squote; |
| 12113 | else |
| 12114 | quote = '"'; |
| 12115 | } |
| 12116 | |
| 12117 | repr = PyUnicode_New(osize, max); |
| 12118 | if (repr == NULL) |
| 12119 | return NULL; |
| 12120 | okind = PyUnicode_KIND(repr); |
| 12121 | odata = PyUnicode_DATA(repr); |
| 12122 | |
| 12123 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12124 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12125 | |
| 12126 | for (i = 0, o = 1; i < isize; i++) { |
| 12127 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12128 | |
| 12129 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12130 | if ((ch == quote) || (ch == '\\')) { |
| 12131 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12132 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12133 | continue; |
| 12134 | } |
| 12135 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12136 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12137 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12138 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12139 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12140 | } |
| 12141 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12143 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12144 | } |
| 12145 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12147 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12148 | } |
| 12149 | |
| 12150 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12151 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12152 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12153 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12154 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12155 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12156 | } |
| 12157 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12158 | /* Copy ASCII characters as-is */ |
| 12159 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12161 | } |
| 12162 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12163 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12164 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12165 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12166 | (categories Z* and C* except ASCII space) |
| 12167 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12169 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12170 | if (ch <= 0xff) { |
| 12171 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12172 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12173 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12174 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12175 | } |
| 12176 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12177 | else if (ch >= 0x10000) { |
| 12178 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12179 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12180 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12181 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12182 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12183 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12184 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12185 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12186 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12187 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12188 | } |
| 12189 | /* Map 16-bit characters to '\uxxxx' */ |
| 12190 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12191 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12192 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12193 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12194 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12195 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12196 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12197 | } |
| 12198 | } |
| 12199 | /* Copy characters as-is */ |
| 12200 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12201 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12202 | } |
| 12203 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12204 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12205 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12206 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12207 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | } |
| 12209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12210 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12211 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | \n\ |
| 12213 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12214 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12215 | arguments start and end are interpreted as in slice notation.\n\ |
| 12216 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12217 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12218 | |
| 12219 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12220 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12222 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12223 | Py_ssize_t start; |
| 12224 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12225 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12227 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12228 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12229 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12231 | if (PyUnicode_READY(self) == -1) |
| 12232 | return NULL; |
| 12233 | if (PyUnicode_READY(substring) == -1) |
| 12234 | return NULL; |
| 12235 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12236 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12237 | |
| 12238 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | if (result == -2) |
| 12241 | return NULL; |
| 12242 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12243 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | } |
| 12245 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12246 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12247 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12249 | 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] | 12250 | |
| 12251 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12254 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12255 | Py_ssize_t start; |
| 12256 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12257 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12258 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12259 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12260 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12261 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12263 | if (PyUnicode_READY(self) == -1) |
| 12264 | return NULL; |
| 12265 | if (PyUnicode_READY(substring) == -1) |
| 12266 | return NULL; |
| 12267 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12268 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12269 | |
| 12270 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12271 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | if (result == -2) |
| 12273 | return NULL; |
| 12274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | if (result < 0) { |
| 12276 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12277 | return NULL; |
| 12278 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12279 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12280 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | } |
| 12282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12283 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12286 | 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] | 12287 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12288 | |
| 12289 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12290 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12291 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12292 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12293 | Py_UCS4 fillchar = ' '; |
| 12294 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12295 | 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] | 12296 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12297 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12298 | if (PyUnicode_READY(self) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | return NULL; |
| 12300 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12301 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12302 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12303 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12304 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | } |
| 12306 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12307 | PyObject * |
| 12308 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | { |
| 12310 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | s = PyUnicode_FromObject(s); |
| 12313 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12314 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12315 | if (sep != NULL) { |
| 12316 | sep = PyUnicode_FromObject(sep); |
| 12317 | if (sep == NULL) { |
| 12318 | Py_DECREF(s); |
| 12319 | return NULL; |
| 12320 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12321 | } |
| 12322 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12323 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12324 | |
| 12325 | Py_DECREF(s); |
| 12326 | Py_XDECREF(sep); |
| 12327 | return result; |
| 12328 | } |
| 12329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12330 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12331 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12332 | \n\ |
| 12333 | Return a list of the words in S, using sep as the\n\ |
| 12334 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12335 | 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] | 12336 | whitespace string is a separator and empty strings are\n\ |
| 12337 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 | |
| 12339 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12340 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | { |
| 12342 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12343 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12344 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12345 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12346 | return NULL; |
| 12347 | |
| 12348 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12349 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12350 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12351 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12352 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12353 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12354 | } |
| 12355 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12356 | PyObject * |
| 12357 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12358 | { |
| 12359 | PyObject* str_obj; |
| 12360 | PyObject* sep_obj; |
| 12361 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12362 | int kind1, kind2, kind; |
| 12363 | void *buf1 = NULL, *buf2 = NULL; |
| 12364 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12365 | |
| 12366 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12367 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12369 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12370 | if (!sep_obj) { |
| 12371 | Py_DECREF(str_obj); |
| 12372 | return NULL; |
| 12373 | } |
| 12374 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12375 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12376 | Py_DECREF(str_obj); |
| 12377 | return NULL; |
| 12378 | } |
| 12379 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12380 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12381 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12382 | kind = Py_MAX(kind1, kind2); |
| 12383 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12384 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12385 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | if (!buf1) |
| 12387 | goto onError; |
| 12388 | buf2 = PyUnicode_DATA(sep_obj); |
| 12389 | if (kind2 != kind) |
| 12390 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12391 | if (!buf2) |
| 12392 | goto onError; |
| 12393 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12394 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12395 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12396 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12397 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12398 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12399 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12400 | else |
| 12401 | 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] | 12402 | break; |
| 12403 | case PyUnicode_2BYTE_KIND: |
| 12404 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12405 | break; |
| 12406 | case PyUnicode_4BYTE_KIND: |
| 12407 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12408 | break; |
| 12409 | default: |
| 12410 | assert(0); |
| 12411 | out = 0; |
| 12412 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12413 | |
| 12414 | Py_DECREF(sep_obj); |
| 12415 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12416 | if (kind1 != kind) |
| 12417 | PyMem_Free(buf1); |
| 12418 | if (kind2 != kind) |
| 12419 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12420 | |
| 12421 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | onError: |
| 12423 | Py_DECREF(sep_obj); |
| 12424 | Py_DECREF(str_obj); |
| 12425 | if (kind1 != kind && buf1) |
| 12426 | PyMem_Free(buf1); |
| 12427 | if (kind2 != kind && buf2) |
| 12428 | PyMem_Free(buf2); |
| 12429 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12430 | } |
| 12431 | |
| 12432 | |
| 12433 | PyObject * |
| 12434 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12435 | { |
| 12436 | PyObject* str_obj; |
| 12437 | PyObject* sep_obj; |
| 12438 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12439 | int kind1, kind2, kind; |
| 12440 | void *buf1 = NULL, *buf2 = NULL; |
| 12441 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12442 | |
| 12443 | str_obj = PyUnicode_FromObject(str_in); |
| 12444 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12445 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12446 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12447 | if (!sep_obj) { |
| 12448 | Py_DECREF(str_obj); |
| 12449 | return NULL; |
| 12450 | } |
| 12451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | kind1 = PyUnicode_KIND(str_in); |
| 12453 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12454 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12455 | buf1 = PyUnicode_DATA(str_in); |
| 12456 | if (kind1 != kind) |
| 12457 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12458 | if (!buf1) |
| 12459 | goto onError; |
| 12460 | buf2 = PyUnicode_DATA(sep_obj); |
| 12461 | if (kind2 != kind) |
| 12462 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12463 | if (!buf2) |
| 12464 | goto onError; |
| 12465 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12466 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12467 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12468 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12469 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12470 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12471 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12472 | else |
| 12473 | 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] | 12474 | break; |
| 12475 | case PyUnicode_2BYTE_KIND: |
| 12476 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12477 | break; |
| 12478 | case PyUnicode_4BYTE_KIND: |
| 12479 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12480 | break; |
| 12481 | default: |
| 12482 | assert(0); |
| 12483 | out = 0; |
| 12484 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12485 | |
| 12486 | Py_DECREF(sep_obj); |
| 12487 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12488 | if (kind1 != kind) |
| 12489 | PyMem_Free(buf1); |
| 12490 | if (kind2 != kind) |
| 12491 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12492 | |
| 12493 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12494 | onError: |
| 12495 | Py_DECREF(sep_obj); |
| 12496 | Py_DECREF(str_obj); |
| 12497 | if (kind1 != kind && buf1) |
| 12498 | PyMem_Free(buf1); |
| 12499 | if (kind2 != kind && buf2) |
| 12500 | PyMem_Free(buf2); |
| 12501 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12502 | } |
| 12503 | |
| 12504 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12505 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12506 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12507 | 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] | 12508 | 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] | 12509 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12510 | |
| 12511 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12512 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12513 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12514 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12515 | } |
| 12516 | |
| 12517 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12518 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12519 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12520 | 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] | 12521 | 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] | 12522 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12523 | |
| 12524 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12525 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12526 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12527 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12528 | } |
| 12529 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12530 | PyObject * |
| 12531 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12532 | { |
| 12533 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12534 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12535 | s = PyUnicode_FromObject(s); |
| 12536 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12537 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12538 | if (sep != NULL) { |
| 12539 | sep = PyUnicode_FromObject(sep); |
| 12540 | if (sep == NULL) { |
| 12541 | Py_DECREF(s); |
| 12542 | return NULL; |
| 12543 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12544 | } |
| 12545 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12546 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12547 | |
| 12548 | Py_DECREF(s); |
| 12549 | Py_XDECREF(sep); |
| 12550 | return result; |
| 12551 | } |
| 12552 | |
| 12553 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12554 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12555 | \n\ |
| 12556 | Return a list of the words in S, using sep as the\n\ |
| 12557 | delimiter string, starting at the end of the string and\n\ |
| 12558 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12559 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12560 | is a separator."); |
| 12561 | |
| 12562 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12563 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12564 | { |
| 12565 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12566 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12567 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12568 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12569 | return NULL; |
| 12570 | |
| 12571 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12572 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12573 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12574 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12575 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12576 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12577 | } |
| 12578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12579 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12580 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | \n\ |
| 12582 | 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] | 12583 | 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] | 12584 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12585 | |
| 12586 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12587 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12588 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12589 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12590 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12592 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12593 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | return NULL; |
| 12595 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12596 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12597 | } |
| 12598 | |
| 12599 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12600 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12601 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12602 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12603 | } |
| 12604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12605 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12606 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12607 | \n\ |
| 12608 | 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] | 12609 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12610 | |
| 12611 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12612 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12613 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12614 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12615 | } |
| 12616 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12617 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12618 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12619 | \n\ |
| 12620 | Return a translation table usable for str.translate().\n\ |
| 12621 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12622 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12623 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12624 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12625 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12626 | character at the same position in y. If there is a third argument, it\n\ |
| 12627 | must be a string, whose characters will be mapped to None in the result."); |
| 12628 | |
| 12629 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12630 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12631 | { |
| 12632 | PyObject *x, *y = NULL, *z = NULL; |
| 12633 | PyObject *new = NULL, *key, *value; |
| 12634 | Py_ssize_t i = 0; |
| 12635 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12636 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12637 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12638 | return NULL; |
| 12639 | new = PyDict_New(); |
| 12640 | if (!new) |
| 12641 | return NULL; |
| 12642 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12643 | int x_kind, y_kind, z_kind; |
| 12644 | void *x_data, *y_data, *z_data; |
| 12645 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12646 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12647 | if (!PyUnicode_Check(x)) { |
| 12648 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12649 | "be a string if there is a second argument"); |
| 12650 | goto err; |
| 12651 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12652 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12653 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12654 | "arguments must have equal length"); |
| 12655 | goto err; |
| 12656 | } |
| 12657 | /* 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] | 12658 | x_kind = PyUnicode_KIND(x); |
| 12659 | y_kind = PyUnicode_KIND(y); |
| 12660 | x_data = PyUnicode_DATA(x); |
| 12661 | y_data = PyUnicode_DATA(y); |
| 12662 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12663 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12664 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12665 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12666 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12667 | if (!value) { |
| 12668 | Py_DECREF(key); |
| 12669 | goto err; |
| 12670 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12671 | res = PyDict_SetItem(new, key, value); |
| 12672 | Py_DECREF(key); |
| 12673 | Py_DECREF(value); |
| 12674 | if (res < 0) |
| 12675 | goto err; |
| 12676 | } |
| 12677 | /* create entries for deleting chars in z */ |
| 12678 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12679 | z_kind = PyUnicode_KIND(z); |
| 12680 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12681 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12682 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12683 | if (!key) |
| 12684 | goto err; |
| 12685 | res = PyDict_SetItem(new, key, Py_None); |
| 12686 | Py_DECREF(key); |
| 12687 | if (res < 0) |
| 12688 | goto err; |
| 12689 | } |
| 12690 | } |
| 12691 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12692 | int kind; |
| 12693 | void *data; |
| 12694 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12695 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12696 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12697 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12698 | "to maketrans it must be a dict"); |
| 12699 | goto err; |
| 12700 | } |
| 12701 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12702 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12703 | if (PyUnicode_Check(key)) { |
| 12704 | /* convert string keys to integer keys */ |
| 12705 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12706 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12707 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12708 | "table must be of length 1"); |
| 12709 | goto err; |
| 12710 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12711 | kind = PyUnicode_KIND(key); |
| 12712 | data = PyUnicode_DATA(key); |
| 12713 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12714 | if (!newkey) |
| 12715 | goto err; |
| 12716 | res = PyDict_SetItem(new, newkey, value); |
| 12717 | Py_DECREF(newkey); |
| 12718 | if (res < 0) |
| 12719 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12720 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12721 | /* just keep integer keys */ |
| 12722 | if (PyDict_SetItem(new, key, value) < 0) |
| 12723 | goto err; |
| 12724 | } else { |
| 12725 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12726 | "be strings or integers"); |
| 12727 | goto err; |
| 12728 | } |
| 12729 | } |
| 12730 | } |
| 12731 | return new; |
| 12732 | err: |
| 12733 | Py_DECREF(new); |
| 12734 | return NULL; |
| 12735 | } |
| 12736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12737 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12738 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12739 | \n\ |
| 12740 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12741 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12742 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12743 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12744 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | |
| 12746 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12747 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12748 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12749 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12750 | } |
| 12751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12752 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12753 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12754 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12755 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12756 | |
| 12757 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12758 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12759 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12760 | if (PyUnicode_READY(self) == -1) |
| 12761 | return NULL; |
| 12762 | if (PyUnicode_IS_ASCII(self)) |
| 12763 | return ascii_upper_or_lower(self, 0); |
| 12764 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12765 | } |
| 12766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12767 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12768 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12770 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12771 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12772 | |
| 12773 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12774 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12776 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12777 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12778 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12779 | int kind; |
| 12780 | void *data; |
| 12781 | Py_UCS4 chr; |
| 12782 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12783 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12784 | return NULL; |
| 12785 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12786 | if (PyUnicode_READY(self) < 0) |
| 12787 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12788 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12789 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12790 | return unicode_result_unchanged(self); |
| 12791 | |
| 12792 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12793 | |
| 12794 | u = pad(self, fill, 0, '0'); |
| 12795 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12796 | if (u == NULL) |
| 12797 | return NULL; |
| 12798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12799 | kind = PyUnicode_KIND(u); |
| 12800 | data = PyUnicode_DATA(u); |
| 12801 | chr = PyUnicode_READ(kind, data, fill); |
| 12802 | |
| 12803 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12804 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12805 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12806 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | } |
| 12808 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12809 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12810 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12811 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12812 | |
| 12813 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12814 | static PyObject * |
| 12815 | unicode__decimal2ascii(PyObject *self) |
| 12816 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12817 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12819 | #endif |
| 12820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12821 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12822 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12824 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12825 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12826 | With optional end, stop comparing S at that position.\n\ |
| 12827 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12828 | |
| 12829 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12830 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12833 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12834 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12835 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12836 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12837 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12838 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12839 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12841 | if (PyTuple_Check(subobj)) { |
| 12842 | Py_ssize_t i; |
| 12843 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12844 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12845 | if (substring == NULL) |
| 12846 | return NULL; |
| 12847 | result = tailmatch(self, substring, start, end, -1); |
| 12848 | Py_DECREF(substring); |
| 12849 | if (result) { |
| 12850 | Py_RETURN_TRUE; |
| 12851 | } |
| 12852 | } |
| 12853 | /* nothing matched */ |
| 12854 | Py_RETURN_FALSE; |
| 12855 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12856 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12857 | if (substring == NULL) { |
| 12858 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12859 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12860 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12861 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12862 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12863 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12864 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12865 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12866 | } |
| 12867 | |
| 12868 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12869 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12870 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12871 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12872 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12873 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12874 | With optional end, stop comparing S at that position.\n\ |
| 12875 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12876 | |
| 12877 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12878 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12879 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12880 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12881 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12882 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12883 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12884 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12885 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12887 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12889 | if (PyTuple_Check(subobj)) { |
| 12890 | Py_ssize_t i; |
| 12891 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12892 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12893 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12894 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12895 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12896 | result = tailmatch(self, substring, start, end, +1); |
| 12897 | Py_DECREF(substring); |
| 12898 | if (result) { |
| 12899 | Py_RETURN_TRUE; |
| 12900 | } |
| 12901 | } |
| 12902 | Py_RETURN_FALSE; |
| 12903 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12904 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12905 | if (substring == NULL) { |
| 12906 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12907 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12908 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12909 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12910 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12911 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12912 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12913 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12914 | } |
| 12915 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12916 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12917 | |
| 12918 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12919 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12920 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12921 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12922 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12923 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12924 | PyDoc_STRVAR(format_map__doc__, |
| 12925 | "S.format_map(mapping) -> str\n\ |
| 12926 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12927 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12928 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12929 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12930 | static PyObject * |
| 12931 | unicode__format__(PyObject* self, PyObject* args) |
| 12932 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12933 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12934 | |
| 12935 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12936 | return NULL; |
| 12937 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12938 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12939 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12940 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12941 | } |
| 12942 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12943 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12944 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12945 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12946 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12947 | |
| 12948 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12949 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12950 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12951 | Py_ssize_t size; |
| 12952 | |
| 12953 | /* If it's a compact object, account for base structure + |
| 12954 | character data. */ |
| 12955 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12956 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12957 | else if (PyUnicode_IS_COMPACT(v)) |
| 12958 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12959 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12960 | else { |
| 12961 | /* If it is a two-block object, account for base object, and |
| 12962 | for character block if present. */ |
| 12963 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12964 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12965 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12966 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12967 | } |
| 12968 | /* 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] | 12969 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12970 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12971 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12972 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12973 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12974 | |
| 12975 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12976 | } |
| 12977 | |
| 12978 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12979 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12980 | |
| 12981 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12982 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12983 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 12984 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12985 | if (!copy) |
| 12986 | return NULL; |
| 12987 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12988 | } |
| 12989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12990 | static PyMethodDef unicode_methods[] = { |
| 12991 | |
| 12992 | /* Order is according to common usage: often used methods should |
| 12993 | appear first, since lookup is done sequentially. */ |
| 12994 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12995 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12996 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12997 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12998 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12999 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13000 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 13001 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13002 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13003 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13004 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13005 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13006 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13007 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13008 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13009 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13010 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13011 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13012 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13013 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13014 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13015 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13016 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13017 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13018 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13019 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13020 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13021 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13022 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13023 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13024 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13025 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13026 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13027 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13028 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13029 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13030 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13031 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13032 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13033 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13034 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13035 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13036 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13037 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13038 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13039 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13040 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13041 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13042 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13043 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13044 | #endif |
| 13045 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13046 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13047 | {NULL, NULL} |
| 13048 | }; |
| 13049 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13050 | static PyObject * |
| 13051 | unicode_mod(PyObject *v, PyObject *w) |
| 13052 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13053 | if (!PyUnicode_Check(v)) |
| 13054 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13055 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13056 | } |
| 13057 | |
| 13058 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13059 | 0, /*nb_add*/ |
| 13060 | 0, /*nb_subtract*/ |
| 13061 | 0, /*nb_multiply*/ |
| 13062 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13063 | }; |
| 13064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13065 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13066 | (lenfunc) unicode_length, /* sq_length */ |
| 13067 | PyUnicode_Concat, /* sq_concat */ |
| 13068 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13069 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13070 | 0, /* sq_slice */ |
| 13071 | 0, /* sq_ass_item */ |
| 13072 | 0, /* sq_ass_slice */ |
| 13073 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13074 | }; |
| 13075 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13076 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13077 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13078 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13079 | if (PyUnicode_READY(self) == -1) |
| 13080 | return NULL; |
| 13081 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13082 | if (PyIndex_Check(item)) { |
| 13083 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13084 | if (i == -1 && PyErr_Occurred()) |
| 13085 | return NULL; |
| 13086 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13087 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13088 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13089 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13090 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13091 | PyObject *result; |
| 13092 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13093 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13094 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13096 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13097 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13098 | return NULL; |
| 13099 | } |
| 13100 | |
| 13101 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13102 | Py_INCREF(unicode_empty); |
| 13103 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13104 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13105 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13106 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13107 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13108 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13109 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13110 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13111 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13112 | src_kind = PyUnicode_KIND(self); |
| 13113 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13114 | if (!PyUnicode_IS_ASCII(self)) { |
| 13115 | kind_limit = kind_maxchar_limit(src_kind); |
| 13116 | max_char = 0; |
| 13117 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13118 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13119 | if (ch > max_char) { |
| 13120 | max_char = ch; |
| 13121 | if (max_char >= kind_limit) |
| 13122 | break; |
| 13123 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13124 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13125 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13126 | else |
| 13127 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13128 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13129 | if (result == NULL) |
| 13130 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13131 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13132 | dest_data = PyUnicode_DATA(result); |
| 13133 | |
| 13134 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13135 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13136 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13137 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13138 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13139 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13140 | } else { |
| 13141 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13142 | return NULL; |
| 13143 | } |
| 13144 | } |
| 13145 | |
| 13146 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13147 | (lenfunc)unicode_length, /* mp_length */ |
| 13148 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13149 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13150 | }; |
| 13151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13153 | /* Helpers for PyUnicode_Format() */ |
| 13154 | |
| 13155 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13156 | 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] | 13157 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13158 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13159 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13160 | (*p_argidx)++; |
| 13161 | if (arglen < 0) |
| 13162 | return args; |
| 13163 | else |
| 13164 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13165 | } |
| 13166 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13167 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | return NULL; |
| 13169 | } |
| 13170 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13171 | /* 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] | 13172 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13173 | static PyObject * |
| 13174 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13175 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13176 | char *p; |
| 13177 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13178 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13180 | x = PyFloat_AsDouble(v); |
| 13181 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13182 | return NULL; |
| 13183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13184 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13185 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13186 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13187 | p = PyOS_double_to_string(x, type, prec, |
| 13188 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13189 | if (p == NULL) |
| 13190 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13191 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13192 | PyMem_Free(p); |
| 13193 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13194 | } |
| 13195 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13196 | static PyObject* |
| 13197 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13198 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13199 | char *buf; |
| 13200 | int len; |
| 13201 | PyObject *str; /* temporary string object. */ |
| 13202 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13203 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13204 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13205 | if (!str) |
| 13206 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13207 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13208 | Py_DECREF(str); |
| 13209 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13210 | } |
| 13211 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13212 | static Py_UCS4 |
| 13213 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13215 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13216 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13217 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13218 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13219 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13220 | goto onError; |
| 13221 | } |
| 13222 | else { |
| 13223 | /* Integer input truncated to a character */ |
| 13224 | long x; |
| 13225 | x = PyLong_AsLong(v); |
| 13226 | if (x == -1 && PyErr_Occurred()) |
| 13227 | goto onError; |
| 13228 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13229 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13230 | PyErr_SetString(PyExc_OverflowError, |
| 13231 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13232 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13233 | } |
| 13234 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13235 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13236 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13237 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13238 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13239 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13240 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13241 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13242 | } |
| 13243 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13244 | static int |
| 13245 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13246 | { |
| 13247 | int r; |
| 13248 | assert(count > 0); |
| 13249 | assert(PyUnicode_Check(obj)); |
| 13250 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13251 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13252 | if (repeated == NULL) |
| 13253 | return -1; |
| 13254 | r = _PyAccu_Accumulate(acc, repeated); |
| 13255 | Py_DECREF(repeated); |
| 13256 | return r; |
| 13257 | } |
| 13258 | else { |
| 13259 | do { |
| 13260 | if (_PyAccu_Accumulate(acc, obj)) |
| 13261 | return -1; |
| 13262 | } while (--count); |
| 13263 | return 0; |
| 13264 | } |
| 13265 | } |
| 13266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13267 | PyObject * |
| 13268 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13270 | void *fmt; |
| 13271 | int fmtkind; |
| 13272 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13273 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13274 | int r; |
| 13275 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13276 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13277 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13278 | PyObject *temp = NULL; |
| 13279 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13280 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13281 | _PyAccu acc; |
| 13282 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13283 | |
| 13284 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13285 | return NULL; |
| 13286 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13287 | return NULL; |
| 13288 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13289 | return NULL; |
| 13290 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13291 | return NULL; |
| 13292 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13293 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13295 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13296 | PyErr_BadInternalCall(); |
| 13297 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13298 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13299 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13300 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13301 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13302 | if (PyUnicode_READY(uformat) == -1) |
| 13303 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13304 | if (_PyAccu_Init(&acc)) |
| 13305 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13306 | fmt = PyUnicode_DATA(uformat); |
| 13307 | fmtkind = PyUnicode_KIND(uformat); |
| 13308 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13309 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13311 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13312 | arglen = PyTuple_Size(args); |
| 13313 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13314 | } |
| 13315 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13316 | arglen = -1; |
| 13317 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13318 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13319 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13320 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13321 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13322 | |
| 13323 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13324 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13325 | PyObject *nonfmt; |
| 13326 | Py_ssize_t nonfmtpos; |
| 13327 | nonfmtpos = fmtpos++; |
| 13328 | while (fmtcnt >= 0 && |
| 13329 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13330 | fmtpos++; |
| 13331 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13332 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13333 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13334 | if (nonfmt == NULL) |
| 13335 | goto onError; |
| 13336 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13337 | Py_DECREF(nonfmt); |
| 13338 | if (r) |
| 13339 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13340 | } |
| 13341 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13342 | /* Got a format specifier */ |
| 13343 | int flags = 0; |
| 13344 | Py_ssize_t width = -1; |
| 13345 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13346 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13347 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13348 | int isnumok; |
| 13349 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13350 | void *pbuf = NULL; |
| 13351 | Py_ssize_t pindex, len; |
| 13352 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13353 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13354 | fmtpos++; |
| 13355 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13356 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13357 | Py_ssize_t keylen; |
| 13358 | PyObject *key; |
| 13359 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13360 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13361 | if (dict == NULL) { |
| 13362 | PyErr_SetString(PyExc_TypeError, |
| 13363 | "format requires a mapping"); |
| 13364 | goto onError; |
| 13365 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13366 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13367 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13368 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13369 | /* Skip over balanced parentheses */ |
| 13370 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13371 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13372 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13373 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13374 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13375 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13376 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13377 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13378 | if (fmtcnt < 0 || pcount > 0) { |
| 13379 | PyErr_SetString(PyExc_ValueError, |
| 13380 | "incomplete format key"); |
| 13381 | goto onError; |
| 13382 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13383 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13384 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13385 | if (key == NULL) |
| 13386 | goto onError; |
| 13387 | if (args_owned) { |
| 13388 | Py_DECREF(args); |
| 13389 | args_owned = 0; |
| 13390 | } |
| 13391 | args = PyObject_GetItem(dict, key); |
| 13392 | Py_DECREF(key); |
| 13393 | if (args == NULL) { |
| 13394 | goto onError; |
| 13395 | } |
| 13396 | args_owned = 1; |
| 13397 | arglen = -1; |
| 13398 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13399 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13400 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13401 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13402 | case '-': flags |= F_LJUST; continue; |
| 13403 | case '+': flags |= F_SIGN; continue; |
| 13404 | case ' ': flags |= F_BLANK; continue; |
| 13405 | case '#': flags |= F_ALT; continue; |
| 13406 | case '0': flags |= F_ZERO; continue; |
| 13407 | } |
| 13408 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13409 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13410 | if (c == '*') { |
| 13411 | v = getnextarg(args, arglen, &argidx); |
| 13412 | if (v == NULL) |
| 13413 | goto onError; |
| 13414 | if (!PyLong_Check(v)) { |
| 13415 | PyErr_SetString(PyExc_TypeError, |
| 13416 | "* wants int"); |
| 13417 | goto onError; |
| 13418 | } |
| 13419 | width = PyLong_AsLong(v); |
| 13420 | if (width == -1 && PyErr_Occurred()) |
| 13421 | goto onError; |
| 13422 | if (width < 0) { |
| 13423 | flags |= F_LJUST; |
| 13424 | width = -width; |
| 13425 | } |
| 13426 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13427 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | } |
| 13429 | else if (c >= '0' && c <= '9') { |
| 13430 | width = c - '0'; |
| 13431 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13432 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | if (c < '0' || c > '9') |
| 13434 | break; |
| 13435 | if ((width*10) / 10 != width) { |
| 13436 | PyErr_SetString(PyExc_ValueError, |
| 13437 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13438 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13439 | } |
| 13440 | width = width*10 + (c - '0'); |
| 13441 | } |
| 13442 | } |
| 13443 | if (c == '.') { |
| 13444 | prec = 0; |
| 13445 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13446 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13447 | if (c == '*') { |
| 13448 | v = getnextarg(args, arglen, &argidx); |
| 13449 | if (v == NULL) |
| 13450 | goto onError; |
| 13451 | if (!PyLong_Check(v)) { |
| 13452 | PyErr_SetString(PyExc_TypeError, |
| 13453 | "* wants int"); |
| 13454 | goto onError; |
| 13455 | } |
| 13456 | prec = PyLong_AsLong(v); |
| 13457 | if (prec == -1 && PyErr_Occurred()) |
| 13458 | goto onError; |
| 13459 | if (prec < 0) |
| 13460 | prec = 0; |
| 13461 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13462 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13463 | } |
| 13464 | else if (c >= '0' && c <= '9') { |
| 13465 | prec = c - '0'; |
| 13466 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13467 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13468 | if (c < '0' || c > '9') |
| 13469 | break; |
| 13470 | if ((prec*10) / 10 != prec) { |
| 13471 | PyErr_SetString(PyExc_ValueError, |
| 13472 | "prec too big"); |
| 13473 | goto onError; |
| 13474 | } |
| 13475 | prec = prec*10 + (c - '0'); |
| 13476 | } |
| 13477 | } |
| 13478 | } /* prec */ |
| 13479 | if (fmtcnt >= 0) { |
| 13480 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13481 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13482 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13483 | } |
| 13484 | } |
| 13485 | if (fmtcnt < 0) { |
| 13486 | PyErr_SetString(PyExc_ValueError, |
| 13487 | "incomplete format"); |
| 13488 | goto onError; |
| 13489 | } |
| 13490 | if (c != '%') { |
| 13491 | v = getnextarg(args, arglen, &argidx); |
| 13492 | if (v == NULL) |
| 13493 | goto onError; |
| 13494 | } |
| 13495 | sign = 0; |
| 13496 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13497 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13498 | switch (c) { |
| 13499 | |
| 13500 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13501 | _PyAccu_Accumulate(&acc, percent); |
| 13502 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13503 | |
| 13504 | case 's': |
| 13505 | case 'r': |
| 13506 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13507 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13508 | temp = v; |
| 13509 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13510 | } |
| 13511 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13512 | if (c == 's') |
| 13513 | temp = PyObject_Str(v); |
| 13514 | else if (c == 'r') |
| 13515 | temp = PyObject_Repr(v); |
| 13516 | else |
| 13517 | temp = PyObject_ASCII(v); |
| 13518 | if (temp == NULL) |
| 13519 | goto onError; |
| 13520 | if (PyUnicode_Check(temp)) |
| 13521 | /* nothing to do */; |
| 13522 | else { |
| 13523 | Py_DECREF(temp); |
| 13524 | PyErr_SetString(PyExc_TypeError, |
| 13525 | "%s argument has non-string str()"); |
| 13526 | goto onError; |
| 13527 | } |
| 13528 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13529 | if (PyUnicode_READY(temp) == -1) { |
| 13530 | Py_CLEAR(temp); |
| 13531 | goto onError; |
| 13532 | } |
| 13533 | pbuf = PyUnicode_DATA(temp); |
| 13534 | kind = PyUnicode_KIND(temp); |
| 13535 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13536 | if (prec >= 0 && len > prec) |
| 13537 | len = prec; |
| 13538 | break; |
| 13539 | |
| 13540 | case 'i': |
| 13541 | case 'd': |
| 13542 | case 'u': |
| 13543 | case 'o': |
| 13544 | case 'x': |
| 13545 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13546 | isnumok = 0; |
| 13547 | if (PyNumber_Check(v)) { |
| 13548 | PyObject *iobj=NULL; |
| 13549 | |
| 13550 | if (PyLong_Check(v)) { |
| 13551 | iobj = v; |
| 13552 | Py_INCREF(iobj); |
| 13553 | } |
| 13554 | else { |
| 13555 | iobj = PyNumber_Long(v); |
| 13556 | } |
| 13557 | if (iobj!=NULL) { |
| 13558 | if (PyLong_Check(iobj)) { |
| 13559 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13560 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13561 | Py_DECREF(iobj); |
| 13562 | if (!temp) |
| 13563 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13564 | if (PyUnicode_READY(temp) == -1) { |
| 13565 | Py_CLEAR(temp); |
| 13566 | goto onError; |
| 13567 | } |
| 13568 | pbuf = PyUnicode_DATA(temp); |
| 13569 | kind = PyUnicode_KIND(temp); |
| 13570 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13571 | sign = 1; |
| 13572 | } |
| 13573 | else { |
| 13574 | Py_DECREF(iobj); |
| 13575 | } |
| 13576 | } |
| 13577 | } |
| 13578 | if (!isnumok) { |
| 13579 | PyErr_Format(PyExc_TypeError, |
| 13580 | "%%%c format: a number is required, " |
| 13581 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13582 | goto onError; |
| 13583 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13584 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13585 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13586 | fillobj = zero; |
| 13587 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13588 | break; |
| 13589 | |
| 13590 | case 'e': |
| 13591 | case 'E': |
| 13592 | case 'f': |
| 13593 | case 'F': |
| 13594 | case 'g': |
| 13595 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13596 | temp = formatfloat(v, flags, prec, c); |
| 13597 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13598 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13599 | if (PyUnicode_READY(temp) == -1) { |
| 13600 | Py_CLEAR(temp); |
| 13601 | goto onError; |
| 13602 | } |
| 13603 | pbuf = PyUnicode_DATA(temp); |
| 13604 | kind = PyUnicode_KIND(temp); |
| 13605 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13606 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13607 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13608 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13609 | fillobj = zero; |
| 13610 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13611 | break; |
| 13612 | |
| 13613 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13614 | { |
| 13615 | Py_UCS4 ch = formatchar(v); |
| 13616 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13617 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13618 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13619 | if (temp == NULL) |
| 13620 | goto onError; |
| 13621 | pbuf = PyUnicode_DATA(temp); |
| 13622 | kind = PyUnicode_KIND(temp); |
| 13623 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13624 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13625 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13626 | |
| 13627 | default: |
| 13628 | PyErr_Format(PyExc_ValueError, |
| 13629 | "unsupported format character '%c' (0x%x) " |
| 13630 | "at index %zd", |
| 13631 | (31<=c && c<=126) ? (char)c : '?', |
| 13632 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13633 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13634 | goto onError; |
| 13635 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13636 | /* pbuf is initialized here. */ |
| 13637 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13638 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13639 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13640 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13641 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13642 | pindex++; |
| 13643 | } |
| 13644 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13645 | signobj = plus; |
| 13646 | len--; |
| 13647 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13648 | } |
| 13649 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13650 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13651 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13652 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13653 | else |
| 13654 | sign = 0; |
| 13655 | } |
| 13656 | if (width < len) |
| 13657 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13658 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13659 | if (fill != ' ') { |
| 13660 | assert(signobj != NULL); |
| 13661 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13662 | goto onError; |
| 13663 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13664 | if (width > len) |
| 13665 | width--; |
| 13666 | } |
| 13667 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13668 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13669 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13670 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13671 | second = get_latin1_char( |
| 13672 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13673 | pindex += 2; |
| 13674 | if (second == NULL || |
| 13675 | _PyAccu_Accumulate(&acc, zero) || |
| 13676 | _PyAccu_Accumulate(&acc, second)) |
| 13677 | goto onError; |
| 13678 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13679 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13680 | width -= 2; |
| 13681 | if (width < 0) |
| 13682 | width = 0; |
| 13683 | len -= 2; |
| 13684 | } |
| 13685 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13686 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13687 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13688 | goto onError; |
| 13689 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13690 | } |
| 13691 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13692 | if (sign) { |
| 13693 | assert(signobj != NULL); |
| 13694 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13695 | goto onError; |
| 13696 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13697 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13698 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13699 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13700 | second = get_latin1_char( |
| 13701 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13702 | pindex += 2; |
| 13703 | if (second == NULL || |
| 13704 | _PyAccu_Accumulate(&acc, zero) || |
| 13705 | _PyAccu_Accumulate(&acc, second)) |
| 13706 | goto onError; |
| 13707 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13708 | } |
| 13709 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13710 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13711 | if (temp != NULL) { |
| 13712 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13713 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13714 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13715 | else { |
| 13716 | const char *p = (const char *) pbuf; |
| 13717 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13718 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13719 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13720 | } |
| 13721 | if (v == NULL) |
| 13722 | goto onError; |
| 13723 | r = _PyAccu_Accumulate(&acc, v); |
| 13724 | Py_DECREF(v); |
| 13725 | if (r) |
| 13726 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13727 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13728 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13729 | if (dict && (argidx < arglen) && c != '%') { |
| 13730 | PyErr_SetString(PyExc_TypeError, |
| 13731 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13732 | goto onError; |
| 13733 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13734 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13735 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13736 | } /* until end */ |
| 13737 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13738 | PyErr_SetString(PyExc_TypeError, |
| 13739 | "not all arguments converted during string formatting"); |
| 13740 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13741 | } |
| 13742 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13743 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13744 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13745 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13746 | } |
| 13747 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13748 | Py_XDECREF(temp); |
| 13749 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13750 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13752 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13753 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13754 | Py_XDECREF(temp); |
| 13755 | Py_XDECREF(second); |
| 13756 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13757 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13758 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13759 | } |
| 13760 | return NULL; |
| 13761 | } |
| 13762 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13763 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13764 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13765 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13766 | static PyObject * |
| 13767 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13768 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13769 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13770 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13771 | char *encoding = NULL; |
| 13772 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13773 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13774 | if (type != &PyUnicode_Type) |
| 13775 | return unicode_subtype_new(type, args, kwds); |
| 13776 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13777 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13778 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13779 | if (x == NULL) { |
| 13780 | Py_INCREF(unicode_empty); |
| 13781 | return unicode_empty; |
| 13782 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13783 | if (encoding == NULL && errors == NULL) |
| 13784 | return PyObject_Str(x); |
| 13785 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13786 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13787 | } |
| 13788 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13789 | static PyObject * |
| 13790 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13791 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13792 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13793 | Py_ssize_t length, char_size; |
| 13794 | int share_wstr, share_utf8; |
| 13795 | unsigned int kind; |
| 13796 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13798 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13799 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13800 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13801 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13802 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13803 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13804 | if (PyUnicode_READY(unicode)) { |
| 13805 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13806 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13807 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13808 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13809 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13810 | if (self == NULL) { |
| 13811 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13812 | return NULL; |
| 13813 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13814 | kind = PyUnicode_KIND(unicode); |
| 13815 | length = PyUnicode_GET_LENGTH(unicode); |
| 13816 | |
| 13817 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13818 | #ifdef Py_DEBUG |
| 13819 | _PyUnicode_HASH(self) = -1; |
| 13820 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13821 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13822 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13823 | _PyUnicode_STATE(self).interned = 0; |
| 13824 | _PyUnicode_STATE(self).kind = kind; |
| 13825 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13826 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13827 | _PyUnicode_STATE(self).ready = 1; |
| 13828 | _PyUnicode_WSTR(self) = NULL; |
| 13829 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13830 | _PyUnicode_UTF8(self) = NULL; |
| 13831 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13832 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13833 | |
| 13834 | share_utf8 = 0; |
| 13835 | share_wstr = 0; |
| 13836 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13837 | char_size = 1; |
| 13838 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13839 | share_utf8 = 1; |
| 13840 | } |
| 13841 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13842 | char_size = 2; |
| 13843 | if (sizeof(wchar_t) == 2) |
| 13844 | share_wstr = 1; |
| 13845 | } |
| 13846 | else { |
| 13847 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13848 | char_size = 4; |
| 13849 | if (sizeof(wchar_t) == 4) |
| 13850 | share_wstr = 1; |
| 13851 | } |
| 13852 | |
| 13853 | /* Ensure we won't overflow the length. */ |
| 13854 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13855 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13856 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13857 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13858 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13859 | if (data == NULL) { |
| 13860 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13861 | goto onError; |
| 13862 | } |
| 13863 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13864 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13865 | if (share_utf8) { |
| 13866 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13867 | _PyUnicode_UTF8(self) = data; |
| 13868 | } |
| 13869 | if (share_wstr) { |
| 13870 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13871 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13872 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13873 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13874 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13875 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13876 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13877 | #ifdef Py_DEBUG |
| 13878 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13879 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13880 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13881 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13882 | |
| 13883 | onError: |
| 13884 | Py_DECREF(unicode); |
| 13885 | Py_DECREF(self); |
| 13886 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13887 | } |
| 13888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13889 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13890 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13891 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13892 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13893 | encoding defaults to the current default string encoding.\n\ |
| 13894 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13895 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13896 | static PyObject *unicode_iter(PyObject *seq); |
| 13897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13898 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13899 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13900 | "str", /* tp_name */ |
| 13901 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13902 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13903 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13904 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13905 | 0, /* tp_print */ |
| 13906 | 0, /* tp_getattr */ |
| 13907 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13908 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13909 | unicode_repr, /* tp_repr */ |
| 13910 | &unicode_as_number, /* tp_as_number */ |
| 13911 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13912 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13913 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13914 | 0, /* tp_call*/ |
| 13915 | (reprfunc) unicode_str, /* tp_str */ |
| 13916 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13917 | 0, /* tp_setattro */ |
| 13918 | 0, /* tp_as_buffer */ |
| 13919 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13920 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13921 | unicode_doc, /* tp_doc */ |
| 13922 | 0, /* tp_traverse */ |
| 13923 | 0, /* tp_clear */ |
| 13924 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13925 | 0, /* tp_weaklistoffset */ |
| 13926 | unicode_iter, /* tp_iter */ |
| 13927 | 0, /* tp_iternext */ |
| 13928 | unicode_methods, /* tp_methods */ |
| 13929 | 0, /* tp_members */ |
| 13930 | 0, /* tp_getset */ |
| 13931 | &PyBaseObject_Type, /* tp_base */ |
| 13932 | 0, /* tp_dict */ |
| 13933 | 0, /* tp_descr_get */ |
| 13934 | 0, /* tp_descr_set */ |
| 13935 | 0, /* tp_dictoffset */ |
| 13936 | 0, /* tp_init */ |
| 13937 | 0, /* tp_alloc */ |
| 13938 | unicode_new, /* tp_new */ |
| 13939 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13940 | }; |
| 13941 | |
| 13942 | /* Initialize the Unicode implementation */ |
| 13943 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13944 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13945 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13946 | int i; |
| 13947 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13948 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13949 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13950 | 0x000A, /* LINE FEED */ |
| 13951 | 0x000D, /* CARRIAGE RETURN */ |
| 13952 | 0x001C, /* FILE SEPARATOR */ |
| 13953 | 0x001D, /* GROUP SEPARATOR */ |
| 13954 | 0x001E, /* RECORD SEPARATOR */ |
| 13955 | 0x0085, /* NEXT LINE */ |
| 13956 | 0x2028, /* LINE SEPARATOR */ |
| 13957 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13958 | }; |
| 13959 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13960 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13961 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13962 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13963 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 13964 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13965 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13966 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13967 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13968 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13969 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13970 | |
| 13971 | /* initialize the linebreak bloom filter */ |
| 13972 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13973 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13974 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13975 | |
| 13976 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13977 | |
| 13978 | #ifdef HAVE_MBCS |
| 13979 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13980 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13981 | PyErr_SetFromWindowsErr(0); |
| 13982 | return -1; |
| 13983 | } |
| 13984 | #endif |
| 13985 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13986 | } |
| 13987 | |
| 13988 | /* Finalize the Unicode implementation */ |
| 13989 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13990 | int |
| 13991 | PyUnicode_ClearFreeList(void) |
| 13992 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13993 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13994 | } |
| 13995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13996 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13997 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13998 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13999 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14000 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14001 | Py_XDECREF(unicode_empty); |
| 14002 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14003 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14004 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14005 | if (unicode_latin1[i]) { |
| 14006 | Py_DECREF(unicode_latin1[i]); |
| 14007 | unicode_latin1[i] = NULL; |
| 14008 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14009 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14010 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14011 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14012 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14013 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14014 | void |
| 14015 | PyUnicode_InternInPlace(PyObject **p) |
| 14016 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14017 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14018 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14019 | #ifdef Py_DEBUG |
| 14020 | assert(s != NULL); |
| 14021 | assert(_PyUnicode_CHECK(s)); |
| 14022 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14023 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14024 | return; |
| 14025 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14026 | /* If it's a subclass, we don't really know what putting |
| 14027 | it in the interned dict might do. */ |
| 14028 | if (!PyUnicode_CheckExact(s)) |
| 14029 | return; |
| 14030 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14031 | return; |
| 14032 | if (interned == NULL) { |
| 14033 | interned = PyDict_New(); |
| 14034 | if (interned == NULL) { |
| 14035 | PyErr_Clear(); /* Don't leave an exception */ |
| 14036 | return; |
| 14037 | } |
| 14038 | } |
| 14039 | /* It might be that the GetItem call fails even |
| 14040 | though the key is present in the dictionary, |
| 14041 | namely when this happens during a stack overflow. */ |
| 14042 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14043 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14044 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14046 | if (t) { |
| 14047 | Py_INCREF(t); |
| 14048 | Py_DECREF(*p); |
| 14049 | *p = t; |
| 14050 | return; |
| 14051 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14052 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14053 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14054 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14055 | PyErr_Clear(); |
| 14056 | PyThreadState_GET()->recursion_critical = 0; |
| 14057 | return; |
| 14058 | } |
| 14059 | PyThreadState_GET()->recursion_critical = 0; |
| 14060 | /* The two references in interned are not counted by refcnt. |
| 14061 | The deallocator will take care of this */ |
| 14062 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14063 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14064 | } |
| 14065 | |
| 14066 | void |
| 14067 | PyUnicode_InternImmortal(PyObject **p) |
| 14068 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14069 | PyUnicode_InternInPlace(p); |
| 14070 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14071 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14072 | Py_INCREF(*p); |
| 14073 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14074 | } |
| 14075 | |
| 14076 | PyObject * |
| 14077 | PyUnicode_InternFromString(const char *cp) |
| 14078 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14079 | PyObject *s = PyUnicode_FromString(cp); |
| 14080 | if (s == NULL) |
| 14081 | return NULL; |
| 14082 | PyUnicode_InternInPlace(&s); |
| 14083 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14084 | } |
| 14085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14086 | void |
| 14087 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14088 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14089 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14090 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14091 | Py_ssize_t i, n; |
| 14092 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14093 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14094 | if (interned == NULL || !PyDict_Check(interned)) |
| 14095 | return; |
| 14096 | keys = PyDict_Keys(interned); |
| 14097 | if (keys == NULL || !PyList_Check(keys)) { |
| 14098 | PyErr_Clear(); |
| 14099 | return; |
| 14100 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14101 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14102 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14103 | detector, interned unicode strings are not forcibly deallocated; |
| 14104 | rather, we give them their stolen references back, and then clear |
| 14105 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14106 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14107 | n = PyList_GET_SIZE(keys); |
| 14108 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14109 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14110 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14111 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14112 | if (PyUnicode_READY(s) == -1) { |
| 14113 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14114 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14115 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14116 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14117 | case SSTATE_NOT_INTERNED: |
| 14118 | /* XXX Shouldn't happen */ |
| 14119 | break; |
| 14120 | case SSTATE_INTERNED_IMMORTAL: |
| 14121 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14122 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14123 | break; |
| 14124 | case SSTATE_INTERNED_MORTAL: |
| 14125 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14126 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14127 | break; |
| 14128 | default: |
| 14129 | Py_FatalError("Inconsistent interned string state."); |
| 14130 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14131 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14132 | } |
| 14133 | fprintf(stderr, "total size of all interned strings: " |
| 14134 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14135 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14136 | Py_DECREF(keys); |
| 14137 | PyDict_Clear(interned); |
| 14138 | Py_DECREF(interned); |
| 14139 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14140 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14141 | |
| 14142 | |
| 14143 | /********************* Unicode Iterator **************************/ |
| 14144 | |
| 14145 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14146 | PyObject_HEAD |
| 14147 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14148 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14149 | } unicodeiterobject; |
| 14150 | |
| 14151 | static void |
| 14152 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14153 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14154 | _PyObject_GC_UNTRACK(it); |
| 14155 | Py_XDECREF(it->it_seq); |
| 14156 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14157 | } |
| 14158 | |
| 14159 | static int |
| 14160 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14161 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14162 | Py_VISIT(it->it_seq); |
| 14163 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14164 | } |
| 14165 | |
| 14166 | static PyObject * |
| 14167 | unicodeiter_next(unicodeiterobject *it) |
| 14168 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14169 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14170 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14171 | assert(it != NULL); |
| 14172 | seq = it->it_seq; |
| 14173 | if (seq == NULL) |
| 14174 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14175 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14177 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14178 | int kind = PyUnicode_KIND(seq); |
| 14179 | void *data = PyUnicode_DATA(seq); |
| 14180 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14181 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14182 | if (item != NULL) |
| 14183 | ++it->it_index; |
| 14184 | return item; |
| 14185 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14186 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14187 | Py_DECREF(seq); |
| 14188 | it->it_seq = NULL; |
| 14189 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14190 | } |
| 14191 | |
| 14192 | static PyObject * |
| 14193 | unicodeiter_len(unicodeiterobject *it) |
| 14194 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14195 | Py_ssize_t len = 0; |
| 14196 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14197 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14198 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14199 | } |
| 14200 | |
| 14201 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14202 | |
| 14203 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14204 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14205 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14206 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14207 | }; |
| 14208 | |
| 14209 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14210 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14211 | "str_iterator", /* tp_name */ |
| 14212 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14213 | 0, /* tp_itemsize */ |
| 14214 | /* methods */ |
| 14215 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14216 | 0, /* tp_print */ |
| 14217 | 0, /* tp_getattr */ |
| 14218 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14219 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14220 | 0, /* tp_repr */ |
| 14221 | 0, /* tp_as_number */ |
| 14222 | 0, /* tp_as_sequence */ |
| 14223 | 0, /* tp_as_mapping */ |
| 14224 | 0, /* tp_hash */ |
| 14225 | 0, /* tp_call */ |
| 14226 | 0, /* tp_str */ |
| 14227 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14228 | 0, /* tp_setattro */ |
| 14229 | 0, /* tp_as_buffer */ |
| 14230 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14231 | 0, /* tp_doc */ |
| 14232 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14233 | 0, /* tp_clear */ |
| 14234 | 0, /* tp_richcompare */ |
| 14235 | 0, /* tp_weaklistoffset */ |
| 14236 | PyObject_SelfIter, /* tp_iter */ |
| 14237 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14238 | unicodeiter_methods, /* tp_methods */ |
| 14239 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14240 | }; |
| 14241 | |
| 14242 | static PyObject * |
| 14243 | unicode_iter(PyObject *seq) |
| 14244 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14245 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14246 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14247 | if (!PyUnicode_Check(seq)) { |
| 14248 | PyErr_BadInternalCall(); |
| 14249 | return NULL; |
| 14250 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14251 | if (PyUnicode_READY(seq) == -1) |
| 14252 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14253 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14254 | if (it == NULL) |
| 14255 | return NULL; |
| 14256 | it->it_index = 0; |
| 14257 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14258 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14259 | _PyObject_GC_TRACK(it); |
| 14260 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14261 | } |
| 14262 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14263 | |
| 14264 | size_t |
| 14265 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14266 | { |
| 14267 | int res = 0; |
| 14268 | while(*u++) |
| 14269 | res++; |
| 14270 | return res; |
| 14271 | } |
| 14272 | |
| 14273 | Py_UNICODE* |
| 14274 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14275 | { |
| 14276 | Py_UNICODE *u = s1; |
| 14277 | while ((*u++ = *s2++)); |
| 14278 | return s1; |
| 14279 | } |
| 14280 | |
| 14281 | Py_UNICODE* |
| 14282 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14283 | { |
| 14284 | Py_UNICODE *u = s1; |
| 14285 | while ((*u++ = *s2++)) |
| 14286 | if (n-- == 0) |
| 14287 | break; |
| 14288 | return s1; |
| 14289 | } |
| 14290 | |
| 14291 | Py_UNICODE* |
| 14292 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14293 | { |
| 14294 | Py_UNICODE *u1 = s1; |
| 14295 | u1 += Py_UNICODE_strlen(u1); |
| 14296 | Py_UNICODE_strcpy(u1, s2); |
| 14297 | return s1; |
| 14298 | } |
| 14299 | |
| 14300 | int |
| 14301 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14302 | { |
| 14303 | while (*s1 && *s2 && *s1 == *s2) |
| 14304 | s1++, s2++; |
| 14305 | if (*s1 && *s2) |
| 14306 | return (*s1 < *s2) ? -1 : +1; |
| 14307 | if (*s1) |
| 14308 | return 1; |
| 14309 | if (*s2) |
| 14310 | return -1; |
| 14311 | return 0; |
| 14312 | } |
| 14313 | |
| 14314 | int |
| 14315 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14316 | { |
| 14317 | register Py_UNICODE u1, u2; |
| 14318 | for (; n != 0; n--) { |
| 14319 | u1 = *s1; |
| 14320 | u2 = *s2; |
| 14321 | if (u1 != u2) |
| 14322 | return (u1 < u2) ? -1 : +1; |
| 14323 | if (u1 == '\0') |
| 14324 | return 0; |
| 14325 | s1++; |
| 14326 | s2++; |
| 14327 | } |
| 14328 | return 0; |
| 14329 | } |
| 14330 | |
| 14331 | Py_UNICODE* |
| 14332 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14333 | { |
| 14334 | const Py_UNICODE *p; |
| 14335 | for (p = s; *p; p++) |
| 14336 | if (*p == c) |
| 14337 | return (Py_UNICODE*)p; |
| 14338 | return NULL; |
| 14339 | } |
| 14340 | |
| 14341 | Py_UNICODE* |
| 14342 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14343 | { |
| 14344 | const Py_UNICODE *p; |
| 14345 | p = s + Py_UNICODE_strlen(s); |
| 14346 | while (p != s) { |
| 14347 | p--; |
| 14348 | if (*p == c) |
| 14349 | return (Py_UNICODE*)p; |
| 14350 | } |
| 14351 | return NULL; |
| 14352 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14353 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14354 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14355 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14356 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14357 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14358 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14360 | if (!PyUnicode_Check(unicode)) { |
| 14361 | PyErr_BadArgument(); |
| 14362 | return NULL; |
| 14363 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14364 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14365 | if (u == NULL) |
| 14366 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14367 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14368 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14369 | PyErr_NoMemory(); |
| 14370 | return NULL; |
| 14371 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14372 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14373 | size *= sizeof(Py_UNICODE); |
| 14374 | copy = PyMem_Malloc(size); |
| 14375 | if (copy == NULL) { |
| 14376 | PyErr_NoMemory(); |
| 14377 | return NULL; |
| 14378 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14379 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14380 | return copy; |
| 14381 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14382 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14383 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14384 | to the string.Formatter class implemented in Python. */ |
| 14385 | |
| 14386 | static PyMethodDef _string_methods[] = { |
| 14387 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14388 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14389 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14390 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14391 | {NULL, NULL} |
| 14392 | }; |
| 14393 | |
| 14394 | static struct PyModuleDef _string_module = { |
| 14395 | PyModuleDef_HEAD_INIT, |
| 14396 | "_string", |
| 14397 | PyDoc_STR("string helper module"), |
| 14398 | 0, |
| 14399 | _string_methods, |
| 14400 | NULL, |
| 14401 | NULL, |
| 14402 | NULL, |
| 14403 | NULL |
| 14404 | }; |
| 14405 | |
| 14406 | PyMODINIT_FUNC |
| 14407 | PyInit__string(void) |
| 14408 | { |
| 14409 | return PyModule_Create(&_string_module); |
| 14410 | } |
| 14411 | |
| 14412 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14413 | #ifdef __cplusplus |
| 14414 | } |
| 14415 | #endif |