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" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 49 | /* Endianness switches; defaults to little endian */ |
| 50 | |
| 51 | #ifdef WORDS_BIGENDIAN |
| 52 | # define BYTEORDER_IS_BIG_ENDIAN |
| 53 | #else |
| 54 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 55 | #endif |
| 56 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 57 | /* --- Globals ------------------------------------------------------------ |
| 58 | |
| 59 | The globals are initialized by the _PyUnicode_Init() API and should |
| 60 | not be used before calling that API. |
| 61 | |
| 62 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 69 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 70 | #define MAX_UNICODE 0x10ffff |
| 71 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 72 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 73 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 74 | #else |
| 75 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 76 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 77 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 78 | #define _PyUnicode_UTF8(op) \ |
| 79 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 80 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 81 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 82 | assert(PyUnicode_IS_READY(op)), \ |
| 83 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 84 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 85 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 86 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 87 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 88 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 89 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 90 | assert(PyUnicode_IS_READY(op)), \ |
| 91 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 92 | ((PyASCIIObject*)(op))->length : \ |
| 93 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 94 | #define _PyUnicode_WSTR(op) \ |
| 95 | (((PyASCIIObject*)(op))->wstr) |
| 96 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 97 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 98 | #define _PyUnicode_LENGTH(op) \ |
| 99 | (((PyASCIIObject *)(op))->length) |
| 100 | #define _PyUnicode_STATE(op) \ |
| 101 | (((PyASCIIObject *)(op))->state) |
| 102 | #define _PyUnicode_HASH(op) \ |
| 103 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 104 | #define _PyUnicode_KIND(op) \ |
| 105 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 107 | #define _PyUnicode_GET_LENGTH(op) \ |
| 108 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 110 | #define _PyUnicode_DATA_ANY(op) \ |
| 111 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 112 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | #undef PyUnicode_READY |
| 114 | #define PyUnicode_READY(op) \ |
| 115 | (assert(_PyUnicode_CHECK(op)), \ |
| 116 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 117 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 118 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 120 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 121 | (assert(_PyUnicode_CHECK(op)), \ |
| 122 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 123 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 124 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 125 | (assert(_PyUnicode_CHECK(op)), \ |
| 126 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 127 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 128 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 129 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 130 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 131 | (assert(_PyUnicode_CHECK(op)), \ |
| 132 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 133 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 134 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 135 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 136 | /* true if the Unicode object has an allocated wstr memory block |
| 137 | (not shared with other data) */ |
| 138 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 139 | (assert(_PyUnicode_CHECK(op)), \ |
| 140 | (_PyUnicode_WSTR(op) && \ |
| 141 | (!PyUnicode_IS_READY(op) || \ |
| 142 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 143 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 144 | /* Generic helper macro to convert characters of different types. |
| 145 | from_type and to_type have to be valid type names, begin and end |
| 146 | are pointers to the source characters which should be of type |
| 147 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 148 | buffer where the result characters are written to. */ |
| 149 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 150 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 151 | to_type *_to = (to_type *) to; \ |
| 152 | const from_type *_iter = (begin); \ |
| 153 | const from_type *_end = (end); \ |
| 154 | Py_ssize_t n = (_end) - (_iter); \ |
| 155 | const from_type *_unrolled_end = \ |
| 156 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 157 | while (_iter < (_unrolled_end)) { \ |
| 158 | _to[0] = (to_type) _iter[0]; \ |
| 159 | _to[1] = (to_type) _iter[1]; \ |
| 160 | _to[2] = (to_type) _iter[2]; \ |
| 161 | _to[3] = (to_type) _iter[3]; \ |
| 162 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 163 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 164 | while (_iter < (_end)) \ |
| 165 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 166 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 167 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 168 | /* This dictionary holds all interned unicode strings. Note that references |
| 169 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 170 | When the interned string reaches a refcnt of 0 the string deallocation |
| 171 | function will delete the reference from this dictionary. |
| 172 | |
| 173 | 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] | 174 | 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] | 175 | */ |
| 176 | static PyObject *interned; |
| 177 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 178 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 179 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 180 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 181 | /* List of static strings. */ |
| 182 | static _Py_Identifier *static_strings; |
| 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* Single character Unicode strings in the Latin-1 range are being |
| 185 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 186 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 187 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 188 | /* Fast detection of the most frequent whitespace characters */ |
| 189 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 190 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 191 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 192 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 193 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 194 | /* case 0x000C: * FORM FEED */ |
| 195 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 196 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 197 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 198 | /* case 0x001C: * FILE SEPARATOR */ |
| 199 | /* case 0x001D: * GROUP SEPARATOR */ |
| 200 | /* case 0x001E: * RECORD SEPARATOR */ |
| 201 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 203 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 204 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 205 | 0, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 209 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 219 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 220 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 221 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 222 | static void copy_characters( |
| 223 | PyObject *to, Py_ssize_t to_start, |
| 224 | PyObject *from, Py_ssize_t from_start, |
| 225 | Py_ssize_t how_many); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 226 | static int unicode_modifiable(PyObject *unicode); |
| 227 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 228 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 229 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 230 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 231 | static PyObject * |
| 232 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 233 | static PyObject * |
| 234 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 235 | static PyObject * |
| 236 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 237 | |
| 238 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 239 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 240 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 241 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 242 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 243 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 244 | static void |
| 245 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 246 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 247 | PyObject *unicode, |
| 248 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 249 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 250 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 251 | /* Same for linebreaks */ |
| 252 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 253 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 254 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 255 | /* 0x000B, * LINE TABULATION */ |
| 256 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 257 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 258 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 260 | /* 0x001C, * FILE SEPARATOR */ |
| 261 | /* 0x001D, * GROUP SEPARATOR */ |
| 262 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 264 | 0, 0, 0, 0, 0, 0, 0, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 268 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 279 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 280 | 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] | 281 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 282 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 283 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 284 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 285 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 286 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 287 | /* This is actually an illegal character, so it should |
| 288 | not be passed to unichr. */ |
| 289 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 290 | #endif |
| 291 | } |
| 292 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 293 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 294 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 295 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 296 | { |
| 297 | PyASCIIObject *ascii; |
| 298 | unsigned int kind; |
| 299 | |
| 300 | assert(PyUnicode_Check(op)); |
| 301 | |
| 302 | ascii = (PyASCIIObject *)op; |
| 303 | kind = ascii->state.kind; |
| 304 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 305 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 306 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(ascii->state.ready == 1); |
| 308 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 309 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 310 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 311 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 313 | if (ascii->state.compact == 1) { |
| 314 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 315 | assert(kind == PyUnicode_1BYTE_KIND |
| 316 | || kind == PyUnicode_2BYTE_KIND |
| 317 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 318 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 320 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 321 | } |
| 322 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 323 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 324 | |
| 325 | data = unicode->data.any; |
| 326 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 327 | assert(ascii->length == 0); |
| 328 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | assert(ascii->state.compact == 0); |
| 330 | assert(ascii->state.ascii == 0); |
| 331 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 332 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 | assert(ascii->wstr != NULL); |
| 334 | assert(data == NULL); |
| 335 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 336 | } |
| 337 | else { |
| 338 | assert(kind == PyUnicode_1BYTE_KIND |
| 339 | || kind == PyUnicode_2BYTE_KIND |
| 340 | || kind == PyUnicode_4BYTE_KIND); |
| 341 | assert(ascii->state.compact == 0); |
| 342 | assert(ascii->state.ready == 1); |
| 343 | assert(data != NULL); |
| 344 | if (ascii->state.ascii) { |
| 345 | assert (compact->utf8 == data); |
| 346 | assert (compact->utf8_length == ascii->length); |
| 347 | } |
| 348 | else |
| 349 | assert (compact->utf8 != data); |
| 350 | } |
| 351 | } |
| 352 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 353 | if ( |
| 354 | #if SIZEOF_WCHAR_T == 2 |
| 355 | kind == PyUnicode_2BYTE_KIND |
| 356 | #else |
| 357 | kind == PyUnicode_4BYTE_KIND |
| 358 | #endif |
| 359 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 360 | { |
| 361 | assert(ascii->wstr == data); |
| 362 | assert(compact->wstr_length == ascii->length); |
| 363 | } else |
| 364 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 365 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 366 | |
| 367 | if (compact->utf8 == NULL) |
| 368 | assert(compact->utf8_length == 0); |
| 369 | if (ascii->wstr == NULL) |
| 370 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 371 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 372 | /* check that the best kind is used */ |
| 373 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 374 | { |
| 375 | Py_ssize_t i; |
| 376 | Py_UCS4 maxchar = 0; |
| 377 | void *data = PyUnicode_DATA(ascii); |
| 378 | for (i=0; i < ascii->length; i++) |
| 379 | { |
| 380 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 381 | if (ch > maxchar) |
| 382 | maxchar = ch; |
| 383 | } |
| 384 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 385 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 386 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 387 | assert(maxchar <= 255); |
| 388 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 389 | else |
| 390 | assert(maxchar < 128); |
| 391 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 392 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 393 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 394 | assert(maxchar <= 0xFFFF); |
| 395 | } |
| 396 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 397 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 398 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 399 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 400 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 401 | return 1; |
| 402 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 403 | #endif |
| 404 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 405 | static PyObject* |
| 406 | unicode_result_wchar(PyObject *unicode) |
| 407 | { |
| 408 | #ifndef Py_DEBUG |
| 409 | Py_ssize_t len; |
| 410 | |
| 411 | assert(Py_REFCNT(unicode) == 1); |
| 412 | |
| 413 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 414 | if (len == 0) { |
| 415 | Py_INCREF(unicode_empty); |
| 416 | Py_DECREF(unicode); |
| 417 | return unicode_empty; |
| 418 | } |
| 419 | |
| 420 | if (len == 1) { |
| 421 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 422 | if (ch < 256) { |
| 423 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 424 | Py_DECREF(unicode); |
| 425 | return latin1_char; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (_PyUnicode_Ready(unicode) < 0) { |
| 430 | Py_XDECREF(unicode); |
| 431 | return NULL; |
| 432 | } |
| 433 | #else |
| 434 | /* don't make the result ready in debug mode to ensure that the caller |
| 435 | makes the string ready before using it */ |
| 436 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 437 | #endif |
| 438 | return unicode; |
| 439 | } |
| 440 | |
| 441 | static PyObject* |
| 442 | unicode_result_ready(PyObject *unicode) |
| 443 | { |
| 444 | Py_ssize_t length; |
| 445 | |
| 446 | length = PyUnicode_GET_LENGTH(unicode); |
| 447 | if (length == 0) { |
| 448 | if (unicode != unicode_empty) { |
| 449 | Py_INCREF(unicode_empty); |
| 450 | Py_DECREF(unicode); |
| 451 | } |
| 452 | return unicode_empty; |
| 453 | } |
| 454 | |
| 455 | if (length == 1) { |
| 456 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 457 | if (ch < 256) { |
| 458 | PyObject *latin1_char = unicode_latin1[ch]; |
| 459 | if (latin1_char != NULL) { |
| 460 | if (unicode != latin1_char) { |
| 461 | Py_INCREF(latin1_char); |
| 462 | Py_DECREF(unicode); |
| 463 | } |
| 464 | return latin1_char; |
| 465 | } |
| 466 | else { |
| 467 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 468 | Py_INCREF(unicode); |
| 469 | unicode_latin1[ch] = unicode; |
| 470 | return unicode; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 476 | return unicode; |
| 477 | } |
| 478 | |
| 479 | static PyObject* |
| 480 | unicode_result(PyObject *unicode) |
| 481 | { |
| 482 | assert(_PyUnicode_CHECK(unicode)); |
| 483 | if (PyUnicode_IS_READY(unicode)) |
| 484 | return unicode_result_ready(unicode); |
| 485 | else |
| 486 | return unicode_result_wchar(unicode); |
| 487 | } |
| 488 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 489 | static PyObject* |
| 490 | unicode_result_unchanged(PyObject *unicode) |
| 491 | { |
| 492 | if (PyUnicode_CheckExact(unicode)) { |
| 493 | if (PyUnicode_READY(unicode) < 0) |
| 494 | return NULL; |
| 495 | Py_INCREF(unicode); |
| 496 | return unicode; |
| 497 | } |
| 498 | else |
| 499 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 500 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 503 | #ifdef HAVE_MBCS |
| 504 | static OSVERSIONINFOEX winver; |
| 505 | #endif |
| 506 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 507 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 508 | |
| 509 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 510 | to keep things simple, we use a single bitmask, using the least 5 |
| 511 | bits from each unicode characters as the bit index. */ |
| 512 | |
| 513 | /* the linebreak mask is set up by Unicode_Init below */ |
| 514 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 515 | #if LONG_BIT >= 128 |
| 516 | #define BLOOM_WIDTH 128 |
| 517 | #elif LONG_BIT >= 64 |
| 518 | #define BLOOM_WIDTH 64 |
| 519 | #elif LONG_BIT >= 32 |
| 520 | #define BLOOM_WIDTH 32 |
| 521 | #else |
| 522 | #error "LONG_BIT is smaller than 32" |
| 523 | #endif |
| 524 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 525 | #define BLOOM_MASK unsigned long |
| 526 | |
| 527 | static BLOOM_MASK bloom_linebreak; |
| 528 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 529 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 530 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 531 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 532 | #define BLOOM_LINEBREAK(ch) \ |
| 533 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 534 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 535 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 536 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 537 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 538 | { |
| 539 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 540 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 541 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | Py_ssize_t i; |
| 543 | |
| 544 | mask = 0; |
| 545 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 546 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 547 | |
| 548 | return mask; |
| 549 | } |
| 550 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 551 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 552 | (BLOOM(mask, chr) \ |
| 553 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 555 | /* Compilation of templated routines */ |
| 556 | |
| 557 | #include "stringlib/asciilib.h" |
| 558 | #include "stringlib/fastsearch.h" |
| 559 | #include "stringlib/partition.h" |
| 560 | #include "stringlib/split.h" |
| 561 | #include "stringlib/count.h" |
| 562 | #include "stringlib/find.h" |
| 563 | #include "stringlib/find_max_char.h" |
| 564 | #include "stringlib/localeutil.h" |
| 565 | #include "stringlib/undef.h" |
| 566 | |
| 567 | #include "stringlib/ucs1lib.h" |
| 568 | #include "stringlib/fastsearch.h" |
| 569 | #include "stringlib/partition.h" |
| 570 | #include "stringlib/split.h" |
| 571 | #include "stringlib/count.h" |
| 572 | #include "stringlib/find.h" |
| 573 | #include "stringlib/find_max_char.h" |
| 574 | #include "stringlib/localeutil.h" |
| 575 | #include "stringlib/undef.h" |
| 576 | |
| 577 | #include "stringlib/ucs2lib.h" |
| 578 | #include "stringlib/fastsearch.h" |
| 579 | #include "stringlib/partition.h" |
| 580 | #include "stringlib/split.h" |
| 581 | #include "stringlib/count.h" |
| 582 | #include "stringlib/find.h" |
| 583 | #include "stringlib/find_max_char.h" |
| 584 | #include "stringlib/localeutil.h" |
| 585 | #include "stringlib/undef.h" |
| 586 | |
| 587 | #include "stringlib/ucs4lib.h" |
| 588 | #include "stringlib/fastsearch.h" |
| 589 | #include "stringlib/partition.h" |
| 590 | #include "stringlib/split.h" |
| 591 | #include "stringlib/count.h" |
| 592 | #include "stringlib/find.h" |
| 593 | #include "stringlib/find_max_char.h" |
| 594 | #include "stringlib/localeutil.h" |
| 595 | #include "stringlib/undef.h" |
| 596 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 597 | #include "stringlib/unicodedefs.h" |
| 598 | #include "stringlib/fastsearch.h" |
| 599 | #include "stringlib/count.h" |
| 600 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 601 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 603 | /* --- Unicode Object ----------------------------------------------------- */ |
| 604 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 605 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 606 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 607 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 608 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 609 | Py_ssize_t size, Py_UCS4 ch, |
| 610 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 611 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 612 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 613 | |
| 614 | switch (kind) { |
| 615 | case PyUnicode_1BYTE_KIND: |
| 616 | { |
| 617 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 618 | if (ch1 == ch) |
| 619 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 620 | else |
| 621 | return -1; |
| 622 | } |
| 623 | case PyUnicode_2BYTE_KIND: |
| 624 | { |
| 625 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 626 | if (ch2 == ch) |
| 627 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 628 | else |
| 629 | return -1; |
| 630 | } |
| 631 | case PyUnicode_4BYTE_KIND: |
| 632 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 633 | default: |
| 634 | assert(0); |
| 635 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 639 | static PyObject* |
| 640 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 641 | { |
| 642 | Py_ssize_t char_size; |
| 643 | Py_ssize_t struct_size; |
| 644 | Py_ssize_t new_size; |
| 645 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 646 | PyObject *new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 647 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 648 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 649 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 650 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 651 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 652 | struct_size = sizeof(PyASCIIObject); |
| 653 | else |
| 654 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 655 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 656 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 658 | PyErr_NoMemory(); |
| 659 | return NULL; |
| 660 | } |
| 661 | new_size = (struct_size + (length + 1) * char_size); |
| 662 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 663 | _Py_DEC_REFTOTAL; |
| 664 | _Py_ForgetReference(unicode); |
| 665 | |
| 666 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 667 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 668 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 669 | PyErr_NoMemory(); |
| 670 | return NULL; |
| 671 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 672 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 673 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 674 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 675 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 676 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 677 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 678 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 679 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 680 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 681 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 682 | length, 0); |
| 683 | return unicode; |
| 684 | } |
| 685 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 686 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 687 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 688 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 689 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 690 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 691 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 692 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 693 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 694 | if (PyUnicode_IS_READY(unicode)) { |
| 695 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 696 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 697 | void *data; |
| 698 | |
| 699 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 700 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 701 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 702 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 703 | |
| 704 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 705 | PyErr_NoMemory(); |
| 706 | return -1; |
| 707 | } |
| 708 | new_size = (length + 1) * char_size; |
| 709 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 710 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 711 | { |
| 712 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 713 | _PyUnicode_UTF8(unicode) = NULL; |
| 714 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 715 | } |
| 716 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 717 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 718 | if (data == NULL) { |
| 719 | PyErr_NoMemory(); |
| 720 | return -1; |
| 721 | } |
| 722 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 723 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 724 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 725 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 726 | } |
| 727 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 728 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 729 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 730 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 731 | _PyUnicode_LENGTH(unicode) = length; |
| 732 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 733 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 734 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 735 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 737 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 738 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 739 | |
| 740 | /* check for integer overflow */ |
| 741 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 742 | PyErr_NoMemory(); |
| 743 | return -1; |
| 744 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 745 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 746 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 747 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 748 | if (!wstr) { |
| 749 | PyErr_NoMemory(); |
| 750 | return -1; |
| 751 | } |
| 752 | _PyUnicode_WSTR(unicode) = wstr; |
| 753 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 754 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 755 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 759 | static PyObject* |
| 760 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 761 | { |
| 762 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 763 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 764 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 765 | |
| 766 | if (PyUnicode_READY(unicode) < 0) |
| 767 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 | |
| 769 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 770 | if (copy == NULL) |
| 771 | return NULL; |
| 772 | |
| 773 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 774 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 775 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 776 | } |
| 777 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 778 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 779 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 780 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 781 | if (w == NULL) |
| 782 | return NULL; |
| 783 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 784 | copy_length = Py_MIN(copy_length, length); |
| 785 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 786 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 787 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 791 | /* 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] | 792 | Ux0000 terminated; some code (e.g. new_identifier) |
| 793 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 794 | |
| 795 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 796 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 797 | |
| 798 | */ |
| 799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 800 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 801 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 802 | #endif |
| 803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 804 | static PyUnicodeObject * |
| 805 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 806 | { |
| 807 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 808 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 809 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 810 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 811 | if (length == 0 && unicode_empty != NULL) { |
| 812 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 813 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 816 | /* Ensure we won't overflow the size. */ |
| 817 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 818 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 819 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 820 | if (length < 0) { |
| 821 | PyErr_SetString(PyExc_SystemError, |
| 822 | "Negative size passed to _PyUnicode_New"); |
| 823 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 826 | #ifdef Py_DEBUG |
| 827 | ++unicode_old_new_calls; |
| 828 | #endif |
| 829 | |
| 830 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 831 | if (unicode == NULL) |
| 832 | return NULL; |
| 833 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 834 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 835 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 836 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 837 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 838 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 839 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 840 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 841 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 842 | * the caller fails before initializing str -- unicode_resize() |
| 843 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 844 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 845 | * We don't want unicode_resize to read uninitialized memory in |
| 846 | * that case. |
| 847 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 848 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 849 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 850 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 851 | _PyUnicode_HASH(unicode) = -1; |
| 852 | _PyUnicode_STATE(unicode).interned = 0; |
| 853 | _PyUnicode_STATE(unicode).kind = 0; |
| 854 | _PyUnicode_STATE(unicode).compact = 0; |
| 855 | _PyUnicode_STATE(unicode).ready = 0; |
| 856 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 857 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 858 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 859 | _PyUnicode_UTF8(unicode) = NULL; |
| 860 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 861 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 862 | return unicode; |
| 863 | } |
| 864 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 865 | static const char* |
| 866 | unicode_kind_name(PyObject *unicode) |
| 867 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 868 | /* don't check consistency: unicode_kind_name() is called from |
| 869 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 870 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 871 | { |
| 872 | if (!PyUnicode_IS_READY(unicode)) |
| 873 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 874 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 875 | { |
| 876 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 877 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 878 | return "legacy ascii"; |
| 879 | else |
| 880 | return "legacy latin1"; |
| 881 | case PyUnicode_2BYTE_KIND: |
| 882 | return "legacy UCS2"; |
| 883 | case PyUnicode_4BYTE_KIND: |
| 884 | return "legacy UCS4"; |
| 885 | default: |
| 886 | return "<legacy invalid kind>"; |
| 887 | } |
| 888 | } |
| 889 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 890 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 891 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 892 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 893 | return "ascii"; |
| 894 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 895 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 896 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 897 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 898 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 899 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 900 | default: |
| 901 | return "<invalid compact kind>"; |
| 902 | } |
| 903 | } |
| 904 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 906 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 907 | |
| 908 | /* Functions wrapping macros for use in debugger */ |
| 909 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 910 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | void *_PyUnicode_compact_data(void *unicode) { |
| 914 | return _PyUnicode_COMPACT_DATA(unicode); |
| 915 | } |
| 916 | void *_PyUnicode_data(void *unicode){ |
| 917 | printf("obj %p\n", unicode); |
| 918 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 919 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 920 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 921 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 922 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 923 | return PyUnicode_DATA(unicode); |
| 924 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 925 | |
| 926 | void |
| 927 | _PyUnicode_Dump(PyObject *op) |
| 928 | { |
| 929 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 930 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 931 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 932 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 933 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 934 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 935 | { |
| 936 | if (ascii->state.ascii) |
| 937 | data = (ascii + 1); |
| 938 | else |
| 939 | data = (compact + 1); |
| 940 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 941 | else |
| 942 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 943 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 944 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 945 | if (ascii->wstr == data) |
| 946 | printf("shared "); |
| 947 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 948 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 949 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 950 | printf(" (%zu), ", compact->wstr_length); |
| 951 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 952 | printf("shared "); |
| 953 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 954 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 955 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 956 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | #endif |
| 958 | |
| 959 | PyObject * |
| 960 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 961 | { |
| 962 | PyObject *obj; |
| 963 | PyCompactUnicodeObject *unicode; |
| 964 | void *data; |
| 965 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 966 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 967 | Py_ssize_t char_size; |
| 968 | Py_ssize_t struct_size; |
| 969 | |
| 970 | /* Optimization for empty strings */ |
| 971 | if (size == 0 && unicode_empty != NULL) { |
| 972 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 973 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | #ifdef Py_DEBUG |
| 977 | ++unicode_new_new_calls; |
| 978 | #endif |
| 979 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 980 | is_ascii = 0; |
| 981 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 982 | struct_size = sizeof(PyCompactUnicodeObject); |
| 983 | if (maxchar < 128) { |
| 984 | kind_state = PyUnicode_1BYTE_KIND; |
| 985 | char_size = 1; |
| 986 | is_ascii = 1; |
| 987 | struct_size = sizeof(PyASCIIObject); |
| 988 | } |
| 989 | else if (maxchar < 256) { |
| 990 | kind_state = PyUnicode_1BYTE_KIND; |
| 991 | char_size = 1; |
| 992 | } |
| 993 | else if (maxchar < 65536) { |
| 994 | kind_state = PyUnicode_2BYTE_KIND; |
| 995 | char_size = 2; |
| 996 | if (sizeof(wchar_t) == 2) |
| 997 | is_sharing = 1; |
| 998 | } |
| 999 | else { |
| 1000 | kind_state = PyUnicode_4BYTE_KIND; |
| 1001 | char_size = 4; |
| 1002 | if (sizeof(wchar_t) == 4) |
| 1003 | is_sharing = 1; |
| 1004 | } |
| 1005 | |
| 1006 | /* Ensure we won't overflow the size. */ |
| 1007 | if (size < 0) { |
| 1008 | PyErr_SetString(PyExc_SystemError, |
| 1009 | "Negative size passed to PyUnicode_New"); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1013 | return PyErr_NoMemory(); |
| 1014 | |
| 1015 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1016 | * PyObject_New() so we are able to allocate space for the object and |
| 1017 | * it's data buffer. |
| 1018 | */ |
| 1019 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1020 | if (obj == NULL) |
| 1021 | return PyErr_NoMemory(); |
| 1022 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1023 | if (obj == NULL) |
| 1024 | return NULL; |
| 1025 | |
| 1026 | unicode = (PyCompactUnicodeObject *)obj; |
| 1027 | if (is_ascii) |
| 1028 | data = ((PyASCIIObject*)obj) + 1; |
| 1029 | else |
| 1030 | data = unicode + 1; |
| 1031 | _PyUnicode_LENGTH(unicode) = size; |
| 1032 | _PyUnicode_HASH(unicode) = -1; |
| 1033 | _PyUnicode_STATE(unicode).interned = 0; |
| 1034 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 1035 | _PyUnicode_STATE(unicode).compact = 1; |
| 1036 | _PyUnicode_STATE(unicode).ready = 1; |
| 1037 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1038 | if (is_ascii) { |
| 1039 | ((char*)data)[size] = 0; |
| 1040 | _PyUnicode_WSTR(unicode) = NULL; |
| 1041 | } |
| 1042 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 1043 | ((char*)data)[size] = 0; |
| 1044 | _PyUnicode_WSTR(unicode) = NULL; |
| 1045 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1046 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1047 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | } |
| 1049 | else { |
| 1050 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1051 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 1053 | ((Py_UCS2*)data)[size] = 0; |
| 1054 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 1055 | ((Py_UCS4*)data)[size] = 0; |
| 1056 | if (is_sharing) { |
| 1057 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1058 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1059 | } |
| 1060 | else { |
| 1061 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1062 | _PyUnicode_WSTR(unicode) = NULL; |
| 1063 | } |
| 1064 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1065 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1066 | return obj; |
| 1067 | } |
| 1068 | |
| 1069 | #if SIZEOF_WCHAR_T == 2 |
| 1070 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1071 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1072 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1073 | |
| 1074 | This function assumes that unicode can hold one more code point than wstr |
| 1075 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1076 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1077 | 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] | 1078 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1079 | { |
| 1080 | const wchar_t *iter; |
| 1081 | Py_UCS4 *ucs4_out; |
| 1082 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1083 | assert(unicode != NULL); |
| 1084 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1085 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1086 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1087 | |
| 1088 | for (iter = begin; iter < end; ) { |
| 1089 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1090 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1091 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1092 | && (iter+1) < end |
| 1093 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1094 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1095 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | iter += 2; |
| 1097 | } |
| 1098 | else { |
| 1099 | *ucs4_out++ = *iter; |
| 1100 | iter++; |
| 1101 | } |
| 1102 | } |
| 1103 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1104 | _PyUnicode_GET_LENGTH(unicode))); |
| 1105 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | } |
| 1107 | #endif |
| 1108 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1109 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1110 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1111 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1112 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1113 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1114 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1115 | return -1; |
| 1116 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1120 | static int |
| 1121 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1122 | PyObject *from, Py_ssize_t from_start, |
| 1123 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1124 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1125 | unsigned int from_kind, to_kind; |
| 1126 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1127 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1129 | assert(PyUnicode_Check(from)); |
| 1130 | assert(PyUnicode_Check(to)); |
| 1131 | assert(PyUnicode_IS_READY(from)); |
| 1132 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1133 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1134 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1135 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1136 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1137 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1138 | if (how_many == 0) |
| 1139 | return 0; |
| 1140 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1141 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1142 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1144 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1145 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1146 | #ifdef Py_DEBUG |
| 1147 | if (!check_maxchar |
| 1148 | && (from_kind > to_kind |
| 1149 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1150 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1151 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1152 | Py_UCS4 ch; |
| 1153 | Py_ssize_t i; |
| 1154 | for (i=0; i < how_many; i++) { |
| 1155 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1156 | assert(ch <= to_maxchar); |
| 1157 | } |
| 1158 | } |
| 1159 | #endif |
| 1160 | fast = (from_kind == to_kind); |
| 1161 | if (check_maxchar |
| 1162 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1163 | { |
| 1164 | /* deny latin1 => ascii */ |
| 1165 | fast = 0; |
| 1166 | } |
| 1167 | |
| 1168 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1169 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1170 | (char*)from_data + from_kind * from_start, |
| 1171 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1173 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1174 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1175 | { |
| 1176 | _PyUnicode_CONVERT_BYTES( |
| 1177 | Py_UCS1, Py_UCS2, |
| 1178 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1179 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1180 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1181 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1182 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1183 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1184 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1185 | { |
| 1186 | _PyUnicode_CONVERT_BYTES( |
| 1187 | Py_UCS1, Py_UCS4, |
| 1188 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1189 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1190 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1191 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1192 | } |
| 1193 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1194 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1195 | { |
| 1196 | _PyUnicode_CONVERT_BYTES( |
| 1197 | Py_UCS2, Py_UCS4, |
| 1198 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1199 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1200 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1201 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1202 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1203 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1204 | /* check if max_char(from substring) <= max_char(to) */ |
| 1205 | if (from_kind > to_kind |
| 1206 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1207 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1208 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1209 | /* slow path to check for character overflow */ |
| 1210 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1211 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1212 | Py_ssize_t i; |
| 1213 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1214 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1215 | for (i=0; i < how_many; i++) { |
| 1216 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1217 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1218 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1219 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1220 | #else |
| 1221 | if (!check_maxchar) { |
| 1222 | for (i=0; i < how_many; i++) { |
| 1223 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1224 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1225 | } |
| 1226 | } |
| 1227 | else { |
| 1228 | for (i=0; i < how_many; i++) { |
| 1229 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1230 | if (ch > to_maxchar) |
| 1231 | return 1; |
| 1232 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1233 | } |
| 1234 | } |
| 1235 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1236 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1237 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1238 | assert(0 && "inconsistent state"); |
| 1239 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1240 | } |
| 1241 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static void |
| 1246 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1247 | PyObject *from, Py_ssize_t from_start, |
| 1248 | Py_ssize_t how_many) |
| 1249 | { |
| 1250 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1251 | } |
| 1252 | |
| 1253 | Py_ssize_t |
| 1254 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1255 | PyObject *from, Py_ssize_t from_start, |
| 1256 | Py_ssize_t how_many) |
| 1257 | { |
| 1258 | int err; |
| 1259 | |
| 1260 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1261 | PyErr_BadInternalCall(); |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | if (PyUnicode_READY(from)) |
| 1266 | return -1; |
| 1267 | if (PyUnicode_READY(to)) |
| 1268 | return -1; |
| 1269 | |
| 1270 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1271 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1272 | PyErr_Format(PyExc_SystemError, |
| 1273 | "Cannot write %zi characters at %zi " |
| 1274 | "in a string of %zi characters", |
| 1275 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | |
| 1279 | if (how_many == 0) |
| 1280 | return 0; |
| 1281 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1282 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1283 | return -1; |
| 1284 | |
| 1285 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1286 | if (err) { |
| 1287 | PyErr_Format(PyExc_SystemError, |
| 1288 | "Cannot copy %s characters " |
| 1289 | "into a string of %s characters", |
| 1290 | unicode_kind_name(from), |
| 1291 | unicode_kind_name(to)); |
| 1292 | return -1; |
| 1293 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1294 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1297 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1298 | correct string length can be computed before converting a string to UCS4. |
| 1299 | This function counts single surrogates as a character and not as a pair. |
| 1300 | |
| 1301 | Return 0 on success, or -1 on error. */ |
| 1302 | static int |
| 1303 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1304 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1305 | { |
| 1306 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1307 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1308 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1309 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1310 | *num_surrogates = 0; |
| 1311 | *maxchar = 0; |
| 1312 | |
| 1313 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1314 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1315 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1316 | && (iter+1) < end |
| 1317 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1318 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1319 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1321 | iter += 2; |
| 1322 | } |
| 1323 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1325 | { |
| 1326 | ch = *iter; |
| 1327 | iter++; |
| 1328 | } |
| 1329 | if (ch > *maxchar) { |
| 1330 | *maxchar = ch; |
| 1331 | if (*maxchar > MAX_UNICODE) { |
| 1332 | PyErr_Format(PyExc_ValueError, |
| 1333 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1334 | ch); |
| 1335 | return -1; |
| 1336 | } |
| 1337 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | } |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1343 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1344 | #endif |
| 1345 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1346 | int |
| 1347 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1348 | { |
| 1349 | wchar_t *end; |
| 1350 | Py_UCS4 maxchar = 0; |
| 1351 | Py_ssize_t num_surrogates; |
| 1352 | #if SIZEOF_WCHAR_T == 2 |
| 1353 | Py_ssize_t length_wo_surrogates; |
| 1354 | #endif |
| 1355 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1356 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1357 | strings were created using _PyObject_New() and where no canonical |
| 1358 | representation (the str field) has been set yet aka strings |
| 1359 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1360 | assert(_PyUnicode_CHECK(unicode)); |
| 1361 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1362 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1363 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1364 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1365 | /* Actually, it should neither be interned nor be anything else: */ |
| 1366 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | |
| 1368 | #ifdef Py_DEBUG |
| 1369 | ++unicode_ready_calls; |
| 1370 | #endif |
| 1371 | |
| 1372 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1373 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1374 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1375 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | |
| 1377 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1378 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1379 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 | PyErr_NoMemory(); |
| 1381 | return -1; |
| 1382 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1383 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | _PyUnicode_WSTR(unicode), end, |
| 1385 | PyUnicode_1BYTE_DATA(unicode)); |
| 1386 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1387 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1388 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1389 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1390 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1391 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1392 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | } |
| 1394 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1395 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1396 | _PyUnicode_UTF8(unicode) = NULL; |
| 1397 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1398 | } |
| 1399 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1400 | _PyUnicode_WSTR(unicode) = NULL; |
| 1401 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1402 | } |
| 1403 | /* In this case we might have to convert down from 4-byte native |
| 1404 | wchar_t to 2-byte unicode. */ |
| 1405 | else if (maxchar < 65536) { |
| 1406 | assert(num_surrogates == 0 && |
| 1407 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1408 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1409 | #if SIZEOF_WCHAR_T == 2 |
| 1410 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1411 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1412 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1413 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1414 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1415 | _PyUnicode_UTF8(unicode) = NULL; |
| 1416 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1417 | #else |
| 1418 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1419 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1420 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1421 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1422 | PyErr_NoMemory(); |
| 1423 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1425 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1426 | _PyUnicode_WSTR(unicode), end, |
| 1427 | PyUnicode_2BYTE_DATA(unicode)); |
| 1428 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1429 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1430 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1431 | _PyUnicode_UTF8(unicode) = NULL; |
| 1432 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1433 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1434 | _PyUnicode_WSTR(unicode) = NULL; |
| 1435 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1436 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1439 | else { |
| 1440 | #if SIZEOF_WCHAR_T == 2 |
| 1441 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1442 | new normalized 4-byte version. */ |
| 1443 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1444 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1445 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1446 | PyErr_NoMemory(); |
| 1447 | return -1; |
| 1448 | } |
| 1449 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1450 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1451 | _PyUnicode_UTF8(unicode) = NULL; |
| 1452 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1453 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1454 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1455 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1456 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1457 | _PyUnicode_WSTR(unicode) = NULL; |
| 1458 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1459 | #else |
| 1460 | assert(num_surrogates == 0); |
| 1461 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1462 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1464 | _PyUnicode_UTF8(unicode) = NULL; |
| 1465 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1466 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1467 | #endif |
| 1468 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1469 | } |
| 1470 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1471 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1475 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1476 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1478 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1479 | case SSTATE_NOT_INTERNED: |
| 1480 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1482 | case SSTATE_INTERNED_MORTAL: |
| 1483 | /* revive dead object temporarily for DelItem */ |
| 1484 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1485 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1486 | Py_FatalError( |
| 1487 | "deletion of interned string failed"); |
| 1488 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1490 | case SSTATE_INTERNED_IMMORTAL: |
| 1491 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1492 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1493 | default: |
| 1494 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1497 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1498 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1499 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1500 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1501 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1502 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1503 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1504 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1507 | #ifdef Py_DEBUG |
| 1508 | static int |
| 1509 | unicode_is_singleton(PyObject *unicode) |
| 1510 | { |
| 1511 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1512 | if (unicode == unicode_empty) |
| 1513 | return 1; |
| 1514 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1515 | { |
| 1516 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1517 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1518 | return 1; |
| 1519 | } |
| 1520 | return 0; |
| 1521 | } |
| 1522 | #endif |
| 1523 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1524 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1525 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1526 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1527 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1528 | if (Py_REFCNT(unicode) != 1) |
| 1529 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1530 | if (_PyUnicode_HASH(unicode) != -1) |
| 1531 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1532 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1533 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1534 | if (!PyUnicode_CheckExact(unicode)) |
| 1535 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1536 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1537 | /* singleton refcount is greater than 1 */ |
| 1538 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1539 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1540 | return 1; |
| 1541 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1542 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1543 | static int |
| 1544 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1545 | { |
| 1546 | PyObject *unicode; |
| 1547 | Py_ssize_t old_length; |
| 1548 | |
| 1549 | assert(p_unicode != NULL); |
| 1550 | unicode = *p_unicode; |
| 1551 | |
| 1552 | assert(unicode != NULL); |
| 1553 | assert(PyUnicode_Check(unicode)); |
| 1554 | assert(0 <= length); |
| 1555 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1556 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1557 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1558 | else |
| 1559 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1560 | if (old_length == length) |
| 1561 | return 0; |
| 1562 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1563 | if (length == 0) { |
| 1564 | Py_DECREF(*p_unicode); |
| 1565 | *p_unicode = unicode_empty; |
| 1566 | Py_INCREF(*p_unicode); |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1570 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1571 | PyObject *copy = resize_copy(unicode, length); |
| 1572 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1573 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1574 | Py_DECREF(*p_unicode); |
| 1575 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1576 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1579 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1580 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1581 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1582 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1583 | *p_unicode = new_unicode; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1584 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1585 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1586 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1587 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1590 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1591 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1592 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1593 | PyObject *unicode; |
| 1594 | if (p_unicode == NULL) { |
| 1595 | PyErr_BadInternalCall(); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1599 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1600 | { |
| 1601 | PyErr_BadInternalCall(); |
| 1602 | return -1; |
| 1603 | } |
| 1604 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1605 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1606 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1607 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1608 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1609 | { |
| 1610 | PyObject *result; |
| 1611 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1612 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1613 | return 0; |
| 1614 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1615 | maxchar); |
| 1616 | if (result == NULL) |
| 1617 | return -1; |
| 1618 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1619 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1620 | Py_DECREF(*p_unicode); |
| 1621 | *p_unicode = result; |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | static int |
| 1626 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1627 | Py_UCS4 ch) |
| 1628 | { |
| 1629 | if (unicode_widen(p_unicode, ch) < 0) |
| 1630 | return -1; |
| 1631 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1632 | PyUnicode_DATA(*p_unicode), |
| 1633 | (*pos)++, ch); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1637 | static PyObject* |
| 1638 | get_latin1_char(unsigned char ch) |
| 1639 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1640 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1641 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1642 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1643 | if (!unicode) |
| 1644 | return NULL; |
| 1645 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1646 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1647 | unicode_latin1[ch] = unicode; |
| 1648 | } |
| 1649 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1650 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1651 | } |
| 1652 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1653 | PyObject * |
| 1654 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1655 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1656 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1657 | Py_UCS4 maxchar = 0; |
| 1658 | Py_ssize_t num_surrogates; |
| 1659 | |
| 1660 | if (u == NULL) |
| 1661 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1662 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1663 | /* If the Unicode data is known at construction time, we can apply |
| 1664 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1666 | /* Optimization for empty strings */ |
| 1667 | if (size == 0 && unicode_empty != NULL) { |
| 1668 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1669 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1670 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1672 | /* Single character Unicode objects in the Latin-1 range are |
| 1673 | shared when using this constructor */ |
| 1674 | if (size == 1 && *u < 256) |
| 1675 | return get_latin1_char((unsigned char)*u); |
| 1676 | |
| 1677 | /* If not empty and not single character, copy the Unicode data |
| 1678 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1679 | if (find_maxchar_surrogates(u, u + size, |
| 1680 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1681 | return NULL; |
| 1682 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1683 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1684 | if (!unicode) |
| 1685 | return NULL; |
| 1686 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1687 | switch (PyUnicode_KIND(unicode)) { |
| 1688 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1689 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1690 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1691 | break; |
| 1692 | case PyUnicode_2BYTE_KIND: |
| 1693 | #if Py_UNICODE_SIZE == 2 |
| 1694 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1695 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1696 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1697 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1698 | #endif |
| 1699 | break; |
| 1700 | case PyUnicode_4BYTE_KIND: |
| 1701 | #if SIZEOF_WCHAR_T == 2 |
| 1702 | /* This is the only case which has to process surrogates, thus |
| 1703 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1704 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1705 | #else |
| 1706 | assert(num_surrogates == 0); |
| 1707 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1708 | #endif |
| 1709 | break; |
| 1710 | default: |
| 1711 | assert(0 && "Impossible state"); |
| 1712 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1713 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1714 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1717 | PyObject * |
| 1718 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1719 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1720 | if (size < 0) { |
| 1721 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1722 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1723 | return NULL; |
| 1724 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1725 | if (u != NULL) |
| 1726 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1727 | else |
| 1728 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1731 | PyObject * |
| 1732 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1733 | { |
| 1734 | size_t size = strlen(u); |
| 1735 | if (size > PY_SSIZE_T_MAX) { |
| 1736 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1737 | return NULL; |
| 1738 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1739 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1742 | PyObject * |
| 1743 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1744 | { |
| 1745 | if (!id->object) { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1746 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1747 | strlen(id->string), |
| 1748 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1749 | if (!id->object) |
| 1750 | return NULL; |
| 1751 | PyUnicode_InternInPlace(&id->object); |
| 1752 | assert(!id->next); |
| 1753 | id->next = static_strings; |
| 1754 | static_strings = id; |
| 1755 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1756 | return id->object; |
| 1757 | } |
| 1758 | |
| 1759 | void |
| 1760 | _PyUnicode_ClearStaticStrings() |
| 1761 | { |
| 1762 | _Py_Identifier *i; |
| 1763 | for (i = static_strings; i; i = i->next) { |
| 1764 | Py_DECREF(i->object); |
| 1765 | i->object = NULL; |
| 1766 | i->next = NULL; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1770 | /* Internal function, don't check maximum character */ |
| 1771 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1772 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1773 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1774 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1775 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1776 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1777 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1778 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1779 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1780 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1781 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1782 | unicode = PyUnicode_New(size, 127); |
| 1783 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1784 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1785 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1786 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1787 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1790 | static Py_UCS4 |
| 1791 | kind_maxchar_limit(unsigned int kind) |
| 1792 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1793 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1794 | case PyUnicode_1BYTE_KIND: |
| 1795 | return 0x80; |
| 1796 | case PyUnicode_2BYTE_KIND: |
| 1797 | return 0x100; |
| 1798 | case PyUnicode_4BYTE_KIND: |
| 1799 | return 0x10000; |
| 1800 | default: |
| 1801 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1802 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1806 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1807 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1808 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1809 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1810 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1811 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1812 | if (size == 0) { |
| 1813 | Py_INCREF(unicode_empty); |
| 1814 | return unicode_empty; |
| 1815 | } |
| 1816 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1817 | if (size == 1) |
| 1818 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1819 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1820 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1821 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1822 | if (!res) |
| 1823 | return NULL; |
| 1824 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1825 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1826 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1829 | static PyObject* |
| 1830 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1831 | { |
| 1832 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1833 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1834 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1835 | if (size == 0) { |
| 1836 | Py_INCREF(unicode_empty); |
| 1837 | return unicode_empty; |
| 1838 | } |
| 1839 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1840 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1841 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1842 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1843 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1844 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | if (!res) |
| 1846 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1847 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1849 | else { |
| 1850 | _PyUnicode_CONVERT_BYTES( |
| 1851 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1852 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1853 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1854 | return res; |
| 1855 | } |
| 1856 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1857 | static PyObject* |
| 1858 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1859 | { |
| 1860 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1861 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1862 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1863 | if (size == 0) { |
| 1864 | Py_INCREF(unicode_empty); |
| 1865 | return unicode_empty; |
| 1866 | } |
| 1867 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1868 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1869 | return get_latin1_char((unsigned char)u[0]); |
| 1870 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1871 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1872 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | if (!res) |
| 1874 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1875 | if (max_char < 256) |
| 1876 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1877 | PyUnicode_1BYTE_DATA(res)); |
| 1878 | else if (max_char < 0x10000) |
| 1879 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1880 | PyUnicode_2BYTE_DATA(res)); |
| 1881 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1882 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1883 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1884 | return res; |
| 1885 | } |
| 1886 | |
| 1887 | PyObject* |
| 1888 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1889 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1890 | if (size < 0) { |
| 1891 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1892 | return NULL; |
| 1893 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1894 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1895 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1896 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1897 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1898 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1899 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1900 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1901 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1902 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1903 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1904 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1907 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1908 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1909 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1910 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1911 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1912 | { |
| 1913 | PyObject *unicode, *copy; |
| 1914 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1915 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1916 | unsigned int kind; |
| 1917 | |
| 1918 | assert(p_unicode != NULL); |
| 1919 | unicode = *p_unicode; |
| 1920 | assert(PyUnicode_IS_READY(unicode)); |
| 1921 | if (PyUnicode_IS_ASCII(unicode)) |
| 1922 | return; |
| 1923 | |
| 1924 | len = PyUnicode_GET_LENGTH(unicode); |
| 1925 | kind = PyUnicode_KIND(unicode); |
| 1926 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1927 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1928 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1929 | if (max_char >= 128) |
| 1930 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1931 | } |
| 1932 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1933 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1934 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1935 | if (max_char >= 256) |
| 1936 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1937 | } |
| 1938 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1939 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1940 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1941 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1942 | if (max_char >= 0x10000) |
| 1943 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1944 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1945 | copy = PyUnicode_New(len, max_char); |
| 1946 | copy_characters(copy, 0, unicode, 0, len); |
| 1947 | Py_DECREF(unicode); |
| 1948 | *p_unicode = copy; |
| 1949 | } |
| 1950 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1951 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 1952 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1953 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1954 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1955 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1956 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1957 | if (!PyUnicode_Check(unicode)) { |
| 1958 | PyErr_BadInternalCall(); |
| 1959 | return NULL; |
| 1960 | } |
| 1961 | if (PyUnicode_READY(unicode)) |
| 1962 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1963 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1964 | length = PyUnicode_GET_LENGTH(unicode); |
| 1965 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1966 | if (!copy) |
| 1967 | return NULL; |
| 1968 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1969 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1970 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1971 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1972 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1973 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1976 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1977 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1978 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | |
| 1980 | void* |
| 1981 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1982 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1983 | Py_ssize_t len; |
| 1984 | void *result; |
| 1985 | unsigned int skind; |
| 1986 | |
| 1987 | if (PyUnicode_READY(s)) |
| 1988 | return NULL; |
| 1989 | |
| 1990 | len = PyUnicode_GET_LENGTH(s); |
| 1991 | skind = PyUnicode_KIND(s); |
| 1992 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1993 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1994 | return NULL; |
| 1995 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1996 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1997 | case PyUnicode_2BYTE_KIND: |
| 1998 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1999 | if (!result) |
| 2000 | return PyErr_NoMemory(); |
| 2001 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2002 | _PyUnicode_CONVERT_BYTES( |
| 2003 | Py_UCS1, Py_UCS2, |
| 2004 | PyUnicode_1BYTE_DATA(s), |
| 2005 | PyUnicode_1BYTE_DATA(s) + len, |
| 2006 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2007 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2008 | case PyUnicode_4BYTE_KIND: |
| 2009 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2010 | if (!result) |
| 2011 | return PyErr_NoMemory(); |
| 2012 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2013 | _PyUnicode_CONVERT_BYTES( |
| 2014 | Py_UCS2, Py_UCS4, |
| 2015 | PyUnicode_2BYTE_DATA(s), |
| 2016 | PyUnicode_2BYTE_DATA(s) + len, |
| 2017 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2018 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2019 | else { |
| 2020 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2021 | _PyUnicode_CONVERT_BYTES( |
| 2022 | Py_UCS1, Py_UCS4, |
| 2023 | PyUnicode_1BYTE_DATA(s), |
| 2024 | PyUnicode_1BYTE_DATA(s) + len, |
| 2025 | result); |
| 2026 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2027 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2028 | default: |
| 2029 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2030 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2031 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2032 | return NULL; |
| 2033 | } |
| 2034 | |
| 2035 | static Py_UCS4* |
| 2036 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2037 | int copy_null) |
| 2038 | { |
| 2039 | int kind; |
| 2040 | void *data; |
| 2041 | Py_ssize_t len, targetlen; |
| 2042 | if (PyUnicode_READY(string) == -1) |
| 2043 | return NULL; |
| 2044 | kind = PyUnicode_KIND(string); |
| 2045 | data = PyUnicode_DATA(string); |
| 2046 | len = PyUnicode_GET_LENGTH(string); |
| 2047 | targetlen = len; |
| 2048 | if (copy_null) |
| 2049 | targetlen++; |
| 2050 | if (!target) { |
| 2051 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2052 | PyErr_NoMemory(); |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2056 | if (!target) { |
| 2057 | PyErr_NoMemory(); |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | } |
| 2061 | else { |
| 2062 | if (targetsize < targetlen) { |
| 2063 | PyErr_Format(PyExc_SystemError, |
| 2064 | "string is longer than the buffer"); |
| 2065 | if (copy_null && 0 < targetsize) |
| 2066 | target[0] = 0; |
| 2067 | return NULL; |
| 2068 | } |
| 2069 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2070 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2071 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2072 | _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] | 2073 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2074 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2075 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2076 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2077 | } |
| 2078 | else { |
| 2079 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2080 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2081 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2082 | if (copy_null) |
| 2083 | target[len] = 0; |
| 2084 | return target; |
| 2085 | } |
| 2086 | |
| 2087 | Py_UCS4* |
| 2088 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2089 | int copy_null) |
| 2090 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2091 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2092 | PyErr_BadInternalCall(); |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | return as_ucs4(string, target, targetsize, copy_null); |
| 2096 | } |
| 2097 | |
| 2098 | Py_UCS4* |
| 2099 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2100 | { |
| 2101 | return as_ucs4(string, NULL, 0, 1); |
| 2102 | } |
| 2103 | |
| 2104 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2105 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2106 | PyObject * |
| 2107 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2108 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2109 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2110 | if (size == 0) { |
| 2111 | Py_INCREF(unicode_empty); |
| 2112 | return unicode_empty; |
| 2113 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2114 | PyErr_BadInternalCall(); |
| 2115 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2118 | if (size == -1) { |
| 2119 | size = wcslen(w); |
| 2120 | } |
| 2121 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2122 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2125 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2126 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2127 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2128 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2129 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2130 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2131 | *fmt++ = '%'; |
| 2132 | if (width) { |
| 2133 | if (zeropad) |
| 2134 | *fmt++ = '0'; |
| 2135 | fmt += sprintf(fmt, "%d", width); |
| 2136 | } |
| 2137 | if (precision) |
| 2138 | fmt += sprintf(fmt, ".%d", precision); |
| 2139 | if (longflag) |
| 2140 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2141 | else if (longlongflag) { |
| 2142 | /* longlongflag should only ever be nonzero on machines with |
| 2143 | HAVE_LONG_LONG defined */ |
| 2144 | #ifdef HAVE_LONG_LONG |
| 2145 | char *f = PY_FORMAT_LONG_LONG; |
| 2146 | while (*f) |
| 2147 | *fmt++ = *f++; |
| 2148 | #else |
| 2149 | /* we shouldn't ever get here */ |
| 2150 | assert(0); |
| 2151 | *fmt++ = 'l'; |
| 2152 | #endif |
| 2153 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2154 | else if (size_tflag) { |
| 2155 | char *f = PY_FORMAT_SIZE_T; |
| 2156 | while (*f) |
| 2157 | *fmt++ = *f++; |
| 2158 | } |
| 2159 | *fmt++ = c; |
| 2160 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2163 | /* helper for PyUnicode_FromFormatV() */ |
| 2164 | |
| 2165 | static const char* |
| 2166 | parse_format_flags(const char *f, |
| 2167 | int *p_width, int *p_precision, |
| 2168 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2169 | { |
| 2170 | int width, precision, longflag, longlongflag, size_tflag; |
| 2171 | |
| 2172 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2173 | f++; |
| 2174 | width = 0; |
| 2175 | while (Py_ISDIGIT((unsigned)*f)) |
| 2176 | width = (width*10) + *f++ - '0'; |
| 2177 | precision = 0; |
| 2178 | if (*f == '.') { |
| 2179 | f++; |
| 2180 | while (Py_ISDIGIT((unsigned)*f)) |
| 2181 | precision = (precision*10) + *f++ - '0'; |
| 2182 | if (*f == '%') { |
| 2183 | /* "%.3%s" => f points to "3" */ |
| 2184 | f--; |
| 2185 | } |
| 2186 | } |
| 2187 | if (*f == '\0') { |
| 2188 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2189 | f--; |
| 2190 | } |
| 2191 | if (p_width != NULL) |
| 2192 | *p_width = width; |
| 2193 | if (p_precision != NULL) |
| 2194 | *p_precision = precision; |
| 2195 | |
| 2196 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2197 | longflag = 0; |
| 2198 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2199 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2200 | |
| 2201 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2202 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2203 | longflag = 1; |
| 2204 | ++f; |
| 2205 | } |
| 2206 | #ifdef HAVE_LONG_LONG |
| 2207 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2208 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2209 | longlongflag = 1; |
| 2210 | f += 2; |
| 2211 | } |
| 2212 | #endif |
| 2213 | } |
| 2214 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2215 | 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] | 2216 | size_tflag = 1; |
| 2217 | ++f; |
| 2218 | } |
| 2219 | if (p_longflag != NULL) |
| 2220 | *p_longflag = longflag; |
| 2221 | if (p_longlongflag != NULL) |
| 2222 | *p_longlongflag = longlongflag; |
| 2223 | if (p_size_tflag != NULL) |
| 2224 | *p_size_tflag = size_tflag; |
| 2225 | return f; |
| 2226 | } |
| 2227 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2228 | /* maximum number of characters required for output of %ld. 21 characters |
| 2229 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2230 | #define MAX_LONG_CHARS 21 |
| 2231 | /* maximum number of characters required for output of %lld. |
| 2232 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2233 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2234 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2235 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2236 | PyObject * |
| 2237 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2238 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2239 | va_list count; |
| 2240 | Py_ssize_t callcount = 0; |
| 2241 | PyObject **callresults = NULL; |
| 2242 | PyObject **callresult = NULL; |
| 2243 | Py_ssize_t n = 0; |
| 2244 | int width = 0; |
| 2245 | int precision = 0; |
| 2246 | int zeropad; |
| 2247 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2248 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2249 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2250 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2251 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2252 | Py_UCS4 argmaxchar; |
| 2253 | Py_ssize_t numbersize = 0; |
| 2254 | char *numberresults = NULL; |
| 2255 | char *numberresult = NULL; |
| 2256 | Py_ssize_t i; |
| 2257 | int kind; |
| 2258 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2259 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2260 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2261 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2262 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2263 | * 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] | 2264 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2265 | * also estimate a upper bound for all the number formats in the string, |
| 2266 | * 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] | 2267 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2268 | for (f = format; *f; f++) { |
| 2269 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2270 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2271 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2272 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2273 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2274 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2275 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2277 | #ifdef HAVE_LONG_LONG |
| 2278 | if (longlongflag) { |
| 2279 | if (width < MAX_LONG_LONG_CHARS) |
| 2280 | width = MAX_LONG_LONG_CHARS; |
| 2281 | } |
| 2282 | else |
| 2283 | #endif |
| 2284 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2285 | including sign. Decimal takes the most space. This |
| 2286 | isn't enough for octal. If a width is specified we |
| 2287 | need more (which we allocate later). */ |
| 2288 | if (width < MAX_LONG_CHARS) |
| 2289 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2290 | |
| 2291 | /* account for the size + '\0' to separate numbers |
| 2292 | inside of the numberresults buffer */ |
| 2293 | numbersize += (width + 1); |
| 2294 | } |
| 2295 | } |
| 2296 | else if ((unsigned char)*f > 127) { |
| 2297 | PyErr_Format(PyExc_ValueError, |
| 2298 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2299 | "string, got a non-ASCII byte: 0x%02x", |
| 2300 | (unsigned char)*f); |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | } |
| 2304 | /* step 2: allocate memory for the results of |
| 2305 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2306 | if (callcount) { |
| 2307 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2308 | if (!callresults) { |
| 2309 | PyErr_NoMemory(); |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | callresult = callresults; |
| 2313 | } |
| 2314 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2315 | if (numbersize) { |
| 2316 | numberresults = PyObject_Malloc(numbersize); |
| 2317 | if (!numberresults) { |
| 2318 | PyErr_NoMemory(); |
| 2319 | goto fail; |
| 2320 | } |
| 2321 | numberresult = numberresults; |
| 2322 | } |
| 2323 | |
| 2324 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2325 | for (f = format; *f; f++) { |
| 2326 | if (*f == '%') { |
| 2327 | const char* p; |
| 2328 | int longflag; |
| 2329 | int longlongflag; |
| 2330 | int size_tflag; |
| 2331 | int numprinted; |
| 2332 | |
| 2333 | p = f; |
| 2334 | zeropad = (f[1] == '0'); |
| 2335 | f = parse_format_flags(f, &width, &precision, |
| 2336 | &longflag, &longlongflag, &size_tflag); |
| 2337 | switch (*f) { |
| 2338 | case 'c': |
| 2339 | { |
| 2340 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2341 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2342 | n++; |
| 2343 | break; |
| 2344 | } |
| 2345 | case '%': |
| 2346 | n++; |
| 2347 | break; |
| 2348 | case 'i': |
| 2349 | case 'd': |
| 2350 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2351 | width, precision, *f); |
| 2352 | if (longflag) |
| 2353 | numprinted = sprintf(numberresult, fmt, |
| 2354 | va_arg(count, long)); |
| 2355 | #ifdef HAVE_LONG_LONG |
| 2356 | else if (longlongflag) |
| 2357 | numprinted = sprintf(numberresult, fmt, |
| 2358 | va_arg(count, PY_LONG_LONG)); |
| 2359 | #endif |
| 2360 | else if (size_tflag) |
| 2361 | numprinted = sprintf(numberresult, fmt, |
| 2362 | va_arg(count, Py_ssize_t)); |
| 2363 | else |
| 2364 | numprinted = sprintf(numberresult, fmt, |
| 2365 | va_arg(count, int)); |
| 2366 | n += numprinted; |
| 2367 | /* advance by +1 to skip over the '\0' */ |
| 2368 | numberresult += (numprinted + 1); |
| 2369 | assert(*(numberresult - 1) == '\0'); |
| 2370 | assert(*(numberresult - 2) != '\0'); |
| 2371 | assert(numprinted >= 0); |
| 2372 | assert(numberresult <= numberresults + numbersize); |
| 2373 | break; |
| 2374 | case 'u': |
| 2375 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2376 | width, precision, 'u'); |
| 2377 | if (longflag) |
| 2378 | numprinted = sprintf(numberresult, fmt, |
| 2379 | va_arg(count, unsigned long)); |
| 2380 | #ifdef HAVE_LONG_LONG |
| 2381 | else if (longlongflag) |
| 2382 | numprinted = sprintf(numberresult, fmt, |
| 2383 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2384 | #endif |
| 2385 | else if (size_tflag) |
| 2386 | numprinted = sprintf(numberresult, fmt, |
| 2387 | va_arg(count, size_t)); |
| 2388 | else |
| 2389 | numprinted = sprintf(numberresult, fmt, |
| 2390 | va_arg(count, unsigned int)); |
| 2391 | n += numprinted; |
| 2392 | numberresult += (numprinted + 1); |
| 2393 | assert(*(numberresult - 1) == '\0'); |
| 2394 | assert(*(numberresult - 2) != '\0'); |
| 2395 | assert(numprinted >= 0); |
| 2396 | assert(numberresult <= numberresults + numbersize); |
| 2397 | break; |
| 2398 | case 'x': |
| 2399 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2400 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2401 | n += numprinted; |
| 2402 | numberresult += (numprinted + 1); |
| 2403 | assert(*(numberresult - 1) == '\0'); |
| 2404 | assert(*(numberresult - 2) != '\0'); |
| 2405 | assert(numprinted >= 0); |
| 2406 | assert(numberresult <= numberresults + numbersize); |
| 2407 | break; |
| 2408 | case 'p': |
| 2409 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2410 | /* %p is ill-defined: ensure leading 0x. */ |
| 2411 | if (numberresult[1] == 'X') |
| 2412 | numberresult[1] = 'x'; |
| 2413 | else if (numberresult[1] != 'x') { |
| 2414 | memmove(numberresult + 2, numberresult, |
| 2415 | strlen(numberresult) + 1); |
| 2416 | numberresult[0] = '0'; |
| 2417 | numberresult[1] = 'x'; |
| 2418 | numprinted += 2; |
| 2419 | } |
| 2420 | n += numprinted; |
| 2421 | numberresult += (numprinted + 1); |
| 2422 | assert(*(numberresult - 1) == '\0'); |
| 2423 | assert(*(numberresult - 2) != '\0'); |
| 2424 | assert(numprinted >= 0); |
| 2425 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | case 's': |
| 2428 | { |
| 2429 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2430 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2431 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2432 | if (!str) |
| 2433 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2434 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2435 | unicode objects, there is no need to call ready on them */ |
| 2436 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2437 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2438 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2439 | /* Remember the str and switch to the next slot */ |
| 2440 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2441 | break; |
| 2442 | } |
| 2443 | case 'U': |
| 2444 | { |
| 2445 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2446 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2447 | if (PyUnicode_READY(obj) == -1) |
| 2448 | goto fail; |
| 2449 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2450 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2451 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | } |
| 2454 | case 'V': |
| 2455 | { |
| 2456 | PyObject *obj = va_arg(count, PyObject *); |
| 2457 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2458 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2459 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2460 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2461 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2462 | if (PyUnicode_READY(obj) == -1) |
| 2463 | goto fail; |
| 2464 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2465 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2466 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2467 | *callresult++ = NULL; |
| 2468 | } |
| 2469 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2470 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2471 | if (!str_obj) |
| 2472 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2473 | if (PyUnicode_READY(str_obj)) { |
| 2474 | Py_DECREF(str_obj); |
| 2475 | goto fail; |
| 2476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2477 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2478 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2479 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2480 | *callresult++ = str_obj; |
| 2481 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | } |
| 2484 | case 'S': |
| 2485 | { |
| 2486 | PyObject *obj = va_arg(count, PyObject *); |
| 2487 | PyObject *str; |
| 2488 | assert(obj); |
| 2489 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2490 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2491 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2492 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2493 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2494 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2495 | /* Remember the str and switch to the next slot */ |
| 2496 | *callresult++ = str; |
| 2497 | break; |
| 2498 | } |
| 2499 | case 'R': |
| 2500 | { |
| 2501 | PyObject *obj = va_arg(count, PyObject *); |
| 2502 | PyObject *repr; |
| 2503 | assert(obj); |
| 2504 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2505 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2506 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2507 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2508 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2509 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2510 | /* Remember the repr and switch to the next slot */ |
| 2511 | *callresult++ = repr; |
| 2512 | break; |
| 2513 | } |
| 2514 | case 'A': |
| 2515 | { |
| 2516 | PyObject *obj = va_arg(count, PyObject *); |
| 2517 | PyObject *ascii; |
| 2518 | assert(obj); |
| 2519 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2520 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2521 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2523 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2524 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2525 | /* Remember the repr and switch to the next slot */ |
| 2526 | *callresult++ = ascii; |
| 2527 | break; |
| 2528 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2529 | default: |
| 2530 | /* if we stumble upon an unknown |
| 2531 | formatting code, copy the rest of |
| 2532 | the format string to the output |
| 2533 | string. (we cannot just skip the |
| 2534 | code, since there's no way to know |
| 2535 | what's in the argument list) */ |
| 2536 | n += strlen(p); |
| 2537 | goto expand; |
| 2538 | } |
| 2539 | } else |
| 2540 | n++; |
| 2541 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2542 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2543 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2544 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2545 | we don't have to resize the string. |
| 2546 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2547 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2548 | if (!string) |
| 2549 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2550 | kind = PyUnicode_KIND(string); |
| 2551 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2552 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2553 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2555 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2556 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2557 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2558 | |
| 2559 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2560 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2561 | /* checking for == because the last argument could be a empty |
| 2562 | string, which causes i to point to end, the assert at the end of |
| 2563 | the loop */ |
| 2564 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2565 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2566 | switch (*f) { |
| 2567 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2568 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2569 | const int ordinal = va_arg(vargs, int); |
| 2570 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2571 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2572 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2573 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2574 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2576 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | case 'p': |
| 2578 | /* unused, since we already have the result */ |
| 2579 | if (*f == 'p') |
| 2580 | (void) va_arg(vargs, void *); |
| 2581 | else |
| 2582 | (void) va_arg(vargs, int); |
| 2583 | /* extract the result from numberresults and append. */ |
| 2584 | for (; *numberresult; ++i, ++numberresult) |
| 2585 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2586 | /* skip over the separating '\0' */ |
| 2587 | assert(*numberresult == '\0'); |
| 2588 | numberresult++; |
| 2589 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2590 | break; |
| 2591 | case 's': |
| 2592 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2593 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2594 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2595 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2596 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2597 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2598 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2599 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2600 | /* We're done with the unicode()/repr() => forget it */ |
| 2601 | Py_DECREF(*callresult); |
| 2602 | /* switch to next unicode()/repr() result */ |
| 2603 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | break; |
| 2605 | } |
| 2606 | case 'U': |
| 2607 | { |
| 2608 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2609 | Py_ssize_t size; |
| 2610 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2611 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2612 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2613 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2614 | break; |
| 2615 | } |
| 2616 | case 'V': |
| 2617 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2619 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2620 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | size = PyUnicode_GET_LENGTH(obj); |
| 2623 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2624 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2625 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2626 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2627 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2628 | assert(PyUnicode_KIND(*callresult) <= |
| 2629 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2630 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2631 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2632 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2634 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | break; |
| 2636 | } |
| 2637 | case 'S': |
| 2638 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2639 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2640 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2641 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2642 | /* unused, since we already have the result */ |
| 2643 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2644 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2645 | copy_characters(string, i, *callresult, 0, size); |
| 2646 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2647 | /* We're done with the unicode()/repr() => forget it */ |
| 2648 | Py_DECREF(*callresult); |
| 2649 | /* switch to next unicode()/repr() result */ |
| 2650 | ++callresult; |
| 2651 | break; |
| 2652 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2653 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2655 | break; |
| 2656 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | for (; *p; ++p, ++i) |
| 2658 | PyUnicode_WRITE(kind, data, i, *p); |
| 2659 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2660 | goto end; |
| 2661 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2663 | else { |
| 2664 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2665 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2666 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2667 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2668 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2670 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2671 | if (callresults) |
| 2672 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | if (numberresults) |
| 2674 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2675 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2676 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | if (callresults) { |
| 2678 | PyObject **callresult2 = callresults; |
| 2679 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2680 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2681 | ++callresult2; |
| 2682 | } |
| 2683 | PyObject_Free(callresults); |
| 2684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2685 | if (numberresults) |
| 2686 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2687 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2690 | PyObject * |
| 2691 | PyUnicode_FromFormat(const char *format, ...) |
| 2692 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2693 | PyObject* ret; |
| 2694 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2695 | |
| 2696 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2697 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2698 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2700 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2701 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2702 | va_end(vargs); |
| 2703 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | #ifdef HAVE_WCHAR_H |
| 2707 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2708 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2709 | convert a Unicode object to a wide character string. |
| 2710 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2711 | - 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] | 2712 | character) required to convert the unicode object. Ignore size argument. |
| 2713 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2714 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2715 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2716 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2717 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2718 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2719 | wchar_t *w, |
| 2720 | Py_ssize_t size) |
| 2721 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2722 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | const wchar_t *wstr; |
| 2724 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2725 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2726 | if (wstr == NULL) |
| 2727 | return -1; |
| 2728 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2729 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2730 | if (size > res) |
| 2731 | size = res + 1; |
| 2732 | else |
| 2733 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2734 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2735 | return res; |
| 2736 | } |
| 2737 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2738 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2742 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2743 | wchar_t *w, |
| 2744 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2745 | { |
| 2746 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2747 | PyErr_BadInternalCall(); |
| 2748 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2749 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2750 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2753 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2754 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2755 | Py_ssize_t *size) |
| 2756 | { |
| 2757 | wchar_t* buffer; |
| 2758 | Py_ssize_t buflen; |
| 2759 | |
| 2760 | if (unicode == NULL) { |
| 2761 | PyErr_BadInternalCall(); |
| 2762 | return NULL; |
| 2763 | } |
| 2764 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2765 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2766 | if (buflen == -1) |
| 2767 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2768 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2769 | PyErr_NoMemory(); |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2773 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2774 | if (buffer == NULL) { |
| 2775 | PyErr_NoMemory(); |
| 2776 | return NULL; |
| 2777 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2778 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2779 | if (buflen == -1) |
| 2780 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2781 | if (size != NULL) |
| 2782 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2783 | return buffer; |
| 2784 | } |
| 2785 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2786 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2788 | PyObject * |
| 2789 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2791 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2792 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2793 | PyErr_SetString(PyExc_ValueError, |
| 2794 | "chr() arg not in range(0x110000)"); |
| 2795 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2796 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2797 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2798 | if (ordinal < 256) |
| 2799 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | v = PyUnicode_New(1, ordinal); |
| 2802 | if (v == NULL) |
| 2803 | return NULL; |
| 2804 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2805 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2806 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2809 | PyObject * |
| 2810 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2812 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2813 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2814 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2815 | if (PyUnicode_READY(obj)) |
| 2816 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2817 | Py_INCREF(obj); |
| 2818 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2819 | } |
| 2820 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2821 | /* For a Unicode subtype that's not a Unicode object, |
| 2822 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2823 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2824 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2825 | PyErr_Format(PyExc_TypeError, |
| 2826 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2827 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2828 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2831 | PyObject * |
| 2832 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2833 | const char *encoding, |
| 2834 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2835 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2836 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2837 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2840 | PyErr_BadInternalCall(); |
| 2841 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2842 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2843 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2844 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2845 | if (PyBytes_Check(obj)) { |
| 2846 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2847 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2848 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2849 | } |
| 2850 | else { |
| 2851 | v = PyUnicode_Decode( |
| 2852 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2853 | encoding, errors); |
| 2854 | } |
| 2855 | return v; |
| 2856 | } |
| 2857 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2858 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2859 | PyErr_SetString(PyExc_TypeError, |
| 2860 | "decoding str is not supported"); |
| 2861 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2862 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2863 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2864 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2865 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2866 | PyErr_Format(PyExc_TypeError, |
| 2867 | "coercing to str: need bytes, bytearray " |
| 2868 | "or buffer-like object, %.80s found", |
| 2869 | Py_TYPE(obj)->tp_name); |
| 2870 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2871 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2873 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2874 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2875 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2876 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2877 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2878 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2879 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2880 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2881 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2884 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2885 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2886 | 1 on success. */ |
| 2887 | static int |
| 2888 | normalize_encoding(const char *encoding, |
| 2889 | char *lower, |
| 2890 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2892 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2893 | char *l; |
| 2894 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2895 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2896 | if (encoding == NULL) { |
| 2897 | strcpy(lower, "utf-8"); |
| 2898 | return 1; |
| 2899 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2900 | e = encoding; |
| 2901 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2902 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2903 | while (*e) { |
| 2904 | if (l == l_end) |
| 2905 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2906 | if (Py_ISUPPER(*e)) { |
| 2907 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2908 | } |
| 2909 | else if (*e == '_') { |
| 2910 | *l++ = '-'; |
| 2911 | e++; |
| 2912 | } |
| 2913 | else { |
| 2914 | *l++ = *e++; |
| 2915 | } |
| 2916 | } |
| 2917 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2918 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2921 | PyObject * |
| 2922 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2923 | Py_ssize_t size, |
| 2924 | const char *encoding, |
| 2925 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2926 | { |
| 2927 | PyObject *buffer = NULL, *unicode; |
| 2928 | Py_buffer info; |
| 2929 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2930 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2931 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2932 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2933 | if ((strcmp(lower, "utf-8") == 0) || |
| 2934 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2935 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2936 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2937 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2938 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2939 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2940 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2941 | else if (strcmp(lower, "mbcs") == 0) |
| 2942 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2943 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2944 | else if (strcmp(lower, "ascii") == 0) |
| 2945 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2946 | else if (strcmp(lower, "utf-16") == 0) |
| 2947 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2948 | else if (strcmp(lower, "utf-32") == 0) |
| 2949 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2950 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2951 | |
| 2952 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2953 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2954 | 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] | 2955 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2956 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2957 | if (buffer == NULL) |
| 2958 | goto onError; |
| 2959 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2960 | if (unicode == NULL) |
| 2961 | goto onError; |
| 2962 | if (!PyUnicode_Check(unicode)) { |
| 2963 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2964 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2965 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | Py_DECREF(unicode); |
| 2967 | goto onError; |
| 2968 | } |
| 2969 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2970 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2973 | Py_XDECREF(buffer); |
| 2974 | return NULL; |
| 2975 | } |
| 2976 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2977 | PyObject * |
| 2978 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2979 | const char *encoding, |
| 2980 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2981 | { |
| 2982 | PyObject *v; |
| 2983 | |
| 2984 | if (!PyUnicode_Check(unicode)) { |
| 2985 | PyErr_BadArgument(); |
| 2986 | goto onError; |
| 2987 | } |
| 2988 | |
| 2989 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2990 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2991 | |
| 2992 | /* Decode via the codec registry */ |
| 2993 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2994 | if (v == NULL) |
| 2995 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2996 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2997 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2998 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2999 | return NULL; |
| 3000 | } |
| 3001 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3002 | PyObject * |
| 3003 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3004 | const char *encoding, |
| 3005 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3006 | { |
| 3007 | PyObject *v; |
| 3008 | |
| 3009 | if (!PyUnicode_Check(unicode)) { |
| 3010 | PyErr_BadArgument(); |
| 3011 | goto onError; |
| 3012 | } |
| 3013 | |
| 3014 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* Decode via the codec registry */ |
| 3018 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3019 | if (v == NULL) |
| 3020 | goto onError; |
| 3021 | if (!PyUnicode_Check(v)) { |
| 3022 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3023 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3024 | Py_TYPE(v)->tp_name); |
| 3025 | Py_DECREF(v); |
| 3026 | goto onError; |
| 3027 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3028 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3029 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3030 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3031 | return NULL; |
| 3032 | } |
| 3033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3034 | PyObject * |
| 3035 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3036 | Py_ssize_t size, |
| 3037 | const char *encoding, |
| 3038 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | { |
| 3040 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 | unicode = PyUnicode_FromUnicode(s, size); |
| 3043 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3044 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3046 | Py_DECREF(unicode); |
| 3047 | return v; |
| 3048 | } |
| 3049 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3050 | PyObject * |
| 3051 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3052 | const char *encoding, |
| 3053 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3054 | { |
| 3055 | PyObject *v; |
| 3056 | |
| 3057 | if (!PyUnicode_Check(unicode)) { |
| 3058 | PyErr_BadArgument(); |
| 3059 | goto onError; |
| 3060 | } |
| 3061 | |
| 3062 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3063 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3064 | |
| 3065 | /* Encode via the codec registry */ |
| 3066 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3067 | if (v == NULL) |
| 3068 | goto onError; |
| 3069 | return v; |
| 3070 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3071 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3072 | return NULL; |
| 3073 | } |
| 3074 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3075 | static size_t |
| 3076 | wcstombs_errorpos(const wchar_t *wstr) |
| 3077 | { |
| 3078 | size_t len; |
| 3079 | #if SIZEOF_WCHAR_T == 2 |
| 3080 | wchar_t buf[3]; |
| 3081 | #else |
| 3082 | wchar_t buf[2]; |
| 3083 | #endif |
| 3084 | char outbuf[MB_LEN_MAX]; |
| 3085 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3086 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3087 | #if SIZEOF_WCHAR_T == 2 |
| 3088 | buf[2] = 0; |
| 3089 | #else |
| 3090 | buf[1] = 0; |
| 3091 | #endif |
| 3092 | start = wstr; |
| 3093 | while (*wstr != L'\0') |
| 3094 | { |
| 3095 | previous = wstr; |
| 3096 | #if SIZEOF_WCHAR_T == 2 |
| 3097 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3098 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3099 | { |
| 3100 | buf[0] = wstr[0]; |
| 3101 | buf[1] = wstr[1]; |
| 3102 | wstr += 2; |
| 3103 | } |
| 3104 | else { |
| 3105 | buf[0] = *wstr; |
| 3106 | buf[1] = 0; |
| 3107 | wstr++; |
| 3108 | } |
| 3109 | #else |
| 3110 | buf[0] = *wstr; |
| 3111 | wstr++; |
| 3112 | #endif |
| 3113 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3114 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3115 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3119 | return 0; |
| 3120 | } |
| 3121 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3122 | static int |
| 3123 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3124 | { |
| 3125 | if (errors == NULL) { |
| 3126 | *surrogateescape = 0; |
| 3127 | return 0; |
| 3128 | } |
| 3129 | |
| 3130 | if (strcmp(errors, "strict") == 0) { |
| 3131 | *surrogateescape = 0; |
| 3132 | return 0; |
| 3133 | } |
| 3134 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3135 | *surrogateescape = 1; |
| 3136 | return 0; |
| 3137 | } |
| 3138 | PyErr_Format(PyExc_ValueError, |
| 3139 | "only 'strict' and 'surrogateescape' error handlers " |
| 3140 | "are supported, not '%s'", |
| 3141 | errors); |
| 3142 | return -1; |
| 3143 | } |
| 3144 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3145 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3146 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3147 | { |
| 3148 | Py_ssize_t wlen, wlen2; |
| 3149 | wchar_t *wstr; |
| 3150 | PyObject *bytes = NULL; |
| 3151 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3152 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3153 | PyObject *exc; |
| 3154 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3155 | int surrogateescape; |
| 3156 | |
| 3157 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3158 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3159 | |
| 3160 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3161 | if (wstr == NULL) |
| 3162 | return NULL; |
| 3163 | |
| 3164 | wlen2 = wcslen(wstr); |
| 3165 | if (wlen2 != wlen) { |
| 3166 | PyMem_Free(wstr); |
| 3167 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | |
| 3171 | if (surrogateescape) { |
| 3172 | /* locale encoding with surrogateescape */ |
| 3173 | char *str; |
| 3174 | |
| 3175 | str = _Py_wchar2char(wstr, &error_pos); |
| 3176 | if (str == NULL) { |
| 3177 | if (error_pos == (size_t)-1) { |
| 3178 | PyErr_NoMemory(); |
| 3179 | PyMem_Free(wstr); |
| 3180 | return NULL; |
| 3181 | } |
| 3182 | else { |
| 3183 | goto encode_error; |
| 3184 | } |
| 3185 | } |
| 3186 | PyMem_Free(wstr); |
| 3187 | |
| 3188 | bytes = PyBytes_FromString(str); |
| 3189 | PyMem_Free(str); |
| 3190 | } |
| 3191 | else { |
| 3192 | size_t len, len2; |
| 3193 | |
| 3194 | len = wcstombs(NULL, wstr, 0); |
| 3195 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3196 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3197 | goto encode_error; |
| 3198 | } |
| 3199 | |
| 3200 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3201 | if (bytes == NULL) { |
| 3202 | PyMem_Free(wstr); |
| 3203 | return NULL; |
| 3204 | } |
| 3205 | |
| 3206 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3207 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3208 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3209 | goto encode_error; |
| 3210 | } |
| 3211 | PyMem_Free(wstr); |
| 3212 | } |
| 3213 | return bytes; |
| 3214 | |
| 3215 | encode_error: |
| 3216 | errmsg = strerror(errno); |
| 3217 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3218 | |
| 3219 | if (error_pos == (size_t)-1) |
| 3220 | error_pos = wcstombs_errorpos(wstr); |
| 3221 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3222 | PyMem_Free(wstr); |
| 3223 | Py_XDECREF(bytes); |
| 3224 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3225 | if (errmsg != NULL) { |
| 3226 | size_t errlen; |
| 3227 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3228 | if (wstr != NULL) { |
| 3229 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3230 | PyMem_Free(wstr); |
| 3231 | } else |
| 3232 | errmsg = NULL; |
| 3233 | } |
| 3234 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3235 | reason = PyUnicode_FromString( |
| 3236 | "wcstombs() encountered an unencodable " |
| 3237 | "wide character"); |
| 3238 | if (reason == NULL) |
| 3239 | return NULL; |
| 3240 | |
| 3241 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3242 | "locale", unicode, |
| 3243 | (Py_ssize_t)error_pos, |
| 3244 | (Py_ssize_t)(error_pos+1), |
| 3245 | reason); |
| 3246 | Py_DECREF(reason); |
| 3247 | if (exc != NULL) { |
| 3248 | PyCodec_StrictErrors(exc); |
| 3249 | Py_XDECREF(exc); |
| 3250 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3251 | return NULL; |
| 3252 | } |
| 3253 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3254 | PyObject * |
| 3255 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3256 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3257 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3258 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3259 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3261 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3262 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3263 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3264 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3265 | the Python codec requires to encode at least its own filename. Use the C |
| 3266 | version of the locale codec until the codec registry is initialized and |
| 3267 | the Python codec is loaded. |
| 3268 | |
| 3269 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3270 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3271 | subinterpreters. */ |
| 3272 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3273 | return PyUnicode_AsEncodedString(unicode, |
| 3274 | Py_FileSystemDefaultEncoding, |
| 3275 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3276 | } |
| 3277 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3278 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3279 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3280 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3283 | PyObject * |
| 3284 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3285 | const char *encoding, |
| 3286 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3287 | { |
| 3288 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3289 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3291 | if (!PyUnicode_Check(unicode)) { |
| 3292 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3294 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3295 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3296 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3297 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3298 | if ((strcmp(lower, "utf-8") == 0) || |
| 3299 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3300 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3301 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3302 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3303 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3304 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3305 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3306 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3307 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3308 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3309 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3310 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3311 | else if (strcmp(lower, "mbcs") == 0) |
| 3312 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3313 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3314 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3315 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | |
| 3318 | /* Encode via the codec registry */ |
| 3319 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3320 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3321 | return NULL; |
| 3322 | |
| 3323 | /* The normal path */ |
| 3324 | if (PyBytes_Check(v)) |
| 3325 | return v; |
| 3326 | |
| 3327 | /* 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] | 3328 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3329 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3330 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3331 | |
| 3332 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3333 | "encoder %s returned bytearray instead of bytes", |
| 3334 | encoding); |
| 3335 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3336 | Py_DECREF(v); |
| 3337 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3338 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3339 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3340 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3341 | Py_DECREF(v); |
| 3342 | return b; |
| 3343 | } |
| 3344 | |
| 3345 | PyErr_Format(PyExc_TypeError, |
| 3346 | "encoder did not return a bytes object (type=%.400s)", |
| 3347 | Py_TYPE(v)->tp_name); |
| 3348 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3349 | return NULL; |
| 3350 | } |
| 3351 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3352 | PyObject * |
| 3353 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3354 | const char *encoding, |
| 3355 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3356 | { |
| 3357 | PyObject *v; |
| 3358 | |
| 3359 | if (!PyUnicode_Check(unicode)) { |
| 3360 | PyErr_BadArgument(); |
| 3361 | goto onError; |
| 3362 | } |
| 3363 | |
| 3364 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3365 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3366 | |
| 3367 | /* Encode via the codec registry */ |
| 3368 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3369 | if (v == NULL) |
| 3370 | goto onError; |
| 3371 | if (!PyUnicode_Check(v)) { |
| 3372 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3373 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3374 | Py_TYPE(v)->tp_name); |
| 3375 | Py_DECREF(v); |
| 3376 | goto onError; |
| 3377 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3380 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 | return NULL; |
| 3382 | } |
| 3383 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3384 | static size_t |
| 3385 | mbstowcs_errorpos(const char *str, size_t len) |
| 3386 | { |
| 3387 | #ifdef HAVE_MBRTOWC |
| 3388 | const char *start = str; |
| 3389 | mbstate_t mbs; |
| 3390 | size_t converted; |
| 3391 | wchar_t ch; |
| 3392 | |
| 3393 | memset(&mbs, 0, sizeof mbs); |
| 3394 | while (len) |
| 3395 | { |
| 3396 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3397 | if (converted == 0) |
| 3398 | /* Reached end of string */ |
| 3399 | break; |
| 3400 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3401 | /* Conversion error or incomplete character */ |
| 3402 | return str - start; |
| 3403 | } |
| 3404 | else { |
| 3405 | str += converted; |
| 3406 | len -= converted; |
| 3407 | } |
| 3408 | } |
| 3409 | /* failed to find the undecodable byte sequence */ |
| 3410 | return 0; |
| 3411 | #endif |
| 3412 | return 0; |
| 3413 | } |
| 3414 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3415 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3416 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3417 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3418 | { |
| 3419 | wchar_t smallbuf[256]; |
| 3420 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3421 | wchar_t *wstr; |
| 3422 | size_t wlen, wlen2; |
| 3423 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3424 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3425 | size_t error_pos; |
| 3426 | char *errmsg; |
| 3427 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3428 | |
| 3429 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3430 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3431 | |
| 3432 | if (str[len] != '\0' || len != strlen(str)) { |
| 3433 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3434 | return NULL; |
| 3435 | } |
| 3436 | |
| 3437 | if (surrogateescape) |
| 3438 | { |
| 3439 | wstr = _Py_char2wchar(str, &wlen); |
| 3440 | if (wstr == NULL) { |
| 3441 | if (wlen == (size_t)-1) |
| 3442 | PyErr_NoMemory(); |
| 3443 | else |
| 3444 | PyErr_SetFromErrno(PyExc_OSError); |
| 3445 | return NULL; |
| 3446 | } |
| 3447 | |
| 3448 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3449 | PyMem_Free(wstr); |
| 3450 | } |
| 3451 | else { |
| 3452 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3453 | wlen = mbstowcs(NULL, str, 0); |
| 3454 | #else |
| 3455 | wlen = len; |
| 3456 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3457 | if (wlen == (size_t)-1) |
| 3458 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3459 | if (wlen+1 <= smallbuf_len) { |
| 3460 | wstr = smallbuf; |
| 3461 | } |
| 3462 | else { |
| 3463 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3464 | return PyErr_NoMemory(); |
| 3465 | |
| 3466 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3467 | if (!wstr) |
| 3468 | return PyErr_NoMemory(); |
| 3469 | } |
| 3470 | |
| 3471 | /* This shouldn't fail now */ |
| 3472 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3473 | if (wlen2 == (size_t)-1) { |
| 3474 | if (wstr != smallbuf) |
| 3475 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3476 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3477 | } |
| 3478 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3479 | assert(wlen2 == wlen); |
| 3480 | #endif |
| 3481 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3482 | if (wstr != smallbuf) |
| 3483 | PyMem_Free(wstr); |
| 3484 | } |
| 3485 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3486 | |
| 3487 | decode_error: |
| 3488 | errmsg = strerror(errno); |
| 3489 | assert(errmsg != NULL); |
| 3490 | |
| 3491 | error_pos = mbstowcs_errorpos(str, len); |
| 3492 | if (errmsg != NULL) { |
| 3493 | size_t errlen; |
| 3494 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3495 | if (wstr != NULL) { |
| 3496 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3497 | PyMem_Free(wstr); |
| 3498 | } else |
| 3499 | errmsg = NULL; |
| 3500 | } |
| 3501 | if (errmsg == NULL) |
| 3502 | reason = PyUnicode_FromString( |
| 3503 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3504 | if (reason == NULL) |
| 3505 | return NULL; |
| 3506 | |
| 3507 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3508 | "locale", str, len, |
| 3509 | (Py_ssize_t)error_pos, |
| 3510 | (Py_ssize_t)(error_pos+1), |
| 3511 | reason); |
| 3512 | Py_DECREF(reason); |
| 3513 | if (exc != NULL) { |
| 3514 | PyCodec_StrictErrors(exc); |
| 3515 | Py_XDECREF(exc); |
| 3516 | } |
| 3517 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3521 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3522 | { |
| 3523 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3524 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | |
| 3528 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3529 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3530 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3531 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3532 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3533 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3534 | PyObject* |
| 3535 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3536 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3537 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3538 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3539 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3540 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3541 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3542 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3543 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3544 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3545 | the Python codec requires to encode at least its own filename. Use the C |
| 3546 | version of the locale codec until the codec registry is initialized and |
| 3547 | the Python codec is loaded. |
| 3548 | |
| 3549 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3550 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3551 | subinterpreters. */ |
| 3552 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3553 | return PyUnicode_Decode(s, size, |
| 3554 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3555 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3556 | } |
| 3557 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3558 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3559 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3560 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3563 | |
| 3564 | int |
| 3565 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3566 | { |
| 3567 | PyObject *output = NULL; |
| 3568 | Py_ssize_t size; |
| 3569 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3570 | if (arg == NULL) { |
| 3571 | Py_DECREF(*(PyObject**)addr); |
| 3572 | return 1; |
| 3573 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3574 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3575 | output = arg; |
| 3576 | Py_INCREF(output); |
| 3577 | } |
| 3578 | else { |
| 3579 | arg = PyUnicode_FromObject(arg); |
| 3580 | if (!arg) |
| 3581 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3582 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3583 | Py_DECREF(arg); |
| 3584 | if (!output) |
| 3585 | return 0; |
| 3586 | if (!PyBytes_Check(output)) { |
| 3587 | Py_DECREF(output); |
| 3588 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3589 | return 0; |
| 3590 | } |
| 3591 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3592 | size = PyBytes_GET_SIZE(output); |
| 3593 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3594 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3595 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3596 | Py_DECREF(output); |
| 3597 | return 0; |
| 3598 | } |
| 3599 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3600 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3604 | int |
| 3605 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3606 | { |
| 3607 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3608 | if (arg == NULL) { |
| 3609 | Py_DECREF(*(PyObject**)addr); |
| 3610 | return 1; |
| 3611 | } |
| 3612 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3613 | if (PyUnicode_READY(arg)) |
| 3614 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3615 | output = arg; |
| 3616 | Py_INCREF(output); |
| 3617 | } |
| 3618 | else { |
| 3619 | arg = PyBytes_FromObject(arg); |
| 3620 | if (!arg) |
| 3621 | return 0; |
| 3622 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3623 | PyBytes_GET_SIZE(arg)); |
| 3624 | Py_DECREF(arg); |
| 3625 | if (!output) |
| 3626 | return 0; |
| 3627 | if (!PyUnicode_Check(output)) { |
| 3628 | Py_DECREF(output); |
| 3629 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3630 | return 0; |
| 3631 | } |
| 3632 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3633 | if (PyUnicode_READY(output) < 0) { |
| 3634 | Py_DECREF(output); |
| 3635 | return 0; |
| 3636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3637 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3638 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3639 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3640 | Py_DECREF(output); |
| 3641 | return 0; |
| 3642 | } |
| 3643 | *(PyObject**)addr = output; |
| 3644 | return Py_CLEANUP_SUPPORTED; |
| 3645 | } |
| 3646 | |
| 3647 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3648 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3649 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3650 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3651 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3652 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3653 | if (!PyUnicode_Check(unicode)) { |
| 3654 | PyErr_BadArgument(); |
| 3655 | return NULL; |
| 3656 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3657 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3658 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3659 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3660 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3661 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3662 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3663 | if (bytes == NULL) |
| 3664 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3665 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3666 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3667 | Py_DECREF(bytes); |
| 3668 | return NULL; |
| 3669 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3670 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3671 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3672 | PyBytes_AS_STRING(bytes), |
| 3673 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3674 | Py_DECREF(bytes); |
| 3675 | } |
| 3676 | |
| 3677 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3678 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3679 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3683 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3684 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3685 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3686 | } |
| 3687 | |
| 3688 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3689 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3690 | #endif |
| 3691 | |
| 3692 | |
| 3693 | Py_UNICODE * |
| 3694 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3695 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3696 | const unsigned char *one_byte; |
| 3697 | #if SIZEOF_WCHAR_T == 4 |
| 3698 | const Py_UCS2 *two_bytes; |
| 3699 | #else |
| 3700 | const Py_UCS4 *four_bytes; |
| 3701 | const Py_UCS4 *ucs4_end; |
| 3702 | Py_ssize_t num_surrogates; |
| 3703 | #endif |
| 3704 | wchar_t *w; |
| 3705 | wchar_t *wchar_end; |
| 3706 | |
| 3707 | if (!PyUnicode_Check(unicode)) { |
| 3708 | PyErr_BadArgument(); |
| 3709 | return NULL; |
| 3710 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3711 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3712 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3713 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3714 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3715 | |
| 3716 | #ifdef Py_DEBUG |
| 3717 | ++unicode_as_unicode_calls; |
| 3718 | #endif |
| 3719 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3720 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3721 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3722 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3723 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3724 | num_surrogates = 0; |
| 3725 | |
| 3726 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3727 | if (*four_bytes > 0xFFFF) |
| 3728 | ++num_surrogates; |
| 3729 | } |
| 3730 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3731 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3732 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3733 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3734 | PyErr_NoMemory(); |
| 3735 | return NULL; |
| 3736 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3737 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3738 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3739 | w = _PyUnicode_WSTR(unicode); |
| 3740 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3741 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3742 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3743 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3744 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3745 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3746 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3747 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3748 | } |
| 3749 | else |
| 3750 | *w = *four_bytes; |
| 3751 | |
| 3752 | if (w > wchar_end) { |
| 3753 | assert(0 && "Miscalculated string end"); |
| 3754 | } |
| 3755 | } |
| 3756 | *w = 0; |
| 3757 | #else |
| 3758 | /* sizeof(wchar_t) == 4 */ |
| 3759 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3760 | "should share memory already."); |
| 3761 | return NULL; |
| 3762 | #endif |
| 3763 | } |
| 3764 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3765 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3766 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3767 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3768 | PyErr_NoMemory(); |
| 3769 | return NULL; |
| 3770 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3771 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3772 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3773 | w = _PyUnicode_WSTR(unicode); |
| 3774 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3776 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3777 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3778 | for (; w < wchar_end; ++one_byte, ++w) |
| 3779 | *w = *one_byte; |
| 3780 | /* null-terminate the wstr */ |
| 3781 | *w = 0; |
| 3782 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3783 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3784 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3785 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3786 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3787 | *w = *two_bytes; |
| 3788 | /* null-terminate the wstr */ |
| 3789 | *w = 0; |
| 3790 | #else |
| 3791 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3792 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3793 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3794 | Py_FatalError("Impossible unicode object state, wstr " |
| 3795 | "and str should share memory already."); |
| 3796 | return NULL; |
| 3797 | #endif |
| 3798 | } |
| 3799 | else { |
| 3800 | assert(0 && "This should never happen."); |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3805 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3806 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3809 | Py_UNICODE * |
| 3810 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3811 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3816 | Py_ssize_t |
| 3817 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3818 | { |
| 3819 | if (!PyUnicode_Check(unicode)) { |
| 3820 | PyErr_BadArgument(); |
| 3821 | goto onError; |
| 3822 | } |
| 3823 | return PyUnicode_GET_SIZE(unicode); |
| 3824 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3825 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3826 | return -1; |
| 3827 | } |
| 3828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | Py_ssize_t |
| 3830 | PyUnicode_GetLength(PyObject *unicode) |
| 3831 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3832 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3833 | PyErr_BadArgument(); |
| 3834 | return -1; |
| 3835 | } |
| 3836 | |
| 3837 | return PyUnicode_GET_LENGTH(unicode); |
| 3838 | } |
| 3839 | |
| 3840 | Py_UCS4 |
| 3841 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3842 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3843 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3844 | PyErr_BadArgument(); |
| 3845 | return (Py_UCS4)-1; |
| 3846 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3847 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3848 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3849 | return (Py_UCS4)-1; |
| 3850 | } |
| 3851 | return PyUnicode_READ_CHAR(unicode, index); |
| 3852 | } |
| 3853 | |
| 3854 | int |
| 3855 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3856 | { |
| 3857 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3858 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | return -1; |
| 3860 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3861 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3862 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3863 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3864 | return -1; |
| 3865 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3866 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3867 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3868 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3869 | index, ch); |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3873 | const char * |
| 3874 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3875 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3876 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3879 | /* create or adjust a UnicodeDecodeError */ |
| 3880 | static void |
| 3881 | make_decode_exception(PyObject **exceptionObject, |
| 3882 | const char *encoding, |
| 3883 | const char *input, Py_ssize_t length, |
| 3884 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3885 | const char *reason) |
| 3886 | { |
| 3887 | if (*exceptionObject == NULL) { |
| 3888 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3889 | encoding, input, length, startpos, endpos, reason); |
| 3890 | } |
| 3891 | else { |
| 3892 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3893 | goto onError; |
| 3894 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3895 | goto onError; |
| 3896 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3897 | goto onError; |
| 3898 | } |
| 3899 | return; |
| 3900 | |
| 3901 | onError: |
| 3902 | Py_DECREF(*exceptionObject); |
| 3903 | *exceptionObject = NULL; |
| 3904 | } |
| 3905 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3906 | /* error handling callback helper: |
| 3907 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3908 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3909 | and adjust various state variables. |
| 3910 | return 0 on success, -1 on error |
| 3911 | */ |
| 3912 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3913 | static int |
| 3914 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3915 | const char *encoding, const char *reason, |
| 3916 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3917 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3918 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3919 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3920 | 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] | 3921 | |
| 3922 | PyObject *restuple = NULL; |
| 3923 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3924 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3925 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3926 | Py_ssize_t requiredsize; |
| 3927 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3928 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3929 | int res = -1; |
| 3930 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3931 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3932 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3933 | else |
| 3934 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3935 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3936 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3937 | *errorHandler = PyCodec_LookupError(errors); |
| 3938 | if (*errorHandler == NULL) |
| 3939 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3942 | make_decode_exception(exceptionObject, |
| 3943 | encoding, |
| 3944 | *input, *inend - *input, |
| 3945 | *startinpos, *endinpos, |
| 3946 | reason); |
| 3947 | if (*exceptionObject == NULL) |
| 3948 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3949 | |
| 3950 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3951 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3952 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3953 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3954 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3955 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3956 | } |
| 3957 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3958 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3959 | if (PyUnicode_READY(repunicode) < 0) |
| 3960 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3961 | |
| 3962 | /* Copy back the bytes variables, which might have been modified by the |
| 3963 | callback */ |
| 3964 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3965 | if (!inputobj) |
| 3966 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3967 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3968 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3969 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3970 | *input = PyBytes_AS_STRING(inputobj); |
| 3971 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3972 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3973 | /* we can DECREF safely, as the exception has another reference, |
| 3974 | so the object won't go away. */ |
| 3975 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3976 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3977 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3978 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3979 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3980 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3981 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3982 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3983 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3984 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3985 | /* need more space? (at least enough for what we |
| 3986 | have+the replacement+the rest of the string (starting |
| 3987 | at the new input position), so we won't have to check space |
| 3988 | when there are no errors in the rest of the string) */ |
| 3989 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3990 | requiredsize = *outpos + replen + insize-newpos; |
| 3991 | if (requiredsize > outsize) { |
| 3992 | if (requiredsize<2*outsize) |
| 3993 | requiredsize = 2*outsize; |
| 3994 | if (unicode_resize(output, requiredsize) < 0) |
| 3995 | goto onError; |
| 3996 | } |
| 3997 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3998 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3999 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 4000 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4001 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4002 | else { |
| 4003 | wchar_t *repwstr; |
| 4004 | Py_ssize_t repwlen; |
| 4005 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4006 | if (repwstr == NULL) |
| 4007 | goto onError; |
| 4008 | /* need more space? (at least enough for what we |
| 4009 | have+the replacement+the rest of the string (starting |
| 4010 | at the new input position), so we won't have to check space |
| 4011 | when there are no errors in the rest of the string) */ |
| 4012 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4013 | if (requiredsize > outsize) { |
| 4014 | if (requiredsize < 2*outsize) |
| 4015 | requiredsize = 2*outsize; |
| 4016 | if (unicode_resize(output, requiredsize) < 0) |
| 4017 | goto onError; |
| 4018 | } |
| 4019 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4020 | *outpos += repwlen; |
| 4021 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4022 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4023 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4024 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4025 | /* we made it! */ |
| 4026 | res = 0; |
| 4027 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4028 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4029 | Py_XDECREF(restuple); |
| 4030 | return res; |
| 4031 | } |
| 4032 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4033 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4034 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4035 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4036 | |
| 4037 | /* Three simple macros defining base-64. */ |
| 4038 | |
| 4039 | /* Is c a base-64 character? */ |
| 4040 | |
| 4041 | #define IS_BASE64(c) \ |
| 4042 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4043 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4044 | ((c) >= '0' && (c) <= '9') || \ |
| 4045 | (c) == '+' || (c) == '/') |
| 4046 | |
| 4047 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4048 | |
| 4049 | #define FROM_BASE64(c) \ |
| 4050 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4051 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4052 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4053 | (c) == '+' ? 62 : 63) |
| 4054 | |
| 4055 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4056 | |
| 4057 | #define TO_BASE64(n) \ |
| 4058 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4059 | |
| 4060 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4061 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4062 | * byte not decoding to itself is the + which begins a base64 |
| 4063 | * string. */ |
| 4064 | |
| 4065 | #define DECODE_DIRECT(c) \ |
| 4066 | ((c) <= 127 && (c) != '+') |
| 4067 | |
| 4068 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4069 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4070 | * the above). See RFC2152. This array identifies these different |
| 4071 | * sets: |
| 4072 | * 0 : "Set D" |
| 4073 | * alphanumeric and '(),-./:? |
| 4074 | * 1 : "Set O" |
| 4075 | * !"#$%&*;<=>@[]^_`{|} |
| 4076 | * 2 : "whitespace" |
| 4077 | * ht nl cr sp |
| 4078 | * 3 : special (must be base64 encoded) |
| 4079 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4080 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4081 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4082 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4083 | char utf7_category[128] = { |
| 4084 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4085 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4086 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4087 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4088 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4089 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4090 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4091 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4092 | /* @ A B C D E F G H I J K L M N O */ |
| 4093 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4094 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4095 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4096 | /* ` a b c d e f g h i j k l m n o */ |
| 4097 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4098 | /* p q r s t u v w x y z { | } ~ del */ |
| 4099 | 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] | 4100 | }; |
| 4101 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4102 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4103 | * answer depends on whether we are encoding set O as itself, and also |
| 4104 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4105 | * clear that the answers to these questions vary between |
| 4106 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4107 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4108 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4109 | ((c) < 128 && (c) > 0 && \ |
| 4110 | ((utf7_category[(c)] == 0) || \ |
| 4111 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4112 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4113 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4114 | PyObject * |
| 4115 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4116 | Py_ssize_t size, |
| 4117 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4118 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4119 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4120 | } |
| 4121 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4122 | /* The decoder. The only state we preserve is our read position, |
| 4123 | * i.e. how many characters we have consumed. So if we end in the |
| 4124 | * middle of a shift sequence we have to back off the read position |
| 4125 | * and the output to the beginning of the sequence, otherwise we lose |
| 4126 | * all the shift state (seen bits, number of bits seen, high |
| 4127 | * surrogate). */ |
| 4128 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4129 | PyObject * |
| 4130 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4131 | Py_ssize_t size, |
| 4132 | const char *errors, |
| 4133 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4134 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4135 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4136 | Py_ssize_t startinpos; |
| 4137 | Py_ssize_t endinpos; |
| 4138 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4139 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4140 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4141 | const char *errmsg = ""; |
| 4142 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4143 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4144 | unsigned int base64bits = 0; |
| 4145 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4146 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | PyObject *errorHandler = NULL; |
| 4148 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4149 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4150 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4151 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4152 | if (!unicode) |
| 4153 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4154 | if (size == 0) { |
| 4155 | if (consumed) |
| 4156 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4157 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4158 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4159 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4160 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4161 | e = s + size; |
| 4162 | |
| 4163 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4164 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4165 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4166 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4167 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4168 | if (inShift) { /* in a base-64 section */ |
| 4169 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4170 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4171 | base64bits += 6; |
| 4172 | s++; |
| 4173 | if (base64bits >= 16) { |
| 4174 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4175 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4176 | base64bits -= 16; |
| 4177 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4178 | if (surrogate) { |
| 4179 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4180 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4181 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4182 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4183 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4184 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4185 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4186 | } |
| 4187 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4188 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4189 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4190 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4191 | } |
| 4192 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4193 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4194 | /* first surrogate */ |
| 4195 | surrogate = outCh; |
| 4196 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4197 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4198 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4199 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4204 | inShift = 0; |
| 4205 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4206 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4207 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4208 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4209 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4210 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4211 | if (base64bits > 0) { /* left-over bits */ |
| 4212 | if (base64bits >= 6) { |
| 4213 | /* We've seen at least one base-64 character */ |
| 4214 | errmsg = "partial character in shift sequence"; |
| 4215 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4216 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4217 | else { |
| 4218 | /* Some bits remain; they should be zero */ |
| 4219 | if (base64buffer != 0) { |
| 4220 | errmsg = "non-zero padding bits in shift sequence"; |
| 4221 | goto utf7Error; |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | if (ch != '-') { |
| 4226 | /* '-' is absorbed; other terminating |
| 4227 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4228 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4229 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4230 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4231 | } |
| 4232 | } |
| 4233 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4234 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4235 | s++; /* consume '+' */ |
| 4236 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4237 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4238 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4239 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4240 | } |
| 4241 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4242 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4243 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4244 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4247 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4248 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4249 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4250 | s++; |
| 4251 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4252 | else { |
| 4253 | startinpos = s-starts; |
| 4254 | s++; |
| 4255 | errmsg = "unexpected special character"; |
| 4256 | goto utf7Error; |
| 4257 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4258 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4259 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4260 | endinpos = s-starts; |
| 4261 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4262 | errors, &errorHandler, |
| 4263 | "utf7", errmsg, |
| 4264 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4265 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4266 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4269 | /* end of string */ |
| 4270 | |
| 4271 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4272 | /* if we're in an inconsistent state, that's an error */ |
| 4273 | if (surrogate || |
| 4274 | (base64bits >= 6) || |
| 4275 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4276 | endinpos = size; |
| 4277 | if (unicode_decode_call_errorhandler( |
| 4278 | errors, &errorHandler, |
| 4279 | "utf7", "unterminated shift sequence", |
| 4280 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4281 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | goto onError; |
| 4283 | if (s < e) |
| 4284 | goto restart; |
| 4285 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4286 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4287 | |
| 4288 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4289 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4290 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4291 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4292 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4293 | } |
| 4294 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4295 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4296 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4297 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4298 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4299 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4300 | goto onError; |
| 4301 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4302 | Py_XDECREF(errorHandler); |
| 4303 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4304 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4305 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4306 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4307 | Py_XDECREF(errorHandler); |
| 4308 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4309 | Py_DECREF(unicode); |
| 4310 | return NULL; |
| 4311 | } |
| 4312 | |
| 4313 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4314 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4315 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4316 | int base64SetO, |
| 4317 | int base64WhiteSpace, |
| 4318 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4320 | int kind; |
| 4321 | void *data; |
| 4322 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4323 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4324 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4325 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4326 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4327 | unsigned int base64bits = 0; |
| 4328 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4329 | char * out; |
| 4330 | char * start; |
| 4331 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4332 | if (PyUnicode_READY(str) < 0) |
| 4333 | return NULL; |
| 4334 | kind = PyUnicode_KIND(str); |
| 4335 | data = PyUnicode_DATA(str); |
| 4336 | len = PyUnicode_GET_LENGTH(str); |
| 4337 | |
| 4338 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4339 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4340 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4341 | /* It might be possible to tighten this worst case */ |
| 4342 | allocated = 8 * len; |
| 4343 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4344 | return PyErr_NoMemory(); |
| 4345 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4346 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4347 | if (v == NULL) |
| 4348 | return NULL; |
| 4349 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4350 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4351 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4352 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4353 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | if (inShift) { |
| 4355 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4356 | /* shifting out */ |
| 4357 | if (base64bits) { /* output remaining bits */ |
| 4358 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4359 | base64buffer = 0; |
| 4360 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4361 | } |
| 4362 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4364 | so no '-' is required, except if the character is itself a '-' */ |
| 4365 | if (IS_BASE64(ch) || ch == '-') { |
| 4366 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4367 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 | *out++ = (char) ch; |
| 4369 | } |
| 4370 | else { |
| 4371 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4372 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4373 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4374 | else { /* not in a shift sequence */ |
| 4375 | if (ch == '+') { |
| 4376 | *out++ = '+'; |
| 4377 | *out++ = '-'; |
| 4378 | } |
| 4379 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4380 | *out++ = (char) ch; |
| 4381 | } |
| 4382 | else { |
| 4383 | *out++ = '+'; |
| 4384 | inShift = 1; |
| 4385 | goto encode_char; |
| 4386 | } |
| 4387 | } |
| 4388 | continue; |
| 4389 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4390 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4391 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4392 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4393 | /* code first surrogate */ |
| 4394 | base64bits += 16; |
| 4395 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4396 | while (base64bits >= 6) { |
| 4397 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4398 | base64bits -= 6; |
| 4399 | } |
| 4400 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4401 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4402 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 | base64bits += 16; |
| 4404 | base64buffer = (base64buffer << 16) | ch; |
| 4405 | while (base64bits >= 6) { |
| 4406 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4407 | base64bits -= 6; |
| 4408 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4409 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4410 | if (base64bits) |
| 4411 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4412 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4413 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4414 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4415 | return NULL; |
| 4416 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4418 | PyObject * |
| 4419 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4420 | Py_ssize_t size, |
| 4421 | int base64SetO, |
| 4422 | int base64WhiteSpace, |
| 4423 | const char *errors) |
| 4424 | { |
| 4425 | PyObject *result; |
| 4426 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4427 | if (tmp == NULL) |
| 4428 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4429 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4430 | base64WhiteSpace, errors); |
| 4431 | Py_DECREF(tmp); |
| 4432 | return result; |
| 4433 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4434 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4435 | #undef IS_BASE64 |
| 4436 | #undef FROM_BASE64 |
| 4437 | #undef TO_BASE64 |
| 4438 | #undef DECODE_DIRECT |
| 4439 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4441 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4442 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4443 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4444 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4445 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4446 | illegal prefix. See RFC 3629 for details */ |
| 4447 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4448 | 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] | 4449 | 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] | 4450 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4454 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4455 | 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] | 4456 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4457 | 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] | 4458 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4459 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4460 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4461 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4462 | 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] | 4463 | }; |
| 4464 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4465 | PyObject * |
| 4466 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4467 | Py_ssize_t size, |
| 4468 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4470 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4471 | } |
| 4472 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4473 | #include "stringlib/ucs1lib.h" |
| 4474 | #include "stringlib/codecs.h" |
| 4475 | #include "stringlib/undef.h" |
| 4476 | |
| 4477 | #include "stringlib/ucs2lib.h" |
| 4478 | #include "stringlib/codecs.h" |
| 4479 | #include "stringlib/undef.h" |
| 4480 | |
| 4481 | #include "stringlib/ucs4lib.h" |
| 4482 | #include "stringlib/codecs.h" |
| 4483 | #include "stringlib/undef.h" |
| 4484 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4485 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4486 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4487 | |
| 4488 | /* Mask to quickly check whether a C 'long' contains a |
| 4489 | non-ASCII, UTF8-encoded char. */ |
| 4490 | #if (SIZEOF_LONG == 8) |
| 4491 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4492 | #elif (SIZEOF_LONG == 4) |
| 4493 | # define ASCII_CHAR_MASK 0x80808080L |
| 4494 | #else |
| 4495 | # error C 'long' size should be either 4 or 8! |
| 4496 | #endif |
| 4497 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4498 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4499 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4500 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4501 | 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] | 4502 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4503 | */ |
| 4504 | static Py_UCS4 |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4505 | 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] | 4506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4507 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4508 | const unsigned char *end = p + string_size; |
| 4509 | 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] | 4510 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4511 | assert(unicode_size != NULL); |
| 4512 | |
| 4513 | /* By having a cascade of independent loops which fallback onto each |
| 4514 | other, we minimize the amount of work done in the average loop |
| 4515 | iteration, and we also maximize the CPU's ability to predict |
| 4516 | branches correctly (because a given condition will have always the |
| 4517 | same boolean outcome except perhaps in the last iteration of the |
| 4518 | corresponding loop). |
| 4519 | In the general case this brings us rather close to decoding |
| 4520 | performance pre-PEP 393, despite the two-pass decoding. |
| 4521 | |
| 4522 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4523 | character has been encountered. It is actually a pessimization (by |
| 4524 | a significant factor) to use this loop on text with many non-ASCII |
| 4525 | characters, and it is important to avoid bad performance on valid |
| 4526 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4527 | */ |
| 4528 | |
| 4529 | /* ASCII */ |
| 4530 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4531 | /* Only check value if it's not a ASCII char... */ |
| 4532 | if (*p < 0x80) { |
| 4533 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4534 | an explanation. */ |
| 4535 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4536 | /* Help register allocation */ |
| 4537 | register const unsigned char *_p = p; |
| 4538 | while (_p < aligned_end) { |
| 4539 | unsigned long value = *(unsigned long *) _p; |
| 4540 | if (value & ASCII_CHAR_MASK) |
| 4541 | break; |
| 4542 | _p += SIZEOF_LONG; |
| 4543 | char_count += SIZEOF_LONG; |
| 4544 | } |
| 4545 | p = _p; |
| 4546 | if (p == end) |
| 4547 | break; |
| 4548 | } |
| 4549 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4550 | if (*p < 0x80) |
| 4551 | ++char_count; |
| 4552 | else |
| 4553 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4554 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4555 | *unicode_size = char_count; |
| 4556 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4557 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4558 | _ucs1loop: |
| 4559 | for (; p < end; ++p) { |
| 4560 | if (*p < 0xc4) |
| 4561 | char_count += ((*p & 0xc0) != 0x80); |
| 4562 | else |
| 4563 | goto _ucs2loop; |
| 4564 | } |
| 4565 | *unicode_size = char_count; |
| 4566 | return 255; |
| 4567 | |
| 4568 | _ucs2loop: |
| 4569 | for (; p < end; ++p) { |
| 4570 | if (*p < 0xf0) |
| 4571 | char_count += ((*p & 0xc0) != 0x80); |
| 4572 | else |
| 4573 | goto _ucs4loop; |
| 4574 | } |
| 4575 | *unicode_size = char_count; |
| 4576 | return 65535; |
| 4577 | |
| 4578 | _ucs4loop: |
| 4579 | for (; p < end; ++p) { |
| 4580 | char_count += ((*p & 0xc0) != 0x80); |
| 4581 | } |
| 4582 | *unicode_size = char_count; |
| 4583 | return 65537; |
| 4584 | } |
| 4585 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4586 | /* 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] | 4587 | in case of errors. Implicit parameters: unicode, kind, data, onError. |
| 4588 | 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] | 4589 | */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4590 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4591 | do { \ |
| 4592 | Py_ssize_t pos = index; \ |
| 4593 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4594 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4595 | goto onError; \ |
| 4596 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4597 | goto onError; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4598 | } while (0) |
| 4599 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 4600 | static PyObject * |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4601 | decode_utf8_errors(const char *starts, |
| 4602 | Py_ssize_t size, |
| 4603 | const char *errors, |
| 4604 | Py_ssize_t *consumed, |
| 4605 | const char *s, |
| 4606 | PyObject *unicode, |
| 4607 | Py_ssize_t i) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4608 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4610 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4611 | Py_ssize_t startinpos; |
| 4612 | Py_ssize_t endinpos; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4613 | const char *e = starts + size; |
| 4614 | const char *aligned_end; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4615 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | PyObject *errorHandler = NULL; |
| 4617 | PyObject *exc = NULL; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4618 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4619 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4620 | |
| 4621 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4622 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4623 | |
| 4624 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4625 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4626 | input will consist of an overwhelming majority of ASCII |
| 4627 | characters, we try to optimize for this case by checking |
| 4628 | as many characters as a C 'long' can contain. |
| 4629 | First, check if we can do an aligned read, as most CPUs have |
| 4630 | a penalty for unaligned reads. |
| 4631 | */ |
| 4632 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4633 | /* Help register allocation */ |
| 4634 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4635 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | while (_s < aligned_end) { |
| 4637 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4638 | and do a fast unrolled copy if it only contains ASCII |
| 4639 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4640 | unsigned long value = *(unsigned long *) _s; |
| 4641 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4642 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4643 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4644 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4645 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4646 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4647 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4648 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4649 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4650 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4651 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4652 | #endif |
| 4653 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4654 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4655 | } |
| 4656 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4657 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4658 | if (s == e) |
| 4659 | break; |
| 4660 | ch = (unsigned char)*s; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4665 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4666 | s++; |
| 4667 | continue; |
| 4668 | } |
| 4669 | |
| 4670 | n = utf8_code_length[ch]; |
| 4671 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4672 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4673 | if (consumed) |
| 4674 | break; |
| 4675 | else { |
| 4676 | errmsg = "unexpected end of data"; |
| 4677 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4678 | endinpos = startinpos+1; |
| 4679 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4680 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4681 | goto utf8Error; |
| 4682 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4683 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | |
| 4685 | switch (n) { |
| 4686 | |
| 4687 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4688 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | startinpos = s-starts; |
| 4690 | endinpos = startinpos+1; |
| 4691 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4692 | |
| 4693 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4694 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4695 | startinpos = s-starts; |
| 4696 | endinpos = startinpos+1; |
| 4697 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4698 | |
| 4699 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4700 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4701 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4702 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4703 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | goto utf8Error; |
| 4705 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4707 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4708 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4709 | break; |
| 4710 | |
| 4711 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4712 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4713 | will result in surrogates in range d800-dfff. Surrogates are |
| 4714 | not valid UTF-8 so they are rejected. |
| 4715 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4716 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4717 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4718 | (s[2] & 0xc0) != 0x80 || |
| 4719 | ((unsigned char)s[0] == 0xE0 && |
| 4720 | (unsigned char)s[1] < 0xA0) || |
| 4721 | ((unsigned char)s[0] == 0xED && |
| 4722 | (unsigned char)s[1] > 0x9F)) { |
| 4723 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4724 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4725 | endinpos = startinpos + 1; |
| 4726 | |
| 4727 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4728 | continuation byte is s[2], so increment endinpos by 1, |
| 4729 | if not, s[1] is invalid and endinpos doesn't need to |
| 4730 | be incremented. */ |
| 4731 | if ((s[1] & 0xC0) == 0x80) |
| 4732 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4733 | goto utf8Error; |
| 4734 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4735 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4736 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4737 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4738 | break; |
| 4739 | |
| 4740 | case 4: |
| 4741 | if ((s[1] & 0xc0) != 0x80 || |
| 4742 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4743 | (s[3] & 0xc0) != 0x80 || |
| 4744 | ((unsigned char)s[0] == 0xF0 && |
| 4745 | (unsigned char)s[1] < 0x90) || |
| 4746 | ((unsigned char)s[0] == 0xF4 && |
| 4747 | (unsigned char)s[1] > 0x8F)) { |
| 4748 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4749 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4750 | endinpos = startinpos + 1; |
| 4751 | if ((s[1] & 0xC0) == 0x80) { |
| 4752 | endinpos++; |
| 4753 | if ((s[2] & 0xC0) == 0x80) |
| 4754 | endinpos++; |
| 4755 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4756 | goto utf8Error; |
| 4757 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4758 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4759 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4760 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4761 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4762 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4763 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4764 | } |
| 4765 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4766 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4767 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4768 | utf8Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4769 | if (unicode_decode_call_errorhandler( |
| 4770 | errors, &errorHandler, |
| 4771 | "utf8", errmsg, |
| 4772 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4773 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4774 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4775 | /* Update data because unicode_decode_call_errorhandler might have |
| 4776 | re-created or resized the unicode object. */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4777 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4779 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4780 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4781 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4782 | /* Adjust length and ready string when it contained errors and |
| 4783 | is of the old resizable kind. */ |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4784 | if (unicode_resize(&unicode, i) < 0) |
| 4785 | goto onError; |
| 4786 | unicode_adjust_maxchar(&unicode); |
| 4787 | if (unicode == NULL) |
| 4788 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4789 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | Py_XDECREF(errorHandler); |
| 4791 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4792 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4793 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4795 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4796 | Py_XDECREF(errorHandler); |
| 4797 | Py_XDECREF(exc); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4798 | Py_XDECREF(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 | return NULL; |
| 4800 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4801 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4802 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4803 | PyObject * |
| 4804 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4805 | Py_ssize_t size, |
| 4806 | const char *errors, |
| 4807 | Py_ssize_t *consumed) |
| 4808 | { |
| 4809 | Py_UCS4 maxchar = 0; |
| 4810 | Py_ssize_t unicode_size; |
| 4811 | int has_errors = 0; |
| 4812 | PyObject *unicode; |
| 4813 | int kind; |
| 4814 | void *data; |
| 4815 | const char *starts = s; |
| 4816 | const char *e; |
| 4817 | Py_ssize_t i; |
| 4818 | |
| 4819 | if (size == 0) { |
| 4820 | if (consumed) |
| 4821 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4822 | Py_INCREF(unicode_empty); |
| 4823 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4824 | } |
| 4825 | |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 4826 | maxchar = utf8_scanner((const unsigned char *)s, size, &unicode_size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4827 | |
| 4828 | /* When the string is ASCII only, just use memcpy and return. |
| 4829 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4830 | sequence at the end of the ASCII block. */ |
| 4831 | if (maxchar < 128 && size == unicode_size) { |
| 4832 | if (consumed) |
| 4833 | *consumed = size; |
Victor Stinner | ab87021 | 2011-12-17 22:39:43 +0100 | [diff] [blame] | 4834 | return unicode_fromascii((const unsigned char *)s, size); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4838 | if (!unicode) |
| 4839 | return NULL; |
| 4840 | kind = PyUnicode_KIND(unicode); |
| 4841 | data = PyUnicode_DATA(unicode); |
| 4842 | |
| 4843 | /* Unpack UTF-8 encoded data */ |
| 4844 | i = 0; |
| 4845 | e = starts + size; |
| 4846 | switch (kind) { |
| 4847 | case PyUnicode_1BYTE_KIND: |
| 4848 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4849 | break; |
| 4850 | case PyUnicode_2BYTE_KIND: |
| 4851 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4852 | break; |
| 4853 | case PyUnicode_4BYTE_KIND: |
| 4854 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4855 | break; |
| 4856 | } |
| 4857 | if (!has_errors) { |
| 4858 | /* Ensure the unicode size calculation was correct */ |
| 4859 | assert(i == unicode_size); |
| 4860 | assert(s == e); |
| 4861 | if (consumed) |
| 4862 | *consumed = size; |
| 4863 | return unicode; |
| 4864 | } |
| 4865 | |
| 4866 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4867 | code below refits and resizes as necessary. */ |
| 4868 | return decode_utf8_errors(starts, size, errors, consumed, s, unicode, i); |
| 4869 | } |
| 4870 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4871 | #ifdef __APPLE__ |
| 4872 | |
| 4873 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4874 | used to decode the command line arguments on Mac OS X. */ |
| 4875 | |
| 4876 | wchar_t* |
| 4877 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4878 | { |
| 4879 | int n; |
| 4880 | const char *e; |
| 4881 | wchar_t *unicode, *p; |
| 4882 | |
| 4883 | /* Note: size will always be longer than the resulting Unicode |
| 4884 | character count */ |
| 4885 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4886 | PyErr_NoMemory(); |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4890 | if (!unicode) |
| 4891 | return NULL; |
| 4892 | |
| 4893 | /* Unpack UTF-8 encoded data */ |
| 4894 | p = unicode; |
| 4895 | e = s + size; |
| 4896 | while (s < e) { |
| 4897 | Py_UCS4 ch = (unsigned char)*s; |
| 4898 | |
| 4899 | if (ch < 0x80) { |
| 4900 | *p++ = (wchar_t)ch; |
| 4901 | s++; |
| 4902 | continue; |
| 4903 | } |
| 4904 | |
| 4905 | n = utf8_code_length[ch]; |
| 4906 | if (s + n > e) { |
| 4907 | goto surrogateescape; |
| 4908 | } |
| 4909 | |
| 4910 | switch (n) { |
| 4911 | case 0: |
| 4912 | case 1: |
| 4913 | goto surrogateescape; |
| 4914 | |
| 4915 | case 2: |
| 4916 | if ((s[1] & 0xc0) != 0x80) |
| 4917 | goto surrogateescape; |
| 4918 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4919 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4920 | *p++ = (wchar_t)ch; |
| 4921 | break; |
| 4922 | |
| 4923 | case 3: |
| 4924 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4925 | will result in surrogates in range d800-dfff. Surrogates are |
| 4926 | not valid UTF-8 so they are rejected. |
| 4927 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4928 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4929 | if ((s[1] & 0xc0) != 0x80 || |
| 4930 | (s[2] & 0xc0) != 0x80 || |
| 4931 | ((unsigned char)s[0] == 0xE0 && |
| 4932 | (unsigned char)s[1] < 0xA0) || |
| 4933 | ((unsigned char)s[0] == 0xED && |
| 4934 | (unsigned char)s[1] > 0x9F)) { |
| 4935 | |
| 4936 | goto surrogateescape; |
| 4937 | } |
| 4938 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4939 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4940 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4941 | break; |
| 4942 | |
| 4943 | case 4: |
| 4944 | if ((s[1] & 0xc0) != 0x80 || |
| 4945 | (s[2] & 0xc0) != 0x80 || |
| 4946 | (s[3] & 0xc0) != 0x80 || |
| 4947 | ((unsigned char)s[0] == 0xF0 && |
| 4948 | (unsigned char)s[1] < 0x90) || |
| 4949 | ((unsigned char)s[0] == 0xF4 && |
| 4950 | (unsigned char)s[1] > 0x8F)) { |
| 4951 | goto surrogateescape; |
| 4952 | } |
| 4953 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4954 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4955 | assert ((ch > 0xFFFF) && (ch <= MAX_UNICODE)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4956 | |
| 4957 | #if SIZEOF_WCHAR_T == 4 |
| 4958 | *p++ = (wchar_t)ch; |
| 4959 | #else |
| 4960 | /* compute and append the two surrogates: */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4961 | *p++ = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4962 | *p++ = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4963 | #endif |
| 4964 | break; |
| 4965 | } |
| 4966 | s += n; |
| 4967 | continue; |
| 4968 | |
| 4969 | surrogateescape: |
| 4970 | *p++ = 0xDC00 + ch; |
| 4971 | s++; |
| 4972 | } |
| 4973 | *p = L'\0'; |
| 4974 | return unicode; |
| 4975 | } |
| 4976 | |
| 4977 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4979 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4980 | |
| 4981 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4982 | and allocate exactly as much space needed at the end. Else allocate the |
| 4983 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4984 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4985 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4986 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4987 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4988 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4989 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4990 | void *data; |
| 4991 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4993 | if (!PyUnicode_Check(unicode)) { |
| 4994 | PyErr_BadArgument(); |
| 4995 | return NULL; |
| 4996 | } |
| 4997 | |
| 4998 | if (PyUnicode_READY(unicode) == -1) |
| 4999 | return NULL; |
| 5000 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5001 | if (PyUnicode_UTF8(unicode)) |
| 5002 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5003 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5004 | |
| 5005 | kind = PyUnicode_KIND(unicode); |
| 5006 | data = PyUnicode_DATA(unicode); |
| 5007 | size = PyUnicode_GET_LENGTH(unicode); |
| 5008 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5009 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5010 | default: |
| 5011 | assert(0); |
| 5012 | case PyUnicode_1BYTE_KIND: |
| 5013 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5014 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5015 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5016 | case PyUnicode_2BYTE_KIND: |
| 5017 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5018 | case PyUnicode_4BYTE_KIND: |
| 5019 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5020 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5023 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5024 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5025 | Py_ssize_t size, |
| 5026 | const char *errors) |
| 5027 | { |
| 5028 | PyObject *v, *unicode; |
| 5029 | |
| 5030 | unicode = PyUnicode_FromUnicode(s, size); |
| 5031 | if (unicode == NULL) |
| 5032 | return NULL; |
| 5033 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5034 | Py_DECREF(unicode); |
| 5035 | return v; |
| 5036 | } |
| 5037 | |
| 5038 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5039 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5041 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5044 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5045 | |
| 5046 | PyObject * |
| 5047 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | Py_ssize_t size, |
| 5049 | const char *errors, |
| 5050 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | { |
| 5052 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5053 | } |
| 5054 | |
| 5055 | PyObject * |
| 5056 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | Py_ssize_t size, |
| 5058 | const char *errors, |
| 5059 | int *byteorder, |
| 5060 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5061 | { |
| 5062 | const char *starts = s; |
| 5063 | Py_ssize_t startinpos; |
| 5064 | Py_ssize_t endinpos; |
| 5065 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5066 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5067 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5068 | int bo = 0; /* assume native ordering by default */ |
| 5069 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5071 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5072 | int iorder[] = {0, 1, 2, 3}; |
| 5073 | #else |
| 5074 | int iorder[] = {3, 2, 1, 0}; |
| 5075 | #endif |
| 5076 | PyObject *errorHandler = NULL; |
| 5077 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5078 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5079 | q = (unsigned char *)s; |
| 5080 | e = q + size; |
| 5081 | |
| 5082 | if (byteorder) |
| 5083 | bo = *byteorder; |
| 5084 | |
| 5085 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5086 | byte order setting accordingly. In native mode, the leading BOM |
| 5087 | mark is skipped, in all other modes, it is copied to the output |
| 5088 | stream as-is (giving a ZWNBSP character). */ |
| 5089 | if (bo == 0) { |
| 5090 | if (size >= 4) { |
| 5091 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5092 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5093 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5094 | if (bom == 0x0000FEFF) { |
| 5095 | q += 4; |
| 5096 | bo = -1; |
| 5097 | } |
| 5098 | else if (bom == 0xFFFE0000) { |
| 5099 | q += 4; |
| 5100 | bo = 1; |
| 5101 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5102 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | if (bom == 0x0000FEFF) { |
| 5104 | q += 4; |
| 5105 | bo = 1; |
| 5106 | } |
| 5107 | else if (bom == 0xFFFE0000) { |
| 5108 | q += 4; |
| 5109 | bo = -1; |
| 5110 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5111 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | if (bo == -1) { |
| 5116 | /* force LE */ |
| 5117 | iorder[0] = 0; |
| 5118 | iorder[1] = 1; |
| 5119 | iorder[2] = 2; |
| 5120 | iorder[3] = 3; |
| 5121 | } |
| 5122 | else if (bo == 1) { |
| 5123 | /* force BE */ |
| 5124 | iorder[0] = 3; |
| 5125 | iorder[1] = 2; |
| 5126 | iorder[2] = 1; |
| 5127 | iorder[3] = 0; |
| 5128 | } |
| 5129 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5130 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5131 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5132 | if (!unicode) |
| 5133 | return NULL; |
| 5134 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5135 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5136 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5137 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5139 | Py_UCS4 ch; |
| 5140 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5141 | if (e-q<4) { |
| 5142 | if (consumed) |
| 5143 | break; |
| 5144 | errmsg = "truncated data"; |
| 5145 | startinpos = ((const char *)q)-starts; |
| 5146 | endinpos = ((const char *)e)-starts; |
| 5147 | goto utf32Error; |
| 5148 | /* The remaining input chars are ignored if the callback |
| 5149 | chooses to skip the input */ |
| 5150 | } |
| 5151 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5152 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5154 | if (ch >= 0x110000) |
| 5155 | { |
| 5156 | errmsg = "codepoint not in range(0x110000)"; |
| 5157 | startinpos = ((const char *)q)-starts; |
| 5158 | endinpos = startinpos+4; |
| 5159 | goto utf32Error; |
| 5160 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5161 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5162 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5163 | q += 4; |
| 5164 | continue; |
| 5165 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 | if (unicode_decode_call_errorhandler( |
| 5167 | errors, &errorHandler, |
| 5168 | "utf32", errmsg, |
| 5169 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5170 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | if (byteorder) |
| 5175 | *byteorder = bo; |
| 5176 | |
| 5177 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5178 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | |
| 5180 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5181 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5182 | goto onError; |
| 5183 | |
| 5184 | Py_XDECREF(errorHandler); |
| 5185 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5186 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5189 | Py_DECREF(unicode); |
| 5190 | Py_XDECREF(errorHandler); |
| 5191 | Py_XDECREF(exc); |
| 5192 | return NULL; |
| 5193 | } |
| 5194 | |
| 5195 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5196 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5197 | const char *errors, |
| 5198 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5199 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5200 | int kind; |
| 5201 | void *data; |
| 5202 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5203 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5204 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5205 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5206 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5207 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5208 | int iorder[] = {0, 1, 2, 3}; |
| 5209 | #else |
| 5210 | int iorder[] = {3, 2, 1, 0}; |
| 5211 | #endif |
| 5212 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5213 | #define STORECHAR(CH) \ |
| 5214 | do { \ |
| 5215 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5216 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5217 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5218 | p[iorder[0]] = (CH) & 0xff; \ |
| 5219 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5220 | } while(0) |
| 5221 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5222 | if (!PyUnicode_Check(str)) { |
| 5223 | PyErr_BadArgument(); |
| 5224 | return NULL; |
| 5225 | } |
| 5226 | if (PyUnicode_READY(str) < 0) |
| 5227 | return NULL; |
| 5228 | kind = PyUnicode_KIND(str); |
| 5229 | data = PyUnicode_DATA(str); |
| 5230 | len = PyUnicode_GET_LENGTH(str); |
| 5231 | |
| 5232 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5233 | bytesize = nsize * 4; |
| 5234 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5235 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5236 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5237 | if (v == NULL) |
| 5238 | return NULL; |
| 5239 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5240 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5241 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5242 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5243 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5244 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5245 | |
| 5246 | if (byteorder == -1) { |
| 5247 | /* force LE */ |
| 5248 | iorder[0] = 0; |
| 5249 | iorder[1] = 1; |
| 5250 | iorder[2] = 2; |
| 5251 | iorder[3] = 3; |
| 5252 | } |
| 5253 | else if (byteorder == 1) { |
| 5254 | /* force BE */ |
| 5255 | iorder[0] = 3; |
| 5256 | iorder[1] = 2; |
| 5257 | iorder[2] = 1; |
| 5258 | iorder[3] = 0; |
| 5259 | } |
| 5260 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5261 | for (i = 0; i < len; i++) |
| 5262 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5263 | |
| 5264 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5265 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5266 | #undef STORECHAR |
| 5267 | } |
| 5268 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5269 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5270 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5271 | Py_ssize_t size, |
| 5272 | const char *errors, |
| 5273 | int byteorder) |
| 5274 | { |
| 5275 | PyObject *result; |
| 5276 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5277 | if (tmp == NULL) |
| 5278 | return NULL; |
| 5279 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5280 | Py_DECREF(tmp); |
| 5281 | return result; |
| 5282 | } |
| 5283 | |
| 5284 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5285 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5286 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5287 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5291 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5292 | PyObject * |
| 5293 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5294 | Py_ssize_t size, |
| 5295 | const char *errors, |
| 5296 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5298 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5299 | } |
| 5300 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5301 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5302 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5303 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5304 | rare in most input. |
| 5305 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5306 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5307 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5308 | #if (SIZEOF_LONG == 8) |
| 5309 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5310 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5311 | #elif (SIZEOF_LONG == 4) |
| 5312 | # define FAST_CHAR_MASK 0x80008000L |
| 5313 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5314 | #else |
| 5315 | # error C 'long' size should be either 4 or 8! |
| 5316 | #endif |
| 5317 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5318 | PyObject * |
| 5319 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5320 | Py_ssize_t size, |
| 5321 | const char *errors, |
| 5322 | int *byteorder, |
| 5323 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5324 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5325 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5326 | Py_ssize_t startinpos; |
| 5327 | Py_ssize_t endinpos; |
| 5328 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5329 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5330 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5331 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5332 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5333 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5334 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5335 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5336 | int ihi = 1, ilo = 0; |
| 5337 | #else |
| 5338 | int ihi = 0, ilo = 1; |
| 5339 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5340 | PyObject *errorHandler = NULL; |
| 5341 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | |
| 5343 | /* Note: size will always be longer than the resulting Unicode |
| 5344 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5345 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | if (!unicode) |
| 5347 | return NULL; |
| 5348 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5349 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5350 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5351 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5352 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5353 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | |
| 5355 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5356 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5358 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5359 | byte order setting accordingly. In native mode, the leading BOM |
| 5360 | mark is skipped, in all other modes, it is copied to the output |
| 5361 | stream as-is (giving a ZWNBSP character). */ |
| 5362 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5363 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5364 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5365 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | if (bom == 0xFEFF) { |
| 5367 | q += 2; |
| 5368 | bo = -1; |
| 5369 | } |
| 5370 | else if (bom == 0xFFFE) { |
| 5371 | q += 2; |
| 5372 | bo = 1; |
| 5373 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5374 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5375 | if (bom == 0xFEFF) { |
| 5376 | q += 2; |
| 5377 | bo = 1; |
| 5378 | } |
| 5379 | else if (bom == 0xFFFE) { |
| 5380 | q += 2; |
| 5381 | bo = -1; |
| 5382 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5383 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5385 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5387 | if (bo == -1) { |
| 5388 | /* force LE */ |
| 5389 | ihi = 1; |
| 5390 | ilo = 0; |
| 5391 | } |
| 5392 | else if (bo == 1) { |
| 5393 | /* force BE */ |
| 5394 | ihi = 0; |
| 5395 | ilo = 1; |
| 5396 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5397 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5398 | native_ordering = ilo < ihi; |
| 5399 | #else |
| 5400 | native_ordering = ilo > ihi; |
| 5401 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5402 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5403 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5404 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5405 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5406 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5407 | reads are more expensive, better to defer to another iteration. */ |
| 5408 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5409 | /* Fast path for runs of non-surrogate chars. */ |
| 5410 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5411 | int kind = PyUnicode_KIND(unicode); |
| 5412 | void *data = PyUnicode_DATA(unicode); |
| 5413 | while (_q < aligned_end) { |
| 5414 | unsigned long block = * (unsigned long *) _q; |
| 5415 | unsigned short *pblock = (unsigned short*)█ |
| 5416 | Py_UCS4 maxch; |
| 5417 | if (native_ordering) { |
| 5418 | /* Can use buffer directly */ |
| 5419 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5420 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5421 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5422 | else { |
| 5423 | /* Need to byte-swap */ |
| 5424 | unsigned char *_p = (unsigned char*)pblock; |
| 5425 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5426 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5427 | _p[0] = _q[1]; |
| 5428 | _p[1] = _q[0]; |
| 5429 | _p[2] = _q[3]; |
| 5430 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5431 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5432 | _p[4] = _q[5]; |
| 5433 | _p[5] = _q[4]; |
| 5434 | _p[6] = _q[7]; |
| 5435 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5436 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5437 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5438 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5439 | #if SIZEOF_LONG == 8 |
| 5440 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5441 | #endif |
| 5442 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5443 | if (unicode_widen(&unicode, maxch) < 0) |
| 5444 | goto onError; |
| 5445 | kind = PyUnicode_KIND(unicode); |
| 5446 | data = PyUnicode_DATA(unicode); |
| 5447 | } |
| 5448 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5449 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5450 | #if SIZEOF_LONG == 8 |
| 5451 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5452 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5453 | #endif |
| 5454 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5455 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5456 | q = _q; |
| 5457 | if (q >= e) |
| 5458 | break; |
| 5459 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5461 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5462 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5463 | |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5464 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5465 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5466 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5467 | continue; |
| 5468 | } |
| 5469 | |
| 5470 | /* UTF-16 code pair: */ |
| 5471 | if (q > e) { |
| 5472 | errmsg = "unexpected end of data"; |
| 5473 | startinpos = (((const char *)q) - 2) - starts; |
| 5474 | endinpos = ((const char *)e) + 1 - starts; |
| 5475 | goto utf16Error; |
| 5476 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5477 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5478 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5480 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5481 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5482 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5483 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5484 | continue; |
| 5485 | } |
| 5486 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5487 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5488 | startinpos = (((const char *)q)-4)-starts; |
| 5489 | endinpos = startinpos+2; |
| 5490 | goto utf16Error; |
| 5491 | } |
| 5492 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5493 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | errmsg = "illegal encoding"; |
| 5495 | startinpos = (((const char *)q)-2)-starts; |
| 5496 | endinpos = startinpos+2; |
| 5497 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5498 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5499 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5500 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5501 | errors, |
| 5502 | &errorHandler, |
| 5503 | "utf16", errmsg, |
| 5504 | &starts, |
| 5505 | (const char **)&e, |
| 5506 | &startinpos, |
| 5507 | &endinpos, |
| 5508 | &exc, |
| 5509 | (const char **)&q, |
| 5510 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5511 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5514 | /* remaining byte at the end? (size should be even) */ |
| 5515 | if (e == q) { |
| 5516 | if (!consumed) { |
| 5517 | errmsg = "truncated data"; |
| 5518 | startinpos = ((const char *)q) - starts; |
| 5519 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5520 | if (unicode_decode_call_errorhandler( |
| 5521 | errors, |
| 5522 | &errorHandler, |
| 5523 | "utf16", errmsg, |
| 5524 | &starts, |
| 5525 | (const char **)&e, |
| 5526 | &startinpos, |
| 5527 | &endinpos, |
| 5528 | &exc, |
| 5529 | (const char **)&q, |
| 5530 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5531 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5532 | goto onError; |
| 5533 | /* The remaining input chars are ignored if the callback |
| 5534 | chooses to skip the input */ |
| 5535 | } |
| 5536 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | |
| 5538 | if (byteorder) |
| 5539 | *byteorder = bo; |
| 5540 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5541 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5542 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5545 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | goto onError; |
| 5547 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5548 | Py_XDECREF(errorHandler); |
| 5549 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5550 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5552 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5554 | Py_XDECREF(errorHandler); |
| 5555 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | return NULL; |
| 5557 | } |
| 5558 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5559 | #undef FAST_CHAR_MASK |
| 5560 | #undef SWAPPED_FAST_CHAR_MASK |
| 5561 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5562 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5563 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5564 | const char *errors, |
| 5565 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5567 | int kind; |
| 5568 | void *data; |
| 5569 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5570 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5571 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5572 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5573 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5574 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5575 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5576 | int ihi = 1, ilo = 0; |
| 5577 | #else |
| 5578 | int ihi = 0, ilo = 1; |
| 5579 | #endif |
| 5580 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5581 | #define STORECHAR(CH) \ |
| 5582 | do { \ |
| 5583 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5584 | p[ilo] = (CH) & 0xff; \ |
| 5585 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5586 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5587 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5588 | if (!PyUnicode_Check(str)) { |
| 5589 | PyErr_BadArgument(); |
| 5590 | return NULL; |
| 5591 | } |
| 5592 | if (PyUnicode_READY(str) < 0) |
| 5593 | return NULL; |
| 5594 | kind = PyUnicode_KIND(str); |
| 5595 | data = PyUnicode_DATA(str); |
| 5596 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5597 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5598 | pairs = 0; |
| 5599 | if (kind == PyUnicode_4BYTE_KIND) |
| 5600 | for (i = 0; i < len; i++) |
| 5601 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5602 | pairs++; |
| 5603 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5604 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5606 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5607 | bytesize = nsize * 2; |
| 5608 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5609 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5610 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | if (v == NULL) |
| 5612 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5614 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5616 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5617 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5618 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5619 | |
| 5620 | if (byteorder == -1) { |
| 5621 | /* force LE */ |
| 5622 | ihi = 1; |
| 5623 | ilo = 0; |
| 5624 | } |
| 5625 | else if (byteorder == 1) { |
| 5626 | /* force BE */ |
| 5627 | ihi = 0; |
| 5628 | ilo = 1; |
| 5629 | } |
| 5630 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5631 | for (i = 0; i < len; i++) { |
| 5632 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5633 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | if (ch >= 0x10000) { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 5635 | ch2 = Py_UNICODE_LOW_SURROGATE(ch); |
| 5636 | ch = Py_UNICODE_HIGH_SURROGATE(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5637 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5638 | STORECHAR(ch); |
| 5639 | if (ch2) |
| 5640 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5641 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5642 | |
| 5643 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5644 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5645 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5648 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5649 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5650 | Py_ssize_t size, |
| 5651 | const char *errors, |
| 5652 | int byteorder) |
| 5653 | { |
| 5654 | PyObject *result; |
| 5655 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5656 | if (tmp == NULL) |
| 5657 | return NULL; |
| 5658 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5659 | Py_DECREF(tmp); |
| 5660 | return result; |
| 5661 | } |
| 5662 | |
| 5663 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5664 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5666 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5670 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5671 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5672 | if all the escapes in the string make it still a valid ASCII string. |
| 5673 | Returns -1 if any escapes were found which cause the string to |
| 5674 | pop out of ASCII range. Otherwise returns the length of the |
| 5675 | required buffer to hold the string. |
| 5676 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5677 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5678 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5679 | { |
| 5680 | const unsigned char *p = (const unsigned char *)s; |
| 5681 | const unsigned char *end = p + size; |
| 5682 | Py_ssize_t length = 0; |
| 5683 | |
| 5684 | if (size < 0) |
| 5685 | return -1; |
| 5686 | |
| 5687 | for (; p < end; ++p) { |
| 5688 | if (*p > 127) { |
| 5689 | /* Non-ASCII */ |
| 5690 | return -1; |
| 5691 | } |
| 5692 | else if (*p != '\\') { |
| 5693 | /* Normal character */ |
| 5694 | ++length; |
| 5695 | } |
| 5696 | else { |
| 5697 | /* Backslash-escape, check next char */ |
| 5698 | ++p; |
| 5699 | /* Escape sequence reaches till end of string or |
| 5700 | non-ASCII follow-up. */ |
| 5701 | if (p >= end || *p > 127) |
| 5702 | return -1; |
| 5703 | switch (*p) { |
| 5704 | case '\n': |
| 5705 | /* backslash + \n result in zero characters */ |
| 5706 | break; |
| 5707 | case '\\': case '\'': case '\"': |
| 5708 | case 'b': case 'f': case 't': |
| 5709 | case 'n': case 'r': case 'v': case 'a': |
| 5710 | ++length; |
| 5711 | break; |
| 5712 | case '0': case '1': case '2': case '3': |
| 5713 | case '4': case '5': case '6': case '7': |
| 5714 | case 'x': case 'u': case 'U': case 'N': |
| 5715 | /* these do not guarantee ASCII characters */ |
| 5716 | return -1; |
| 5717 | default: |
| 5718 | /* count the backslash + the other character */ |
| 5719 | length += 2; |
| 5720 | } |
| 5721 | } |
| 5722 | } |
| 5723 | return length; |
| 5724 | } |
| 5725 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5726 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5727 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5728 | PyObject * |
| 5729 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5730 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5731 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5733 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5734 | Py_ssize_t startinpos; |
| 5735 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5736 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5737 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5739 | char* message; |
| 5740 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | PyObject *errorHandler = NULL; |
| 5742 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5743 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5744 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5745 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5746 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5747 | |
| 5748 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5749 | either the string is pure ASCII with named escapes like \n, etc. |
| 5750 | and we determined it's exact size (common case) |
| 5751 | or it contains \x, \u, ... escape sequences. then we create a |
| 5752 | 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] | 5753 | if (len >= 0) { |
| 5754 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5755 | if (!v) |
| 5756 | goto onError; |
| 5757 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5758 | } |
| 5759 | else { |
| 5760 | /* Escaped strings will always be longer than the resulting |
| 5761 | Unicode string, so we start with size here and then reduce the |
| 5762 | length after conversion to the true value. |
| 5763 | (but if the error callback returns a long replacement string |
| 5764 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5765 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5766 | if (!v) |
| 5767 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5768 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5772 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5773 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5774 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | while (s < end) { |
| 5777 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5778 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5779 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5781 | /* The only case in which i == ascii_length is a backslash |
| 5782 | followed by a newline. */ |
| 5783 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5785 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5786 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5787 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5788 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | continue; |
| 5790 | } |
| 5791 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5792 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5793 | /* \ - Escapes */ |
| 5794 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5795 | c = *s++; |
| 5796 | if (s > end) |
| 5797 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5798 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5799 | /* The only case in which i == ascii_length is a backslash |
| 5800 | followed by a newline. */ |
| 5801 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5802 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5803 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5805 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5806 | #define WRITECHAR(ch) \ |
| 5807 | do { \ |
| 5808 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5809 | goto onError; \ |
| 5810 | }while(0) |
| 5811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5813 | case '\\': WRITECHAR('\\'); break; |
| 5814 | case '\'': WRITECHAR('\''); break; |
| 5815 | case '\"': WRITECHAR('\"'); break; |
| 5816 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5817 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5818 | case 'f': WRITECHAR('\014'); break; |
| 5819 | case 't': WRITECHAR('\t'); break; |
| 5820 | case 'n': WRITECHAR('\n'); break; |
| 5821 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5822 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5823 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5824 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5825 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | case '0': case '1': case '2': case '3': |
| 5829 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5830 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5831 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5832 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5833 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5834 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5835 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5836 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | break; |
| 5838 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5839 | /* hex escapes */ |
| 5840 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5842 | digits = 2; |
| 5843 | message = "truncated \\xXX escape"; |
| 5844 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5845 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5846 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5847 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5848 | digits = 4; |
| 5849 | message = "truncated \\uXXXX escape"; |
| 5850 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5853 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5854 | digits = 8; |
| 5855 | message = "truncated \\UXXXXXXXX escape"; |
| 5856 | hexescape: |
| 5857 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5858 | if (s+digits>end) { |
| 5859 | endinpos = size; |
| 5860 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | errors, &errorHandler, |
| 5862 | "unicodeescape", "end of string in escape sequence", |
| 5863 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5864 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5865 | goto onError; |
| 5866 | goto nextByte; |
| 5867 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5868 | for (j = 0; j < digits; ++j) { |
| 5869 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5870 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5871 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5872 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | errors, &errorHandler, |
| 5874 | "unicodeescape", message, |
| 5875 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5876 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5877 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5878 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5880 | } |
| 5881 | chr = (chr<<4) & ~0xF; |
| 5882 | if (c >= '0' && c <= '9') |
| 5883 | chr += c - '0'; |
| 5884 | else if (c >= 'a' && c <= 'f') |
| 5885 | chr += 10 + c - 'a'; |
| 5886 | else |
| 5887 | chr += 10 + c - 'A'; |
| 5888 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5889 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5890 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5891 | /* _decoding_error will have already written into the |
| 5892 | target buffer. */ |
| 5893 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5894 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5895 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5896 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5897 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5898 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5899 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5900 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | errors, &errorHandler, |
| 5902 | "unicodeescape", "illegal Unicode character", |
| 5903 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5904 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5905 | goto onError; |
| 5906 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5907 | break; |
| 5908 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5910 | case 'N': |
| 5911 | message = "malformed \\N character escape"; |
| 5912 | if (ucnhash_CAPI == NULL) { |
| 5913 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5914 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5915 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5916 | if (ucnhash_CAPI == NULL) |
| 5917 | goto ucnhashError; |
| 5918 | } |
| 5919 | if (*s == '{') { |
| 5920 | const char *start = s+1; |
| 5921 | /* look for the closing brace */ |
| 5922 | while (*s != '}' && s < end) |
| 5923 | s++; |
| 5924 | if (s > start && s < end && *s == '}') { |
| 5925 | /* found a name. look it up in the unicode database */ |
| 5926 | message = "unknown Unicode character name"; |
| 5927 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5928 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5929 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5930 | goto store; |
| 5931 | } |
| 5932 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5933 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5934 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | errors, &errorHandler, |
| 5936 | "unicodeescape", message, |
| 5937 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5938 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5939 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5940 | break; |
| 5941 | |
| 5942 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5943 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5944 | message = "\\ at end of string"; |
| 5945 | s--; |
| 5946 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5947 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5948 | errors, &errorHandler, |
| 5949 | "unicodeescape", message, |
| 5950 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5951 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5952 | goto onError; |
| 5953 | } |
| 5954 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5955 | WRITECHAR('\\'); |
| 5956 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5957 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5958 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5960 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5961 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5963 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5964 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5965 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5966 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5967 | Py_XDECREF(errorHandler); |
| 5968 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5969 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5971 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5972 | PyErr_SetString( |
| 5973 | PyExc_UnicodeError, |
| 5974 | "\\N escapes not supported (can't load unicodedata module)" |
| 5975 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5976 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5977 | Py_XDECREF(errorHandler); |
| 5978 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5979 | return NULL; |
| 5980 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5981 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5983 | Py_XDECREF(errorHandler); |
| 5984 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | return NULL; |
| 5986 | } |
| 5987 | |
| 5988 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5989 | |
| 5990 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5991 | appropriate. |
| 5992 | |
| 5993 | */ |
| 5994 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5995 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5996 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5998 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5999 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6001 | int kind; |
| 6002 | void *data; |
| 6003 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6005 | /* Initial allocation is based on the longest-possible unichr |
| 6006 | escape. |
| 6007 | |
| 6008 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 6009 | unichr, so in this case it's the longest unichr escape. In |
| 6010 | narrow (UTF-16) builds this is five chars per source unichr |
| 6011 | since there are two unichrs in the surrogate pair, so in narrow |
| 6012 | (UTF-16) builds it's not the longest unichr escape. |
| 6013 | |
| 6014 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 6015 | so in the narrow (UTF-16) build case it's the longest unichr |
| 6016 | escape. |
| 6017 | */ |
| 6018 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6019 | if (!PyUnicode_Check(unicode)) { |
| 6020 | PyErr_BadArgument(); |
| 6021 | return NULL; |
| 6022 | } |
| 6023 | if (PyUnicode_READY(unicode) < 0) |
| 6024 | return NULL; |
| 6025 | len = PyUnicode_GET_LENGTH(unicode); |
| 6026 | kind = PyUnicode_KIND(unicode); |
| 6027 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6028 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6029 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6030 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6031 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6032 | } |
| 6033 | |
| 6034 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6035 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6036 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6037 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6038 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6039 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6040 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6042 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6043 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | if (repr == NULL) |
| 6045 | return NULL; |
| 6046 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6047 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6049 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6050 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6051 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6052 | /* Escape backslashes */ |
| 6053 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | *p++ = '\\'; |
| 6055 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6056 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6057 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6058 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6059 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6060 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6061 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6062 | *p++ = '\\'; |
| 6063 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6064 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6065 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6066 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6067 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6068 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6069 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6070 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6071 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6073 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6076 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | *p++ = '\\'; |
| 6078 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6079 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6080 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6081 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6082 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6083 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6084 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6085 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6086 | else if (ch == '\t') { |
| 6087 | *p++ = '\\'; |
| 6088 | *p++ = 't'; |
| 6089 | } |
| 6090 | else if (ch == '\n') { |
| 6091 | *p++ = '\\'; |
| 6092 | *p++ = 'n'; |
| 6093 | } |
| 6094 | else if (ch == '\r') { |
| 6095 | *p++ = '\\'; |
| 6096 | *p++ = 'r'; |
| 6097 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6098 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6099 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6100 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6102 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6103 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6104 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6105 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | /* Copy everything else as-is */ |
| 6108 | else |
| 6109 | *p++ = (char) ch; |
| 6110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6112 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6113 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6114 | return NULL; |
| 6115 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6118 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6119 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6120 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6122 | PyObject *result; |
| 6123 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6124 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6126 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6127 | Py_DECREF(tmp); |
| 6128 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6132 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6133 | PyObject * |
| 6134 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6135 | Py_ssize_t size, |
| 6136 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6138 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6139 | Py_ssize_t startinpos; |
| 6140 | Py_ssize_t endinpos; |
| 6141 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6142 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | const char *end; |
| 6144 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | PyObject *errorHandler = NULL; |
| 6146 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | /* Escaped strings will always be longer than the resulting |
| 6149 | 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] | 6150 | length after conversion to the true value. (But decoding error |
| 6151 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6152 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6156 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6157 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6158 | end = s + size; |
| 6159 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6160 | unsigned char c; |
| 6161 | Py_UCS4 x; |
| 6162 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6163 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6165 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6166 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6167 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6168 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6170 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6171 | startinpos = s-starts; |
| 6172 | |
| 6173 | /* \u-escapes are only interpreted iff the number of leading |
| 6174 | backslashes if odd */ |
| 6175 | bs = s; |
| 6176 | for (;s < end;) { |
| 6177 | if (*s != '\\') |
| 6178 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6179 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6180 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6181 | } |
| 6182 | if (((s - bs) & 1) == 0 || |
| 6183 | s >= end || |
| 6184 | (*s != 'u' && *s != 'U')) { |
| 6185 | continue; |
| 6186 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6187 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6188 | count = *s=='u' ? 4 : 8; |
| 6189 | s++; |
| 6190 | |
| 6191 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6192 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6193 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6194 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6195 | endinpos = s-starts; |
| 6196 | if (unicode_decode_call_errorhandler( |
| 6197 | errors, &errorHandler, |
| 6198 | "rawunicodeescape", "truncated \\uXXXX", |
| 6199 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6200 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | goto onError; |
| 6202 | goto nextByte; |
| 6203 | } |
| 6204 | x = (x<<4) & ~0xF; |
| 6205 | if (c >= '0' && c <= '9') |
| 6206 | x += c - '0'; |
| 6207 | else if (c >= 'a' && c <= 'f') |
| 6208 | x += 10 + c - 'a'; |
| 6209 | else |
| 6210 | x += 10 + c - 'A'; |
| 6211 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6212 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6213 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6214 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6215 | } else { |
| 6216 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6217 | if (unicode_decode_call_errorhandler( |
| 6218 | errors, &errorHandler, |
| 6219 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6220 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6221 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6222 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6223 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | nextByte: |
| 6225 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6227 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6229 | Py_XDECREF(errorHandler); |
| 6230 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6231 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6235 | Py_XDECREF(errorHandler); |
| 6236 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6237 | return NULL; |
| 6238 | } |
| 6239 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6240 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6241 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6242 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6244 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | char *p; |
| 6246 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6247 | Py_ssize_t expandsize, pos; |
| 6248 | int kind; |
| 6249 | void *data; |
| 6250 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6252 | if (!PyUnicode_Check(unicode)) { |
| 6253 | PyErr_BadArgument(); |
| 6254 | return NULL; |
| 6255 | } |
| 6256 | if (PyUnicode_READY(unicode) < 0) |
| 6257 | return NULL; |
| 6258 | kind = PyUnicode_KIND(unicode); |
| 6259 | data = PyUnicode_DATA(unicode); |
| 6260 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6261 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6262 | bytes, and 1 byte characters 4. */ |
| 6263 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6264 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6265 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6266 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6267 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6268 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | if (repr == NULL) |
| 6270 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6271 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6272 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6273 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6274 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6275 | for (pos = 0; pos < len; pos++) { |
| 6276 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6278 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6279 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6280 | *p++ = '\\'; |
| 6281 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6282 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6283 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6284 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6285 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6286 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6287 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6288 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6289 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6290 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6292 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6293 | *p++ = '\\'; |
| 6294 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6295 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6296 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6297 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6298 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6299 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | /* Copy everything else as-is */ |
| 6301 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | *p++ = (char) ch; |
| 6303 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6304 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6305 | assert(p > q); |
| 6306 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6307 | return NULL; |
| 6308 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6311 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6312 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6313 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6315 | PyObject *result; |
| 6316 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6317 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6318 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6319 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6320 | Py_DECREF(tmp); |
| 6321 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6324 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6326 | PyObject * |
| 6327 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6328 | Py_ssize_t size, |
| 6329 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6330 | { |
| 6331 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6332 | Py_ssize_t startinpos; |
| 6333 | Py_ssize_t endinpos; |
| 6334 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6335 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6336 | const char *end; |
| 6337 | const char *reason; |
| 6338 | PyObject *errorHandler = NULL; |
| 6339 | PyObject *exc = NULL; |
| 6340 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6341 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6342 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6343 | 1)) |
| 6344 | return NULL; |
| 6345 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6346 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6347 | 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] | 6348 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6350 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6351 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6352 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6353 | end = s + size; |
| 6354 | |
| 6355 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6356 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6357 | Py_UCS4 ch; |
| 6358 | /* We copy the raw representation one byte at a time because the |
| 6359 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6360 | ((char *) &uch)[0] = s[0]; |
| 6361 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6362 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6363 | ((char *) &uch)[2] = s[2]; |
| 6364 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6365 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6366 | ch = uch; |
| 6367 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6368 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6369 | some malformed UCS-4 data. */ |
| 6370 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6372 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6373 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6374 | end-s < Py_UNICODE_SIZE |
| 6375 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6377 | startinpos = s - starts; |
| 6378 | if (end-s < Py_UNICODE_SIZE) { |
| 6379 | endinpos = end-starts; |
| 6380 | reason = "truncated input"; |
| 6381 | } |
| 6382 | else { |
| 6383 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6384 | reason = "illegal code point (> 0x10FFFF)"; |
| 6385 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6386 | if (unicode_decode_call_errorhandler( |
| 6387 | errors, &errorHandler, |
| 6388 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6389 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6390 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6391 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6392 | continue; |
| 6393 | } |
| 6394 | |
| 6395 | s += Py_UNICODE_SIZE; |
| 6396 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6397 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6398 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6399 | Py_UNICODE uch2; |
| 6400 | ((char *) &uch2)[0] = s[0]; |
| 6401 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6402 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6403 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6404 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6405 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6406 | } |
| 6407 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6408 | #endif |
| 6409 | |
| 6410 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6411 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6414 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6415 | goto onError; |
| 6416 | Py_XDECREF(errorHandler); |
| 6417 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6418 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6419 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6421 | Py_XDECREF(v); |
| 6422 | Py_XDECREF(errorHandler); |
| 6423 | Py_XDECREF(exc); |
| 6424 | return NULL; |
| 6425 | } |
| 6426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6428 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6429 | PyObject * |
| 6430 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6431 | Py_ssize_t size, |
| 6432 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6435 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6439 | static void |
| 6440 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6441 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6442 | PyObject *unicode, |
| 6443 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6444 | const char *reason) |
| 6445 | { |
| 6446 | if (*exceptionObject == NULL) { |
| 6447 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6448 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6449 | encoding, unicode, startpos, endpos, reason); |
| 6450 | } |
| 6451 | else { |
| 6452 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6453 | goto onError; |
| 6454 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6455 | goto onError; |
| 6456 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6457 | goto onError; |
| 6458 | return; |
| 6459 | onError: |
| 6460 | Py_DECREF(*exceptionObject); |
| 6461 | *exceptionObject = NULL; |
| 6462 | } |
| 6463 | } |
| 6464 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6466 | static void |
| 6467 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6468 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6469 | PyObject *unicode, |
| 6470 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6471 | const char *reason) |
| 6472 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6473 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6474 | encoding, unicode, startpos, endpos, reason); |
| 6475 | if (*exceptionObject != NULL) |
| 6476 | PyCodec_StrictErrors(*exceptionObject); |
| 6477 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6478 | |
| 6479 | /* error handling callback helper: |
| 6480 | build arguments, call the callback and check the arguments, |
| 6481 | put the result into newpos and return the replacement string, which |
| 6482 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6483 | static PyObject * |
| 6484 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6485 | PyObject **errorHandler, |
| 6486 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6487 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6488 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6489 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6490 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6491 | 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] | 6492 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6493 | PyObject *restuple; |
| 6494 | PyObject *resunicode; |
| 6495 | |
| 6496 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6498 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6502 | if (PyUnicode_READY(unicode) < 0) |
| 6503 | return NULL; |
| 6504 | len = PyUnicode_GET_LENGTH(unicode); |
| 6505 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6506 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6507 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6508 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6510 | |
| 6511 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6513 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6514 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6515 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6516 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | Py_DECREF(restuple); |
| 6518 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6519 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6520 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6521 | &resunicode, newpos)) { |
| 6522 | Py_DECREF(restuple); |
| 6523 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6524 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6525 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6526 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6527 | Py_DECREF(restuple); |
| 6528 | return NULL; |
| 6529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6530 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6531 | *newpos = len + *newpos; |
| 6532 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6534 | Py_DECREF(restuple); |
| 6535 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6536 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6537 | Py_INCREF(resunicode); |
| 6538 | Py_DECREF(restuple); |
| 6539 | return resunicode; |
| 6540 | } |
| 6541 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6542 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6543 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6544 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6545 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6546 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6547 | /* input state */ |
| 6548 | Py_ssize_t pos=0, size; |
| 6549 | int kind; |
| 6550 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6551 | /* output object */ |
| 6552 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6553 | /* pointer into the output */ |
| 6554 | char *str; |
| 6555 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6556 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6557 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6558 | 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] | 6559 | PyObject *errorHandler = NULL; |
| 6560 | PyObject *exc = NULL; |
| 6561 | /* the following variable is used for caching string comparisons |
| 6562 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6563 | int known_errorHandler = -1; |
| 6564 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6565 | if (PyUnicode_READY(unicode) < 0) |
| 6566 | return NULL; |
| 6567 | size = PyUnicode_GET_LENGTH(unicode); |
| 6568 | kind = PyUnicode_KIND(unicode); |
| 6569 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6570 | /* allocate enough for a simple encoding without |
| 6571 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6572 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6573 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6574 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6575 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6576 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6577 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6578 | ressize = size; |
| 6579 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6580 | while (pos < size) { |
| 6581 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6582 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | /* can we encode this? */ |
| 6584 | if (c<limit) { |
| 6585 | /* no overflow check, because we know that the space is enough */ |
| 6586 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6587 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6588 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6589 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | Py_ssize_t requiredsize; |
| 6591 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6592 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6594 | Py_ssize_t collstart = pos; |
| 6595 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6597 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | ++collend; |
| 6599 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6600 | if (known_errorHandler==-1) { |
| 6601 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6602 | known_errorHandler = 1; |
| 6603 | else if (!strcmp(errors, "replace")) |
| 6604 | known_errorHandler = 2; |
| 6605 | else if (!strcmp(errors, "ignore")) |
| 6606 | known_errorHandler = 3; |
| 6607 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6608 | known_errorHandler = 4; |
| 6609 | else |
| 6610 | known_errorHandler = 0; |
| 6611 | } |
| 6612 | switch (known_errorHandler) { |
| 6613 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6614 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | goto onError; |
| 6616 | case 2: /* replace */ |
| 6617 | while (collstart++<collend) |
| 6618 | *str++ = '?'; /* fall through */ |
| 6619 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6621 | break; |
| 6622 | case 4: /* xmlcharrefreplace */ |
| 6623 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6624 | /* determine replacement size */ |
| 6625 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6626 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6627 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6629 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6631 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6632 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6633 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6634 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6635 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6637 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6638 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6639 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6640 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6641 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6642 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6644 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6645 | if (requiredsize > ressize) { |
| 6646 | if (requiredsize<2*ressize) |
| 6647 | requiredsize = 2*ressize; |
| 6648 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6649 | goto onError; |
| 6650 | str = PyBytes_AS_STRING(res) + respos; |
| 6651 | ressize = requiredsize; |
| 6652 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6653 | /* generate replacement */ |
| 6654 | for (i = collstart; i < collend; ++i) { |
| 6655 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6656 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6657 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6658 | break; |
| 6659 | default: |
| 6660 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6661 | encoding, reason, unicode, &exc, |
| 6662 | collstart, collend, &newpos); |
| 6663 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6664 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6665 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6666 | if (PyBytes_Check(repunicode)) { |
| 6667 | /* Directly copy bytes result to output. */ |
| 6668 | repsize = PyBytes_Size(repunicode); |
| 6669 | if (repsize > 1) { |
| 6670 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6671 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6672 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6673 | Py_DECREF(repunicode); |
| 6674 | goto onError; |
| 6675 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6676 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6677 | ressize += repsize-1; |
| 6678 | } |
| 6679 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6680 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6681 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6682 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6683 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6684 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6685 | /* need more space? (at least enough for what we |
| 6686 | have+the replacement+the rest of the string, so |
| 6687 | we won't have to check space for encodable characters) */ |
| 6688 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6689 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6690 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6691 | if (requiredsize > ressize) { |
| 6692 | if (requiredsize<2*ressize) |
| 6693 | requiredsize = 2*ressize; |
| 6694 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6695 | Py_DECREF(repunicode); |
| 6696 | goto onError; |
| 6697 | } |
| 6698 | str = PyBytes_AS_STRING(res) + respos; |
| 6699 | ressize = requiredsize; |
| 6700 | } |
| 6701 | /* check if there is anything unencodable in the replacement |
| 6702 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6703 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6704 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6706 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6707 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6708 | Py_DECREF(repunicode); |
| 6709 | goto onError; |
| 6710 | } |
| 6711 | *str = (char)c; |
| 6712 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6713 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6714 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6715 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6716 | } |
| 6717 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6718 | /* Resize if we allocated to much */ |
| 6719 | size = str - PyBytes_AS_STRING(res); |
| 6720 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6721 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6722 | if (_PyBytes_Resize(&res, size) < 0) |
| 6723 | goto onError; |
| 6724 | } |
| 6725 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6726 | Py_XDECREF(errorHandler); |
| 6727 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6728 | return res; |
| 6729 | |
| 6730 | onError: |
| 6731 | Py_XDECREF(res); |
| 6732 | Py_XDECREF(errorHandler); |
| 6733 | Py_XDECREF(exc); |
| 6734 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6737 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6738 | PyObject * |
| 6739 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6740 | Py_ssize_t size, |
| 6741 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6743 | PyObject *result; |
| 6744 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6745 | if (unicode == NULL) |
| 6746 | return NULL; |
| 6747 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6748 | Py_DECREF(unicode); |
| 6749 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6752 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6753 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | { |
| 6755 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6756 | PyErr_BadArgument(); |
| 6757 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6759 | if (PyUnicode_READY(unicode) == -1) |
| 6760 | return NULL; |
| 6761 | /* Fast path: if it is a one-byte string, construct |
| 6762 | bytes object directly. */ |
| 6763 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6764 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6765 | PyUnicode_GET_LENGTH(unicode)); |
| 6766 | /* Non-Latin-1 characters present. Defer to above function to |
| 6767 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6768 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | PyObject* |
| 6772 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6773 | { |
| 6774 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6778 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6779 | PyObject * |
| 6780 | PyUnicode_DecodeASCII(const char *s, |
| 6781 | Py_ssize_t size, |
| 6782 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6784 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6785 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6786 | int kind; |
| 6787 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6788 | Py_ssize_t startinpos; |
| 6789 | Py_ssize_t endinpos; |
| 6790 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6791 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6792 | int has_error; |
| 6793 | const unsigned char *p = (const unsigned char *)s; |
| 6794 | const unsigned char *end = p + size; |
| 6795 | 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] | 6796 | PyObject *errorHandler = NULL; |
| 6797 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6798 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6799 | if (size == 0) { |
| 6800 | Py_INCREF(unicode_empty); |
| 6801 | return unicode_empty; |
| 6802 | } |
| 6803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6805 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6806 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6807 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6808 | has_error = 0; |
| 6809 | while (p < end && !has_error) { |
| 6810 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6811 | an explanation. */ |
| 6812 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6813 | /* Help register allocation */ |
| 6814 | register const unsigned char *_p = p; |
| 6815 | while (_p < aligned_end) { |
| 6816 | unsigned long value = *(unsigned long *) _p; |
| 6817 | if (value & ASCII_CHAR_MASK) { |
| 6818 | has_error = 1; |
| 6819 | break; |
| 6820 | } |
| 6821 | _p += SIZEOF_LONG; |
| 6822 | } |
| 6823 | if (_p == end) |
| 6824 | break; |
| 6825 | if (has_error) |
| 6826 | break; |
| 6827 | p = _p; |
| 6828 | } |
| 6829 | if (*p & 0x80) { |
| 6830 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6831 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6832 | } |
| 6833 | else { |
| 6834 | ++p; |
| 6835 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6836 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6837 | if (!has_error) |
| 6838 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6839 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6840 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6842 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6844 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6845 | kind = PyUnicode_KIND(v); |
| 6846 | data = PyUnicode_DATA(v); |
| 6847 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6848 | e = s + size; |
| 6849 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6850 | register unsigned char c = (unsigned char)*s; |
| 6851 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6852 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 | ++s; |
| 6854 | } |
| 6855 | else { |
| 6856 | startinpos = s-starts; |
| 6857 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6858 | if (unicode_decode_call_errorhandler( |
| 6859 | errors, &errorHandler, |
| 6860 | "ascii", "ordinal not in range(128)", |
| 6861 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6862 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6863 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6864 | kind = PyUnicode_KIND(v); |
| 6865 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6866 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6868 | if (unicode_resize(&v, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6869 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6870 | Py_XDECREF(errorHandler); |
| 6871 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6872 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6873 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6874 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6875 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6877 | Py_XDECREF(errorHandler); |
| 6878 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | return NULL; |
| 6880 | } |
| 6881 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6882 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6883 | PyObject * |
| 6884 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6885 | Py_ssize_t size, |
| 6886 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6888 | PyObject *result; |
| 6889 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6890 | if (unicode == NULL) |
| 6891 | return NULL; |
| 6892 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6893 | Py_DECREF(unicode); |
| 6894 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6897 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6898 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6899 | { |
| 6900 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | PyErr_BadArgument(); |
| 6902 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6904 | if (PyUnicode_READY(unicode) == -1) |
| 6905 | return NULL; |
| 6906 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6907 | directly. Else defer to above function to raise the exception. */ |
| 6908 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6909 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6910 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6911 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6912 | } |
| 6913 | |
| 6914 | PyObject * |
| 6915 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6916 | { |
| 6917 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6920 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6921 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6922 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6923 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6924 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6925 | #define NEED_RETRY |
| 6926 | #endif |
| 6927 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | #ifndef WC_ERR_INVALID_CHARS |
| 6929 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6930 | #endif |
| 6931 | |
| 6932 | static char* |
| 6933 | code_page_name(UINT code_page, PyObject **obj) |
| 6934 | { |
| 6935 | *obj = NULL; |
| 6936 | if (code_page == CP_ACP) |
| 6937 | return "mbcs"; |
| 6938 | if (code_page == CP_UTF7) |
| 6939 | return "CP_UTF7"; |
| 6940 | if (code_page == CP_UTF8) |
| 6941 | return "CP_UTF8"; |
| 6942 | |
| 6943 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6944 | if (*obj == NULL) |
| 6945 | return NULL; |
| 6946 | return PyBytes_AS_STRING(*obj); |
| 6947 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6949 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6950 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 | { |
| 6952 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6953 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6955 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6956 | return 0; |
| 6957 | |
| 6958 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6959 | if (prev == curr) |
| 6960 | return 1; |
| 6961 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6962 | as it assumes an incomplete character consists of a single |
| 6963 | byte. */ |
| 6964 | if (curr - prev == 2) |
| 6965 | return 1; |
| 6966 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6967 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6968 | return 0; |
| 6969 | } |
| 6970 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | static DWORD |
| 6972 | decode_code_page_flags(UINT code_page) |
| 6973 | { |
| 6974 | if (code_page == CP_UTF7) { |
| 6975 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6976 | return 0; |
| 6977 | } |
| 6978 | else |
| 6979 | return MB_ERR_INVALID_CHARS; |
| 6980 | } |
| 6981 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6982 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6983 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6984 | * mode. |
| 6985 | * |
| 6986 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6987 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6988 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6989 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6990 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6991 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6992 | const char *in, |
| 6993 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6994 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6995 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6996 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6997 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6998 | |
| 6999 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | assert(insize > 0); |
| 7001 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7002 | if (outsize <= 0) |
| 7003 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7004 | |
| 7005 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7006 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7007 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7008 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7009 | if (*v == NULL) |
| 7010 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7011 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7012 | } |
| 7013 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7014 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7015 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7016 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7018 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7019 | } |
| 7020 | |
| 7021 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7022 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7023 | if (outsize <= 0) |
| 7024 | goto error; |
| 7025 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7026 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7027 | error: |
| 7028 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7029 | return -2; |
| 7030 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7031 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7034 | /* |
| 7035 | * Decode a byte string from a code page into unicode object with an error |
| 7036 | * handler. |
| 7037 | * |
| 7038 | * Returns consumed size if succeed, or raise a WindowsError or |
| 7039 | * UnicodeDecodeError exception and returns -1 on error. |
| 7040 | */ |
| 7041 | static int |
| 7042 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7043 | PyObject **v, |
| 7044 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7045 | const char *errors) |
| 7046 | { |
| 7047 | const char *startin = in; |
| 7048 | const char *endin = in + size; |
| 7049 | const DWORD flags = decode_code_page_flags(code_page); |
| 7050 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7051 | 2000 English version of the message. */ |
| 7052 | const char *reason = "No mapping for the Unicode character exists " |
| 7053 | "in the target code page."; |
| 7054 | /* each step cannot decode more than 1 character, but a character can be |
| 7055 | represented as a surrogate pair */ |
| 7056 | wchar_t buffer[2], *startout, *out; |
| 7057 | int insize, outsize; |
| 7058 | PyObject *errorHandler = NULL; |
| 7059 | PyObject *exc = NULL; |
| 7060 | PyObject *encoding_obj = NULL; |
| 7061 | char *encoding; |
| 7062 | DWORD err; |
| 7063 | int ret = -1; |
| 7064 | |
| 7065 | assert(size > 0); |
| 7066 | |
| 7067 | encoding = code_page_name(code_page, &encoding_obj); |
| 7068 | if (encoding == NULL) |
| 7069 | return -1; |
| 7070 | |
| 7071 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7072 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7073 | UnicodeDecodeError. */ |
| 7074 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7075 | if (exc != NULL) { |
| 7076 | PyCodec_StrictErrors(exc); |
| 7077 | Py_CLEAR(exc); |
| 7078 | } |
| 7079 | goto error; |
| 7080 | } |
| 7081 | |
| 7082 | if (*v == NULL) { |
| 7083 | /* Create unicode object */ |
| 7084 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7085 | PyErr_NoMemory(); |
| 7086 | goto error; |
| 7087 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7088 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7089 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7090 | if (*v == NULL) |
| 7091 | goto error; |
| 7092 | startout = PyUnicode_AS_UNICODE(*v); |
| 7093 | } |
| 7094 | else { |
| 7095 | /* Extend unicode object */ |
| 7096 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7097 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7098 | PyErr_NoMemory(); |
| 7099 | goto error; |
| 7100 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7101 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7102 | goto error; |
| 7103 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7104 | } |
| 7105 | |
| 7106 | /* Decode the byte string character per character */ |
| 7107 | out = startout; |
| 7108 | while (in < endin) |
| 7109 | { |
| 7110 | /* Decode a character */ |
| 7111 | insize = 1; |
| 7112 | do |
| 7113 | { |
| 7114 | outsize = MultiByteToWideChar(code_page, flags, |
| 7115 | in, insize, |
| 7116 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7117 | if (outsize > 0) |
| 7118 | break; |
| 7119 | err = GetLastError(); |
| 7120 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7121 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7122 | { |
| 7123 | PyErr_SetFromWindowsErr(0); |
| 7124 | goto error; |
| 7125 | } |
| 7126 | insize++; |
| 7127 | } |
| 7128 | /* 4=maximum length of a UTF-8 sequence */ |
| 7129 | while (insize <= 4 && (in + insize) <= endin); |
| 7130 | |
| 7131 | if (outsize <= 0) { |
| 7132 | Py_ssize_t startinpos, endinpos, outpos; |
| 7133 | |
| 7134 | startinpos = in - startin; |
| 7135 | endinpos = startinpos + 1; |
| 7136 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7137 | if (unicode_decode_call_errorhandler( |
| 7138 | errors, &errorHandler, |
| 7139 | encoding, reason, |
| 7140 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7141 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7142 | { |
| 7143 | goto error; |
| 7144 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7145 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7146 | } |
| 7147 | else { |
| 7148 | in += insize; |
| 7149 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7150 | out += outsize; |
| 7151 | } |
| 7152 | } |
| 7153 | |
| 7154 | /* write a NUL character at the end */ |
| 7155 | *out = 0; |
| 7156 | |
| 7157 | /* Extend unicode object */ |
| 7158 | outsize = out - startout; |
| 7159 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7160 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7161 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7162 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | |
| 7164 | error: |
| 7165 | Py_XDECREF(encoding_obj); |
| 7166 | Py_XDECREF(errorHandler); |
| 7167 | Py_XDECREF(exc); |
| 7168 | return ret; |
| 7169 | } |
| 7170 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7171 | static PyObject * |
| 7172 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7173 | const char *s, Py_ssize_t size, |
| 7174 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7175 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7176 | PyObject *v = NULL; |
| 7177 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7178 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7179 | if (code_page < 0) { |
| 7180 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7181 | return NULL; |
| 7182 | } |
| 7183 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7184 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7185 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7186 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7187 | do |
| 7188 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7189 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7190 | if (size > INT_MAX) { |
| 7191 | chunk_size = INT_MAX; |
| 7192 | final = 0; |
| 7193 | done = 0; |
| 7194 | } |
| 7195 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7196 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7197 | { |
| 7198 | chunk_size = (int)size; |
| 7199 | final = (consumed == NULL); |
| 7200 | done = 1; |
| 7201 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7202 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7203 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7204 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7205 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7206 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7207 | if (chunk_size == 0 && done) { |
| 7208 | if (v != NULL) |
| 7209 | break; |
| 7210 | Py_INCREF(unicode_empty); |
| 7211 | return unicode_empty; |
| 7212 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7213 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7214 | |
| 7215 | converted = decode_code_page_strict(code_page, &v, |
| 7216 | s, chunk_size); |
| 7217 | if (converted == -2) |
| 7218 | converted = decode_code_page_errors(code_page, &v, |
| 7219 | s, chunk_size, |
| 7220 | errors); |
| 7221 | assert(converted != 0); |
| 7222 | |
| 7223 | if (converted < 0) { |
| 7224 | Py_XDECREF(v); |
| 7225 | return NULL; |
| 7226 | } |
| 7227 | |
| 7228 | if (consumed) |
| 7229 | *consumed += converted; |
| 7230 | |
| 7231 | s += converted; |
| 7232 | size -= converted; |
| 7233 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7235 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7238 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7239 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7240 | const char *s, |
| 7241 | Py_ssize_t size, |
| 7242 | const char *errors, |
| 7243 | Py_ssize_t *consumed) |
| 7244 | { |
| 7245 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7246 | } |
| 7247 | |
| 7248 | PyObject * |
| 7249 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7250 | Py_ssize_t size, |
| 7251 | const char *errors, |
| 7252 | Py_ssize_t *consumed) |
| 7253 | { |
| 7254 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7255 | } |
| 7256 | |
| 7257 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7258 | PyUnicode_DecodeMBCS(const char *s, |
| 7259 | Py_ssize_t size, |
| 7260 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7261 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7262 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7263 | } |
| 7264 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7265 | static DWORD |
| 7266 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7267 | { |
| 7268 | if (code_page == CP_UTF8) { |
| 7269 | if (winver.dwMajorVersion >= 6) |
| 7270 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7271 | and later */ |
| 7272 | return WC_ERR_INVALID_CHARS; |
| 7273 | else |
| 7274 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7275 | return 0; |
| 7276 | } |
| 7277 | else if (code_page == CP_UTF7) { |
| 7278 | /* CP_UTF7 only supports flags=0 */ |
| 7279 | return 0; |
| 7280 | } |
| 7281 | else { |
| 7282 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7283 | return 0; |
| 7284 | else |
| 7285 | return WC_NO_BEST_FIT_CHARS; |
| 7286 | } |
| 7287 | } |
| 7288 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7289 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7290 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7291 | * mode. |
| 7292 | * |
| 7293 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7294 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7295 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7296 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7297 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7298 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7300 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7301 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7303 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7304 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7305 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7306 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7307 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7308 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7309 | /* Create a substring so that we can get the UTF-16 representation |
| 7310 | of just the slice under consideration. */ |
| 7311 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7312 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7313 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7314 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7315 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7316 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7317 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7318 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7319 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7320 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7321 | if (substring == NULL) |
| 7322 | return -1; |
| 7323 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7324 | if (p == NULL) { |
| 7325 | Py_DECREF(substring); |
| 7326 | return -1; |
| 7327 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7328 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7329 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7330 | outsize = WideCharToMultiByte(code_page, flags, |
| 7331 | p, size, |
| 7332 | NULL, 0, |
| 7333 | NULL, pusedDefaultChar); |
| 7334 | if (outsize <= 0) |
| 7335 | goto error; |
| 7336 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7337 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7338 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7339 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7340 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7341 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7342 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7343 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7344 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7345 | if (*outbytes == NULL) { |
| 7346 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7347 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7348 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7349 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7350 | } |
| 7351 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7352 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7353 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7354 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7355 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7356 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7357 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7358 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7359 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7360 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7361 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7362 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7363 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7367 | outsize = WideCharToMultiByte(code_page, flags, |
| 7368 | p, size, |
| 7369 | out, outsize, |
| 7370 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7371 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7372 | if (outsize <= 0) |
| 7373 | goto error; |
| 7374 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7375 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7376 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7377 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7379 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7381 | return -2; |
| 7382 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7383 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7386 | /* |
| 7387 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7388 | * error handler. |
| 7389 | * |
| 7390 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7391 | * -1 on other error. |
| 7392 | */ |
| 7393 | static int |
| 7394 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7395 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7396 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7397 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7398 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7399 | Py_ssize_t pos = unicode_offset; |
| 7400 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7401 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7402 | 2000 English version of the message. */ |
| 7403 | const char *reason = "invalid character"; |
| 7404 | /* 4=maximum length of a UTF-8 sequence */ |
| 7405 | char buffer[4]; |
| 7406 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7407 | Py_ssize_t outsize; |
| 7408 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7409 | PyObject *errorHandler = NULL; |
| 7410 | PyObject *exc = NULL; |
| 7411 | PyObject *encoding_obj = NULL; |
| 7412 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7413 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7414 | PyObject *rep; |
| 7415 | int ret = -1; |
| 7416 | |
| 7417 | assert(insize > 0); |
| 7418 | |
| 7419 | encoding = code_page_name(code_page, &encoding_obj); |
| 7420 | if (encoding == NULL) |
| 7421 | return -1; |
| 7422 | |
| 7423 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7424 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7425 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7426 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7427 | if (exc != NULL) { |
| 7428 | PyCodec_StrictErrors(exc); |
| 7429 | Py_DECREF(exc); |
| 7430 | } |
| 7431 | Py_XDECREF(encoding_obj); |
| 7432 | return -1; |
| 7433 | } |
| 7434 | |
| 7435 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7436 | pusedDefaultChar = &usedDefaultChar; |
| 7437 | else |
| 7438 | pusedDefaultChar = NULL; |
| 7439 | |
| 7440 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7441 | PyErr_NoMemory(); |
| 7442 | goto error; |
| 7443 | } |
| 7444 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7445 | |
| 7446 | if (*outbytes == NULL) { |
| 7447 | /* Create string object */ |
| 7448 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7449 | if (*outbytes == NULL) |
| 7450 | goto error; |
| 7451 | out = PyBytes_AS_STRING(*outbytes); |
| 7452 | } |
| 7453 | else { |
| 7454 | /* Extend string object */ |
| 7455 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7456 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7457 | PyErr_NoMemory(); |
| 7458 | goto error; |
| 7459 | } |
| 7460 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7461 | goto error; |
| 7462 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7463 | } |
| 7464 | |
| 7465 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7466 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7467 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7468 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7469 | wchar_t chars[2]; |
| 7470 | int charsize; |
| 7471 | if (ch < 0x10000) { |
| 7472 | chars[0] = (wchar_t)ch; |
| 7473 | charsize = 1; |
| 7474 | } |
| 7475 | else { |
| 7476 | ch -= 0x10000; |
| 7477 | chars[0] = 0xd800 + (ch >> 10); |
| 7478 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7479 | charsize = 2; |
| 7480 | } |
| 7481 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7482 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7483 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7484 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7485 | NULL, pusedDefaultChar); |
| 7486 | if (outsize > 0) { |
| 7487 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7488 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7489 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7490 | memcpy(out, buffer, outsize); |
| 7491 | out += outsize; |
| 7492 | continue; |
| 7493 | } |
| 7494 | } |
| 7495 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7496 | PyErr_SetFromWindowsErr(0); |
| 7497 | goto error; |
| 7498 | } |
| 7499 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7500 | rep = unicode_encode_call_errorhandler( |
| 7501 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7502 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7503 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7504 | if (rep == NULL) |
| 7505 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7506 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7507 | |
| 7508 | if (PyBytes_Check(rep)) { |
| 7509 | outsize = PyBytes_GET_SIZE(rep); |
| 7510 | if (outsize != 1) { |
| 7511 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7512 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7513 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7514 | Py_DECREF(rep); |
| 7515 | goto error; |
| 7516 | } |
| 7517 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7518 | } |
| 7519 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7520 | out += outsize; |
| 7521 | } |
| 7522 | else { |
| 7523 | Py_ssize_t i; |
| 7524 | enum PyUnicode_Kind kind; |
| 7525 | void *data; |
| 7526 | |
| 7527 | if (PyUnicode_READY(rep) < 0) { |
| 7528 | Py_DECREF(rep); |
| 7529 | goto error; |
| 7530 | } |
| 7531 | |
| 7532 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7533 | if (outsize != 1) { |
| 7534 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7535 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7536 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7537 | Py_DECREF(rep); |
| 7538 | goto error; |
| 7539 | } |
| 7540 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7541 | } |
| 7542 | kind = PyUnicode_KIND(rep); |
| 7543 | data = PyUnicode_DATA(rep); |
| 7544 | for (i=0; i < outsize; i++) { |
| 7545 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7546 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7547 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7548 | encoding, unicode, |
| 7549 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7550 | "unable to encode error handler result to ASCII"); |
| 7551 | Py_DECREF(rep); |
| 7552 | goto error; |
| 7553 | } |
| 7554 | *out = (unsigned char)ch; |
| 7555 | out++; |
| 7556 | } |
| 7557 | } |
| 7558 | Py_DECREF(rep); |
| 7559 | } |
| 7560 | /* write a NUL byte */ |
| 7561 | *out = 0; |
| 7562 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7563 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7564 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7565 | goto error; |
| 7566 | ret = 0; |
| 7567 | |
| 7568 | error: |
| 7569 | Py_XDECREF(encoding_obj); |
| 7570 | Py_XDECREF(errorHandler); |
| 7571 | Py_XDECREF(exc); |
| 7572 | return ret; |
| 7573 | } |
| 7574 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7575 | static PyObject * |
| 7576 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7577 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7578 | const char *errors) |
| 7579 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7580 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7581 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7582 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7583 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7584 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7585 | if (PyUnicode_READY(unicode) < 0) |
| 7586 | return NULL; |
| 7587 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7588 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7589 | if (code_page < 0) { |
| 7590 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7591 | return NULL; |
| 7592 | } |
| 7593 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7594 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7595 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7596 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7597 | offset = 0; |
| 7598 | do |
| 7599 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7600 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7601 | /* 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] | 7602 | chunks. */ |
| 7603 | if (len > INT_MAX/2) { |
| 7604 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7605 | done = 0; |
| 7606 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7607 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7608 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7609 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7610 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7611 | done = 1; |
| 7612 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7613 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7614 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7615 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7616 | errors); |
| 7617 | if (ret == -2) |
| 7618 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7619 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7620 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7621 | if (ret < 0) { |
| 7622 | Py_XDECREF(outbytes); |
| 7623 | return NULL; |
| 7624 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7625 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7626 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7627 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7628 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7629 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7630 | return outbytes; |
| 7631 | } |
| 7632 | |
| 7633 | PyObject * |
| 7634 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7635 | Py_ssize_t size, |
| 7636 | const char *errors) |
| 7637 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7638 | PyObject *unicode, *res; |
| 7639 | unicode = PyUnicode_FromUnicode(p, size); |
| 7640 | if (unicode == NULL) |
| 7641 | return NULL; |
| 7642 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7643 | Py_DECREF(unicode); |
| 7644 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | PyObject * |
| 7648 | PyUnicode_EncodeCodePage(int code_page, |
| 7649 | PyObject *unicode, |
| 7650 | const char *errors) |
| 7651 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7652 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7653 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7654 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7655 | PyObject * |
| 7656 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7657 | { |
| 7658 | if (!PyUnicode_Check(unicode)) { |
| 7659 | PyErr_BadArgument(); |
| 7660 | return NULL; |
| 7661 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7662 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7663 | } |
| 7664 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7665 | #undef NEED_RETRY |
| 7666 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7667 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7669 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7670 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7671 | PyObject * |
| 7672 | PyUnicode_DecodeCharmap(const char *s, |
| 7673 | Py_ssize_t size, |
| 7674 | PyObject *mapping, |
| 7675 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7676 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7677 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7678 | Py_ssize_t startinpos; |
| 7679 | Py_ssize_t endinpos; |
| 7680 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7681 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7682 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7683 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7684 | PyObject *errorHandler = NULL; |
| 7685 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7687 | /* Default to Latin-1 */ |
| 7688 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7689 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7691 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7692 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7693 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7694 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7695 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7696 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7697 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7698 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7699 | Py_ssize_t maplen; |
| 7700 | enum PyUnicode_Kind kind; |
| 7701 | void *data; |
| 7702 | Py_UCS4 x; |
| 7703 | |
| 7704 | if (PyUnicode_READY(mapping) < 0) |
| 7705 | return NULL; |
| 7706 | |
| 7707 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7708 | data = PyUnicode_DATA(mapping); |
| 7709 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7710 | while (s < e) { |
| 7711 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7713 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7714 | x = PyUnicode_READ(kind, data, ch); |
| 7715 | else |
| 7716 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7718 | if (x == 0xfffe) |
| 7719 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7720 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | startinpos = s-starts; |
| 7722 | endinpos = startinpos+1; |
| 7723 | if (unicode_decode_call_errorhandler( |
| 7724 | errors, &errorHandler, |
| 7725 | "charmap", "character maps to <undefined>", |
| 7726 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7727 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7728 | goto onError; |
| 7729 | } |
| 7730 | continue; |
| 7731 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7732 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7733 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7734 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7735 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7736 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7737 | } |
| 7738 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | while (s < e) { |
| 7740 | unsigned char ch = *s; |
| 7741 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7743 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7744 | w = PyLong_FromLong((long)ch); |
| 7745 | if (w == NULL) |
| 7746 | goto onError; |
| 7747 | x = PyObject_GetItem(mapping, w); |
| 7748 | Py_DECREF(w); |
| 7749 | if (x == NULL) { |
| 7750 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7751 | /* No mapping found means: mapping is undefined. */ |
| 7752 | PyErr_Clear(); |
| 7753 | x = Py_None; |
| 7754 | Py_INCREF(x); |
| 7755 | } else |
| 7756 | goto onError; |
| 7757 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7758 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | /* Apply mapping */ |
| 7760 | if (PyLong_Check(x)) { |
| 7761 | long value = PyLong_AS_LONG(x); |
| 7762 | if (value < 0 || value > 65535) { |
| 7763 | PyErr_SetString(PyExc_TypeError, |
| 7764 | "character mapping must be in range(65536)"); |
| 7765 | Py_DECREF(x); |
| 7766 | goto onError; |
| 7767 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7768 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7769 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | } |
| 7771 | else if (x == Py_None) { |
| 7772 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | startinpos = s-starts; |
| 7774 | endinpos = startinpos+1; |
| 7775 | if (unicode_decode_call_errorhandler( |
| 7776 | errors, &errorHandler, |
| 7777 | "charmap", "character maps to <undefined>", |
| 7778 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7779 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7780 | Py_DECREF(x); |
| 7781 | goto onError; |
| 7782 | } |
| 7783 | Py_DECREF(x); |
| 7784 | continue; |
| 7785 | } |
| 7786 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7787 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7788 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7789 | if (PyUnicode_READY(x) < 0) |
| 7790 | goto onError; |
| 7791 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7792 | |
| 7793 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7795 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7796 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7797 | goto onError; |
| 7798 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7799 | else if (targetsize > 1) { |
| 7800 | /* 1-n mapping */ |
| 7801 | if (targetsize > extrachars) { |
| 7802 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7803 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7804 | (targetsize << 2); |
| 7805 | extrachars += needed; |
| 7806 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7807 | if (unicode_resize(&v, |
| 7808 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7809 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7810 | Py_DECREF(x); |
| 7811 | goto onError; |
| 7812 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7813 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7814 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7815 | goto onError; |
| 7816 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7817 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7818 | extrachars -= targetsize; |
| 7819 | } |
| 7820 | /* 1-0 mapping: skip the character */ |
| 7821 | } |
| 7822 | else { |
| 7823 | /* wrong return value */ |
| 7824 | PyErr_SetString(PyExc_TypeError, |
| 7825 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7826 | Py_DECREF(x); |
| 7827 | goto onError; |
| 7828 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | Py_DECREF(x); |
| 7830 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7831 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7833 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7834 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7835 | Py_XDECREF(errorHandler); |
| 7836 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7837 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7838 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7839 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7840 | Py_XDECREF(errorHandler); |
| 7841 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | Py_XDECREF(v); |
| 7843 | return NULL; |
| 7844 | } |
| 7845 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7846 | /* Charmap encoding: the lookup table */ |
| 7847 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7848 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7849 | PyObject_HEAD |
| 7850 | unsigned char level1[32]; |
| 7851 | int count2, count3; |
| 7852 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7853 | }; |
| 7854 | |
| 7855 | static PyObject* |
| 7856 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7857 | { |
| 7858 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7859 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7860 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7861 | } |
| 7862 | |
| 7863 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7864 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7866 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7867 | }; |
| 7868 | |
| 7869 | static void |
| 7870 | encoding_map_dealloc(PyObject* o) |
| 7871 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7872 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7873 | } |
| 7874 | |
| 7875 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7876 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7877 | "EncodingMap", /*tp_name*/ |
| 7878 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7879 | 0, /*tp_itemsize*/ |
| 7880 | /* methods */ |
| 7881 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7882 | 0, /*tp_print*/ |
| 7883 | 0, /*tp_getattr*/ |
| 7884 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7885 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | 0, /*tp_repr*/ |
| 7887 | 0, /*tp_as_number*/ |
| 7888 | 0, /*tp_as_sequence*/ |
| 7889 | 0, /*tp_as_mapping*/ |
| 7890 | 0, /*tp_hash*/ |
| 7891 | 0, /*tp_call*/ |
| 7892 | 0, /*tp_str*/ |
| 7893 | 0, /*tp_getattro*/ |
| 7894 | 0, /*tp_setattro*/ |
| 7895 | 0, /*tp_as_buffer*/ |
| 7896 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7897 | 0, /*tp_doc*/ |
| 7898 | 0, /*tp_traverse*/ |
| 7899 | 0, /*tp_clear*/ |
| 7900 | 0, /*tp_richcompare*/ |
| 7901 | 0, /*tp_weaklistoffset*/ |
| 7902 | 0, /*tp_iter*/ |
| 7903 | 0, /*tp_iternext*/ |
| 7904 | encoding_map_methods, /*tp_methods*/ |
| 7905 | 0, /*tp_members*/ |
| 7906 | 0, /*tp_getset*/ |
| 7907 | 0, /*tp_base*/ |
| 7908 | 0, /*tp_dict*/ |
| 7909 | 0, /*tp_descr_get*/ |
| 7910 | 0, /*tp_descr_set*/ |
| 7911 | 0, /*tp_dictoffset*/ |
| 7912 | 0, /*tp_init*/ |
| 7913 | 0, /*tp_alloc*/ |
| 7914 | 0, /*tp_new*/ |
| 7915 | 0, /*tp_free*/ |
| 7916 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7917 | }; |
| 7918 | |
| 7919 | PyObject* |
| 7920 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7921 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7922 | PyObject *result; |
| 7923 | struct encoding_map *mresult; |
| 7924 | int i; |
| 7925 | int need_dict = 0; |
| 7926 | unsigned char level1[32]; |
| 7927 | unsigned char level2[512]; |
| 7928 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7929 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7930 | int kind; |
| 7931 | void *data; |
| 7932 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7934 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7935 | PyErr_BadArgument(); |
| 7936 | return NULL; |
| 7937 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7938 | kind = PyUnicode_KIND(string); |
| 7939 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7940 | memset(level1, 0xFF, sizeof level1); |
| 7941 | memset(level2, 0xFF, sizeof level2); |
| 7942 | |
| 7943 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7944 | or if there are non-BMP characters, we need to use |
| 7945 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7946 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7947 | need_dict = 1; |
| 7948 | for (i = 1; i < 256; i++) { |
| 7949 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7950 | ch = PyUnicode_READ(kind, data, i); |
| 7951 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7952 | need_dict = 1; |
| 7953 | break; |
| 7954 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7955 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7956 | /* unmapped character */ |
| 7957 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7958 | l1 = ch >> 11; |
| 7959 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7960 | if (level1[l1] == 0xFF) |
| 7961 | level1[l1] = count2++; |
| 7962 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7963 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7964 | } |
| 7965 | |
| 7966 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7967 | need_dict = 1; |
| 7968 | |
| 7969 | if (need_dict) { |
| 7970 | PyObject *result = PyDict_New(); |
| 7971 | PyObject *key, *value; |
| 7972 | if (!result) |
| 7973 | return NULL; |
| 7974 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7975 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7976 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7977 | if (!key || !value) |
| 7978 | goto failed1; |
| 7979 | if (PyDict_SetItem(result, key, value) == -1) |
| 7980 | goto failed1; |
| 7981 | Py_DECREF(key); |
| 7982 | Py_DECREF(value); |
| 7983 | } |
| 7984 | return result; |
| 7985 | failed1: |
| 7986 | Py_XDECREF(key); |
| 7987 | Py_XDECREF(value); |
| 7988 | Py_DECREF(result); |
| 7989 | return NULL; |
| 7990 | } |
| 7991 | |
| 7992 | /* Create a three-level trie */ |
| 7993 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7994 | 16*count2 + 128*count3 - 1); |
| 7995 | if (!result) |
| 7996 | return PyErr_NoMemory(); |
| 7997 | PyObject_Init(result, &EncodingMapType); |
| 7998 | mresult = (struct encoding_map*)result; |
| 7999 | mresult->count2 = count2; |
| 8000 | mresult->count3 = count3; |
| 8001 | mlevel1 = mresult->level1; |
| 8002 | mlevel2 = mresult->level23; |
| 8003 | mlevel3 = mresult->level23 + 16*count2; |
| 8004 | memcpy(mlevel1, level1, 32); |
| 8005 | memset(mlevel2, 0xFF, 16*count2); |
| 8006 | memset(mlevel3, 0, 128*count3); |
| 8007 | count3 = 0; |
| 8008 | for (i = 1; i < 256; i++) { |
| 8009 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8010 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8011 | /* unmapped character */ |
| 8012 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8013 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 8014 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8015 | i2 = 16*mlevel1[o1] + o2; |
| 8016 | if (mlevel2[i2] == 0xFF) |
| 8017 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8018 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8019 | i3 = 128*mlevel2[i2] + o3; |
| 8020 | mlevel3[i3] = i; |
| 8021 | } |
| 8022 | return result; |
| 8023 | } |
| 8024 | |
| 8025 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8026 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8027 | { |
| 8028 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8029 | int l1 = c>>11; |
| 8030 | int l2 = (c>>7) & 0xF; |
| 8031 | int l3 = c & 0x7F; |
| 8032 | int i; |
| 8033 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8034 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8036 | if (c == 0) |
| 8037 | return 0; |
| 8038 | /* level 1*/ |
| 8039 | i = map->level1[l1]; |
| 8040 | if (i == 0xFF) { |
| 8041 | return -1; |
| 8042 | } |
| 8043 | /* level 2*/ |
| 8044 | i = map->level23[16*i+l2]; |
| 8045 | if (i == 0xFF) { |
| 8046 | return -1; |
| 8047 | } |
| 8048 | /* level 3 */ |
| 8049 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8050 | if (i == 0) { |
| 8051 | return -1; |
| 8052 | } |
| 8053 | return i; |
| 8054 | } |
| 8055 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8056 | /* Lookup the character ch in the mapping. If the character |
| 8057 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8058 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8059 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8060 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8062 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8063 | PyObject *x; |
| 8064 | |
| 8065 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8067 | x = PyObject_GetItem(mapping, w); |
| 8068 | Py_DECREF(w); |
| 8069 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8070 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8071 | /* No mapping found means: mapping is undefined. */ |
| 8072 | PyErr_Clear(); |
| 8073 | x = Py_None; |
| 8074 | Py_INCREF(x); |
| 8075 | return x; |
| 8076 | } else |
| 8077 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8078 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8079 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8080 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8081 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8082 | long value = PyLong_AS_LONG(x); |
| 8083 | if (value < 0 || value > 255) { |
| 8084 | PyErr_SetString(PyExc_TypeError, |
| 8085 | "character mapping must be in range(256)"); |
| 8086 | Py_DECREF(x); |
| 8087 | return NULL; |
| 8088 | } |
| 8089 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8091 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8093 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8094 | /* wrong return value */ |
| 8095 | PyErr_Format(PyExc_TypeError, |
| 8096 | "character mapping must return integer, bytes or None, not %.400s", |
| 8097 | x->ob_type->tp_name); |
| 8098 | Py_DECREF(x); |
| 8099 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8100 | } |
| 8101 | } |
| 8102 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8103 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8104 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8105 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8106 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8107 | /* exponentially overallocate to minimize reallocations */ |
| 8108 | if (requiredsize < 2*outsize) |
| 8109 | requiredsize = 2*outsize; |
| 8110 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8111 | return -1; |
| 8112 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8115 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8116 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8117 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | /* 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] | 8119 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | space is available. Return a new reference to the object that |
| 8121 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8122 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8123 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8124 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8125 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8126 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8127 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8128 | PyObject *rep; |
| 8129 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8130 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8131 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8132 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8133 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8135 | if (res == -1) |
| 8136 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | if (outsize<requiredsize) |
| 8138 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8139 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8140 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8141 | outstart[(*outpos)++] = (char)res; |
| 8142 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8146 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8148 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8149 | Py_DECREF(rep); |
| 8150 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8151 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 | if (PyLong_Check(rep)) { |
| 8153 | Py_ssize_t requiredsize = *outpos+1; |
| 8154 | if (outsize<requiredsize) |
| 8155 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8156 | Py_DECREF(rep); |
| 8157 | return enc_EXCEPTION; |
| 8158 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8159 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8161 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | else { |
| 8163 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8164 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8165 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8166 | if (outsize<requiredsize) |
| 8167 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8168 | Py_DECREF(rep); |
| 8169 | return enc_EXCEPTION; |
| 8170 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8171 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8172 | memcpy(outstart + *outpos, repchars, repsize); |
| 8173 | *outpos += repsize; |
| 8174 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8175 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8176 | Py_DECREF(rep); |
| 8177 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
| 8180 | /* handle an error in PyUnicode_EncodeCharmap |
| 8181 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8182 | static int |
| 8183 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8184 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8185 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8186 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8187 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | { |
| 8189 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8190 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8191 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8192 | enum PyUnicode_Kind kind; |
| 8193 | void *data; |
| 8194 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8195 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8196 | Py_ssize_t collstartpos = *inpos; |
| 8197 | Py_ssize_t collendpos = *inpos+1; |
| 8198 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8199 | char *encoding = "charmap"; |
| 8200 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8201 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8202 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8203 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8204 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8205 | if (PyUnicode_READY(unicode) < 0) |
| 8206 | return -1; |
| 8207 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8208 | /* find all unencodable characters */ |
| 8209 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8210 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8211 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8212 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8213 | val = encoding_map_lookup(ch, mapping); |
| 8214 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8215 | break; |
| 8216 | ++collendpos; |
| 8217 | continue; |
| 8218 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8219 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8220 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8221 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8222 | if (rep==NULL) |
| 8223 | return -1; |
| 8224 | else if (rep!=Py_None) { |
| 8225 | Py_DECREF(rep); |
| 8226 | break; |
| 8227 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8228 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8229 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8230 | } |
| 8231 | /* cache callback name lookup |
| 8232 | * (if not done yet, i.e. it's the first error) */ |
| 8233 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8235 | *known_errorHandler = 1; |
| 8236 | else if (!strcmp(errors, "replace")) |
| 8237 | *known_errorHandler = 2; |
| 8238 | else if (!strcmp(errors, "ignore")) |
| 8239 | *known_errorHandler = 3; |
| 8240 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8241 | *known_errorHandler = 4; |
| 8242 | else |
| 8243 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | } |
| 8245 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8246 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8247 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8248 | return -1; |
| 8249 | case 2: /* replace */ |
| 8250 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8251 | x = charmapencode_output('?', mapping, res, respos); |
| 8252 | if (x==enc_EXCEPTION) { |
| 8253 | return -1; |
| 8254 | } |
| 8255 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8256 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | return -1; |
| 8258 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8259 | } |
| 8260 | /* fall through */ |
| 8261 | case 3: /* ignore */ |
| 8262 | *inpos = collendpos; |
| 8263 | break; |
| 8264 | case 4: /* xmlcharrefreplace */ |
| 8265 | /* generate replacement (temporarily (mis)uses p) */ |
| 8266 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | char buffer[2+29+1+1]; |
| 8268 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8269 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8270 | for (cp = buffer; *cp; ++cp) { |
| 8271 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8272 | if (x==enc_EXCEPTION) |
| 8273 | return -1; |
| 8274 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8275 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | return -1; |
| 8277 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8278 | } |
| 8279 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8280 | *inpos = collendpos; |
| 8281 | break; |
| 8282 | default: |
| 8283 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8284 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8286 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8288 | if (PyBytes_Check(repunicode)) { |
| 8289 | /* Directly copy bytes result to output. */ |
| 8290 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8291 | Py_ssize_t requiredsize; |
| 8292 | repsize = PyBytes_Size(repunicode); |
| 8293 | requiredsize = *respos + repsize; |
| 8294 | if (requiredsize > outsize) |
| 8295 | /* Make room for all additional bytes. */ |
| 8296 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8297 | Py_DECREF(repunicode); |
| 8298 | return -1; |
| 8299 | } |
| 8300 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8301 | PyBytes_AsString(repunicode), repsize); |
| 8302 | *respos += repsize; |
| 8303 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8304 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8305 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8306 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8307 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8308 | if (PyUnicode_READY(repunicode) < 0) { |
| 8309 | Py_DECREF(repunicode); |
| 8310 | return -1; |
| 8311 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8312 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8313 | data = PyUnicode_DATA(repunicode); |
| 8314 | kind = PyUnicode_KIND(repunicode); |
| 8315 | for (index = 0; index < repsize; index++) { |
| 8316 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8317 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8318 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8319 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | return -1; |
| 8321 | } |
| 8322 | else if (x==enc_FAILED) { |
| 8323 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8324 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8325 | return -1; |
| 8326 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8327 | } |
| 8328 | *inpos = newpos; |
| 8329 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8330 | } |
| 8331 | return 0; |
| 8332 | } |
| 8333 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8334 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8335 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8336 | PyObject *mapping, |
| 8337 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8339 | /* output object */ |
| 8340 | PyObject *res = NULL; |
| 8341 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8342 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8343 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8344 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8345 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8346 | PyObject *errorHandler = NULL; |
| 8347 | PyObject *exc = NULL; |
| 8348 | /* the following variable is used for caching string comparisons |
| 8349 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8350 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8351 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8352 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8353 | if (PyUnicode_READY(unicode) < 0) |
| 8354 | return NULL; |
| 8355 | size = PyUnicode_GET_LENGTH(unicode); |
| 8356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | /* Default to Latin-1 */ |
| 8358 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8359 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8360 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8361 | /* allocate enough for a simple encoding without |
| 8362 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8363 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8364 | if (res == NULL) |
| 8365 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8366 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8368 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8369 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8370 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8371 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8372 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | if (x==enc_EXCEPTION) /* error */ |
| 8374 | goto onError; |
| 8375 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8376 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8377 | &exc, |
| 8378 | &known_errorHandler, &errorHandler, errors, |
| 8379 | &res, &respos)) { |
| 8380 | goto onError; |
| 8381 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8382 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 | else |
| 8384 | /* done with this character => adjust input position */ |
| 8385 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8388 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8389 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8390 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8391 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8392 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | Py_XDECREF(exc); |
| 8394 | Py_XDECREF(errorHandler); |
| 8395 | return res; |
| 8396 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8398 | Py_XDECREF(res); |
| 8399 | Py_XDECREF(exc); |
| 8400 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | return NULL; |
| 8402 | } |
| 8403 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8404 | /* Deprecated */ |
| 8405 | PyObject * |
| 8406 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8407 | Py_ssize_t size, |
| 8408 | PyObject *mapping, |
| 8409 | const char *errors) |
| 8410 | { |
| 8411 | PyObject *result; |
| 8412 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8413 | if (unicode == NULL) |
| 8414 | return NULL; |
| 8415 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8416 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8417 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8418 | } |
| 8419 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8420 | PyObject * |
| 8421 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8422 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | { |
| 8424 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8425 | PyErr_BadArgument(); |
| 8426 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8428 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | } |
| 8430 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8432 | static void |
| 8433 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8435 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8436 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8439 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8440 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | } |
| 8442 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8444 | goto onError; |
| 8445 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8446 | goto onError; |
| 8447 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8448 | goto onError; |
| 8449 | return; |
| 8450 | onError: |
| 8451 | Py_DECREF(*exceptionObject); |
| 8452 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | } |
| 8454 | } |
| 8455 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8456 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8457 | static void |
| 8458 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8459 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8460 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8461 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8462 | { |
| 8463 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
| 8469 | /* error handling callback helper: |
| 8470 | build arguments, call the callback and check the arguments, |
| 8471 | put the result into newpos and return the replacement string, which |
| 8472 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8473 | static PyObject * |
| 8474 | unicode_translate_call_errorhandler(const char *errors, |
| 8475 | PyObject **errorHandler, |
| 8476 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8477 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8478 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8479 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8480 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8481 | 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] | 8482 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8483 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8484 | PyObject *restuple; |
| 8485 | PyObject *resunicode; |
| 8486 | |
| 8487 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8488 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8489 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8490 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8491 | } |
| 8492 | |
| 8493 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8495 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8496 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8497 | |
| 8498 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8500 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8501 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8502 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8503 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | Py_DECREF(restuple); |
| 8505 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8506 | } |
| 8507 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8508 | &resunicode, &i_newpos)) { |
| 8509 | Py_DECREF(restuple); |
| 8510 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8511 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8512 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8513 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8514 | else |
| 8515 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8518 | Py_DECREF(restuple); |
| 8519 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8520 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8521 | Py_INCREF(resunicode); |
| 8522 | Py_DECREF(restuple); |
| 8523 | return resunicode; |
| 8524 | } |
| 8525 | |
| 8526 | /* Lookup the character ch in the mapping and put the result in result, |
| 8527 | which must be decrefed by the caller. |
| 8528 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8529 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8530 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8531 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8532 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | PyObject *x; |
| 8534 | |
| 8535 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8536 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8537 | x = PyObject_GetItem(mapping, w); |
| 8538 | Py_DECREF(w); |
| 8539 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8541 | /* No mapping found means: use 1:1 mapping. */ |
| 8542 | PyErr_Clear(); |
| 8543 | *result = NULL; |
| 8544 | return 0; |
| 8545 | } else |
| 8546 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8547 | } |
| 8548 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | *result = x; |
| 8550 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8551 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8552 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | long value = PyLong_AS_LONG(x); |
| 8554 | long max = PyUnicode_GetMax(); |
| 8555 | if (value < 0 || value > max) { |
| 8556 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8557 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | Py_DECREF(x); |
| 8559 | return -1; |
| 8560 | } |
| 8561 | *result = x; |
| 8562 | return 0; |
| 8563 | } |
| 8564 | else if (PyUnicode_Check(x)) { |
| 8565 | *result = x; |
| 8566 | return 0; |
| 8567 | } |
| 8568 | else { |
| 8569 | /* wrong return value */ |
| 8570 | PyErr_SetString(PyExc_TypeError, |
| 8571 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8572 | Py_DECREF(x); |
| 8573 | return -1; |
| 8574 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | } |
| 8576 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | if not reallocate and adjust various state variables. |
| 8578 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8579 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8580 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8581 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8584 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8585 | /* exponentially overallocate to minimize reallocations */ |
| 8586 | if (requiredsize < 2 * oldsize) |
| 8587 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8589 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8591 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8592 | } |
| 8593 | return 0; |
| 8594 | } |
| 8595 | /* lookup the character, put the result in the output string and adjust |
| 8596 | various state variables. Return a new reference to the object that |
| 8597 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8598 | undefined (in which case no character was written). |
| 8599 | The called must decref result. |
| 8600 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8601 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8603 | PyObject *mapping, Py_UCS4 **output, |
| 8604 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8605 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8606 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8607 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8608 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8609 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8610 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8612 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8613 | } |
| 8614 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8615 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8616 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | /* 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] | 8618 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8619 | } |
| 8620 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8621 | Py_ssize_t repsize; |
| 8622 | if (PyUnicode_READY(*res) == -1) |
| 8623 | return -1; |
| 8624 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | if (repsize==1) { |
| 8626 | /* 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] | 8627 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8628 | } |
| 8629 | else if (repsize!=0) { |
| 8630 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 | Py_ssize_t requiredsize = *opos + |
| 8632 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8633 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | Py_ssize_t i; |
| 8635 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8636 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8637 | for(i = 0; i < repsize; i++) |
| 8638 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8639 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8640 | } |
| 8641 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8643 | return 0; |
| 8644 | } |
| 8645 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8646 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8648 | PyObject *mapping, |
| 8649 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8651 | /* input object */ |
| 8652 | char *idata; |
| 8653 | Py_ssize_t size, i; |
| 8654 | int kind; |
| 8655 | /* output buffer */ |
| 8656 | Py_UCS4 *output = NULL; |
| 8657 | Py_ssize_t osize; |
| 8658 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8659 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8660 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8661 | char *reason = "character maps to <undefined>"; |
| 8662 | PyObject *errorHandler = NULL; |
| 8663 | PyObject *exc = NULL; |
| 8664 | /* the following variable is used for caching string comparisons |
| 8665 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8666 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8667 | int known_errorHandler = -1; |
| 8668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8670 | PyErr_BadArgument(); |
| 8671 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8672 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8673 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | if (PyUnicode_READY(input) == -1) |
| 8675 | return NULL; |
| 8676 | idata = (char*)PyUnicode_DATA(input); |
| 8677 | kind = PyUnicode_KIND(input); |
| 8678 | size = PyUnicode_GET_LENGTH(input); |
| 8679 | i = 0; |
| 8680 | |
| 8681 | if (size == 0) { |
| 8682 | Py_INCREF(input); |
| 8683 | return input; |
| 8684 | } |
| 8685 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8686 | /* allocate enough for a simple 1:1 translation without |
| 8687 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8688 | osize = size; |
| 8689 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8690 | opos = 0; |
| 8691 | if (output == NULL) { |
| 8692 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8696 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | /* try to encode it */ |
| 8698 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 | if (charmaptranslate_output(input, i, mapping, |
| 8700 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | Py_XDECREF(x); |
| 8702 | goto onError; |
| 8703 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8704 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8705 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8706 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8707 | else { /* untranslatable character */ |
| 8708 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8709 | Py_ssize_t repsize; |
| 8710 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8711 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8712 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | Py_ssize_t collstart = i; |
| 8714 | Py_ssize_t collend = i+1; |
| 8715 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | while (collend < size) { |
| 8719 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8720 | goto onError; |
| 8721 | Py_XDECREF(x); |
| 8722 | if (x!=Py_None) |
| 8723 | break; |
| 8724 | ++collend; |
| 8725 | } |
| 8726 | /* cache callback name lookup |
| 8727 | * (if not done yet, i.e. it's the first error) */ |
| 8728 | if (known_errorHandler==-1) { |
| 8729 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8730 | known_errorHandler = 1; |
| 8731 | else if (!strcmp(errors, "replace")) |
| 8732 | known_errorHandler = 2; |
| 8733 | else if (!strcmp(errors, "ignore")) |
| 8734 | known_errorHandler = 3; |
| 8735 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8736 | known_errorHandler = 4; |
| 8737 | else |
| 8738 | known_errorHandler = 0; |
| 8739 | } |
| 8740 | switch (known_errorHandler) { |
| 8741 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8742 | raise_translate_exception(&exc, input, collstart, |
| 8743 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8744 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8745 | case 2: /* replace */ |
| 8746 | /* 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] | 8747 | for (coll = collstart; coll<collend; coll++) |
| 8748 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | /* fall through */ |
| 8750 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8752 | break; |
| 8753 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | /* generate replacement (temporarily (mis)uses i) */ |
| 8755 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8756 | char buffer[2+29+1+1]; |
| 8757 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8758 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8759 | if (charmaptranslate_makespace(&output, &osize, |
| 8760 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8761 | goto onError; |
| 8762 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8763 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8764 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8766 | break; |
| 8767 | default: |
| 8768 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8769 | reason, input, &exc, |
| 8770 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8771 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8772 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8773 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8774 | Py_DECREF(repunicode); |
| 8775 | goto onError; |
| 8776 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8777 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8778 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8779 | if (charmaptranslate_makespace(&output, &osize, |
| 8780 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8781 | Py_DECREF(repunicode); |
| 8782 | goto onError; |
| 8783 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8785 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8786 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8787 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8788 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8789 | } |
| 8790 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8792 | if (!res) |
| 8793 | goto onError; |
| 8794 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8795 | Py_XDECREF(exc); |
| 8796 | Py_XDECREF(errorHandler); |
| 8797 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8799 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8801 | Py_XDECREF(exc); |
| 8802 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8803 | return NULL; |
| 8804 | } |
| 8805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8806 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8807 | PyObject * |
| 8808 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8809 | Py_ssize_t size, |
| 8810 | PyObject *mapping, |
| 8811 | const char *errors) |
| 8812 | { |
| 8813 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8814 | if (!unicode) |
| 8815 | return NULL; |
| 8816 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8817 | } |
| 8818 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8819 | PyObject * |
| 8820 | PyUnicode_Translate(PyObject *str, |
| 8821 | PyObject *mapping, |
| 8822 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8823 | { |
| 8824 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | str = PyUnicode_FromObject(str); |
| 8827 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8828 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8830 | Py_DECREF(str); |
| 8831 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8832 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8833 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | Py_XDECREF(str); |
| 8835 | return NULL; |
| 8836 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8838 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8839 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8840 | { |
| 8841 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8842 | called as a callback from fixup() which does it already. */ |
| 8843 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8844 | const int kind = PyUnicode_KIND(self); |
| 8845 | void *data = PyUnicode_DATA(self); |
| 8846 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8847 | Py_ssize_t i; |
| 8848 | |
| 8849 | for (i = 0; i < len; ++i) { |
| 8850 | ch = PyUnicode_READ(kind, data, i); |
| 8851 | fixed = 0; |
| 8852 | if (ch > 127) { |
| 8853 | if (Py_UNICODE_ISSPACE(ch)) |
| 8854 | fixed = ' '; |
| 8855 | else { |
| 8856 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8857 | if (decimal >= 0) |
| 8858 | fixed = '0' + decimal; |
| 8859 | } |
| 8860 | if (fixed != 0) { |
| 8861 | if (fixed > maxchar) |
| 8862 | maxchar = fixed; |
| 8863 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8864 | } |
| 8865 | else if (ch > maxchar) |
| 8866 | maxchar = ch; |
| 8867 | } |
| 8868 | else if (ch > maxchar) |
| 8869 | maxchar = ch; |
| 8870 | } |
| 8871 | |
| 8872 | return maxchar; |
| 8873 | } |
| 8874 | |
| 8875 | PyObject * |
| 8876 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8877 | { |
| 8878 | if (!PyUnicode_Check(unicode)) { |
| 8879 | PyErr_BadInternalCall(); |
| 8880 | return NULL; |
| 8881 | } |
| 8882 | if (PyUnicode_READY(unicode) == -1) |
| 8883 | return NULL; |
| 8884 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8885 | /* If the string is already ASCII, just return the same string */ |
| 8886 | Py_INCREF(unicode); |
| 8887 | return unicode; |
| 8888 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8889 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8890 | } |
| 8891 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8892 | PyObject * |
| 8893 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8894 | Py_ssize_t length) |
| 8895 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8896 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8897 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8898 | Py_UCS4 maxchar; |
| 8899 | enum PyUnicode_Kind kind; |
| 8900 | void *data; |
| 8901 | |
| 8902 | maxchar = 0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8903 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8904 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8905 | if (ch > 127) { |
| 8906 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8907 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8908 | ch = '0' + decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8909 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8910 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8911 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8912 | |
| 8913 | /* Copy to a new string */ |
| 8914 | decimal = PyUnicode_New(length, maxchar); |
| 8915 | if (decimal == NULL) |
| 8916 | return decimal; |
| 8917 | kind = PyUnicode_KIND(decimal); |
| 8918 | data = PyUnicode_DATA(decimal); |
| 8919 | /* Iterate over code points */ |
| 8920 | for (i = 0; i < length; i++) { |
| 8921 | Py_UNICODE ch = s[i]; |
| 8922 | if (ch > 127) { |
| 8923 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8924 | if (decimal >= 0) |
| 8925 | ch = '0' + decimal; |
| 8926 | } |
| 8927 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8929 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8930 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8931 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8933 | int |
| 8934 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8935 | Py_ssize_t length, |
| 8936 | char *output, |
| 8937 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8938 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8939 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8940 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8941 | enum PyUnicode_Kind kind; |
| 8942 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8943 | |
| 8944 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8945 | PyErr_BadArgument(); |
| 8946 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8949 | unicode = PyUnicode_FromUnicode(s, length); |
| 8950 | if (unicode == NULL) |
| 8951 | return -1; |
| 8952 | |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8953 | if (PyUnicode_READY(unicode) < 0) { |
| 8954 | Py_DECREF(unicode); |
| 8955 | return -1; |
| 8956 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8957 | kind = PyUnicode_KIND(unicode); |
| 8958 | data = PyUnicode_DATA(unicode); |
| 8959 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8960 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8961 | PyObject *exc; |
| 8962 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8964 | Py_ssize_t startpos; |
| 8965 | |
| 8966 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8967 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8968 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8969 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8970 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8971 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8972 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8973 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8974 | if (decimal >= 0) { |
| 8975 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8976 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8977 | continue; |
| 8978 | } |
| 8979 | if (0 < ch && ch < 256) { |
| 8980 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8981 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8982 | continue; |
| 8983 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8984 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8985 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8986 | exc = NULL; |
| 8987 | raise_encode_exception(&exc, "decimal", unicode, |
| 8988 | startpos, startpos+1, |
| 8989 | "invalid decimal Unicode string"); |
| 8990 | Py_XDECREF(exc); |
| 8991 | Py_DECREF(unicode); |
| 8992 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8993 | } |
| 8994 | /* 0-terminate the output string */ |
| 8995 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8996 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8997 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8998 | } |
| 8999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | /* --- Helpers ------------------------------------------------------------ */ |
| 9001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9003 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | Py_ssize_t start, |
| 9005 | Py_ssize_t end) |
| 9006 | { |
| 9007 | int kind1, kind2, kind; |
| 9008 | void *buf1, *buf2; |
| 9009 | Py_ssize_t len1, len2, result; |
| 9010 | |
| 9011 | kind1 = PyUnicode_KIND(s1); |
| 9012 | kind2 = PyUnicode_KIND(s2); |
| 9013 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9014 | buf1 = PyUnicode_DATA(s1); |
| 9015 | buf2 = PyUnicode_DATA(s2); |
| 9016 | if (kind1 != kind) |
| 9017 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9018 | if (!buf1) |
| 9019 | return -2; |
| 9020 | if (kind2 != kind) |
| 9021 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9022 | if (!buf2) { |
| 9023 | if (kind1 != kind) PyMem_Free(buf1); |
| 9024 | return -2; |
| 9025 | } |
| 9026 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9027 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9028 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9029 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9030 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9031 | case PyUnicode_1BYTE_KIND: |
| 9032 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9033 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9034 | else |
| 9035 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9036 | break; |
| 9037 | case PyUnicode_2BYTE_KIND: |
| 9038 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9039 | break; |
| 9040 | case PyUnicode_4BYTE_KIND: |
| 9041 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9042 | break; |
| 9043 | default: |
| 9044 | assert(0); result = -2; |
| 9045 | } |
| 9046 | } |
| 9047 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9048 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9049 | case PyUnicode_1BYTE_KIND: |
| 9050 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9051 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9052 | else |
| 9053 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9054 | break; |
| 9055 | case PyUnicode_2BYTE_KIND: |
| 9056 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9057 | break; |
| 9058 | case PyUnicode_4BYTE_KIND: |
| 9059 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9060 | break; |
| 9061 | default: |
| 9062 | assert(0); result = -2; |
| 9063 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | if (kind1 != kind) |
| 9067 | PyMem_Free(buf1); |
| 9068 | if (kind2 != kind) |
| 9069 | PyMem_Free(buf2); |
| 9070 | |
| 9071 | return result; |
| 9072 | } |
| 9073 | |
| 9074 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9075 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9076 | Py_ssize_t n_buffer, |
| 9077 | void *digits, Py_ssize_t n_digits, |
| 9078 | Py_ssize_t min_width, |
| 9079 | const char *grouping, |
| 9080 | const char *thousands_sep) |
| 9081 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9082 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9083 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9084 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9085 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9086 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9087 | min_width, grouping, thousands_sep); |
| 9088 | else |
| 9089 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9090 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9091 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9092 | case PyUnicode_2BYTE_KIND: |
| 9093 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9094 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9095 | min_width, grouping, thousands_sep); |
| 9096 | case PyUnicode_4BYTE_KIND: |
| 9097 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9098 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9099 | min_width, grouping, thousands_sep); |
| 9100 | } |
| 9101 | assert(0); |
| 9102 | return -1; |
| 9103 | } |
| 9104 | |
| 9105 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9106 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9107 | #define ADJUST_INDICES(start, end, len) \ |
| 9108 | if (end > len) \ |
| 9109 | end = len; \ |
| 9110 | else if (end < 0) { \ |
| 9111 | end += len; \ |
| 9112 | if (end < 0) \ |
| 9113 | end = 0; \ |
| 9114 | } \ |
| 9115 | if (start < 0) { \ |
| 9116 | start += len; \ |
| 9117 | if (start < 0) \ |
| 9118 | start = 0; \ |
| 9119 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9120 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9121 | Py_ssize_t |
| 9122 | PyUnicode_Count(PyObject *str, |
| 9123 | PyObject *substr, |
| 9124 | Py_ssize_t start, |
| 9125 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9127 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9128 | PyObject* str_obj; |
| 9129 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9130 | int kind1, kind2, kind; |
| 9131 | void *buf1 = NULL, *buf2 = NULL; |
| 9132 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9133 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9134 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9135 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9136 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9137 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9138 | if (!sub_obj) { |
| 9139 | Py_DECREF(str_obj); |
| 9140 | return -1; |
| 9141 | } |
| 9142 | if (PyUnicode_READY(substr) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 9143 | Py_DECREF(substr); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | Py_DECREF(str_obj); |
| 9145 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9148 | kind1 = PyUnicode_KIND(str_obj); |
| 9149 | kind2 = PyUnicode_KIND(sub_obj); |
| 9150 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9151 | buf1 = PyUnicode_DATA(str_obj); |
| 9152 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9153 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9154 | if (!buf1) |
| 9155 | goto onError; |
| 9156 | buf2 = PyUnicode_DATA(sub_obj); |
| 9157 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9158 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9159 | if (!buf2) |
| 9160 | goto onError; |
| 9161 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9162 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9163 | |
| 9164 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9165 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9166 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9167 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9168 | result = asciilib_count( |
| 9169 | ((Py_UCS1*)buf1) + start, end - start, |
| 9170 | buf2, len2, PY_SSIZE_T_MAX |
| 9171 | ); |
| 9172 | else |
| 9173 | result = ucs1lib_count( |
| 9174 | ((Py_UCS1*)buf1) + start, end - start, |
| 9175 | buf2, len2, PY_SSIZE_T_MAX |
| 9176 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9177 | break; |
| 9178 | case PyUnicode_2BYTE_KIND: |
| 9179 | result = ucs2lib_count( |
| 9180 | ((Py_UCS2*)buf1) + start, end - start, |
| 9181 | buf2, len2, PY_SSIZE_T_MAX |
| 9182 | ); |
| 9183 | break; |
| 9184 | case PyUnicode_4BYTE_KIND: |
| 9185 | result = ucs4lib_count( |
| 9186 | ((Py_UCS4*)buf1) + start, end - start, |
| 9187 | buf2, len2, PY_SSIZE_T_MAX |
| 9188 | ); |
| 9189 | break; |
| 9190 | default: |
| 9191 | assert(0); result = 0; |
| 9192 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9193 | |
| 9194 | Py_DECREF(sub_obj); |
| 9195 | Py_DECREF(str_obj); |
| 9196 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | if (kind1 != kind) |
| 9198 | PyMem_Free(buf1); |
| 9199 | if (kind2 != kind) |
| 9200 | PyMem_Free(buf2); |
| 9201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9202 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9203 | onError: |
| 9204 | Py_DECREF(sub_obj); |
| 9205 | Py_DECREF(str_obj); |
| 9206 | if (kind1 != kind && buf1) |
| 9207 | PyMem_Free(buf1); |
| 9208 | if (kind2 != kind && buf2) |
| 9209 | PyMem_Free(buf2); |
| 9210 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9211 | } |
| 9212 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9213 | Py_ssize_t |
| 9214 | PyUnicode_Find(PyObject *str, |
| 9215 | PyObject *sub, |
| 9216 | Py_ssize_t start, |
| 9217 | Py_ssize_t end, |
| 9218 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9219 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9220 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9221 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9222 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9223 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9224 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9225 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9226 | if (!sub) { |
| 9227 | Py_DECREF(str); |
| 9228 | return -2; |
| 9229 | } |
| 9230 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9231 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9232 | Py_DECREF(str); |
| 9233 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9234 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9235 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9236 | result = any_find_slice(direction, |
| 9237 | str, sub, start, end |
| 9238 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9240 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9241 | Py_DECREF(sub); |
| 9242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9243 | return result; |
| 9244 | } |
| 9245 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9246 | Py_ssize_t |
| 9247 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9248 | Py_ssize_t start, Py_ssize_t end, |
| 9249 | int direction) |
| 9250 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9251 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9252 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9253 | if (PyUnicode_READY(str) == -1) |
| 9254 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9255 | if (start < 0 || end < 0) { |
| 9256 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9257 | return -2; |
| 9258 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9259 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9260 | end = PyUnicode_GET_LENGTH(str); |
| 9261 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9262 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9263 | kind, end-start, ch, direction); |
| 9264 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9265 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9266 | else |
| 9267 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9268 | } |
| 9269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9270 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9271 | tailmatch(PyObject *self, |
| 9272 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9273 | Py_ssize_t start, |
| 9274 | Py_ssize_t end, |
| 9275 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | int kind_self; |
| 9278 | int kind_sub; |
| 9279 | void *data_self; |
| 9280 | void *data_sub; |
| 9281 | Py_ssize_t offset; |
| 9282 | Py_ssize_t i; |
| 9283 | Py_ssize_t end_sub; |
| 9284 | |
| 9285 | if (PyUnicode_READY(self) == -1 || |
| 9286 | PyUnicode_READY(substring) == -1) |
| 9287 | return 0; |
| 9288 | |
| 9289 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9290 | return 1; |
| 9291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9292 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9293 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9294 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9295 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | kind_self = PyUnicode_KIND(self); |
| 9298 | data_self = PyUnicode_DATA(self); |
| 9299 | kind_sub = PyUnicode_KIND(substring); |
| 9300 | data_sub = PyUnicode_DATA(substring); |
| 9301 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9302 | |
| 9303 | if (direction > 0) |
| 9304 | offset = end; |
| 9305 | else |
| 9306 | offset = start; |
| 9307 | |
| 9308 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9309 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9310 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9311 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9312 | /* If both are of the same kind, memcmp is sufficient */ |
| 9313 | if (kind_self == kind_sub) { |
| 9314 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9315 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | data_sub, |
| 9317 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9318 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | } |
| 9320 | /* otherwise we have to compare each character by first accesing it */ |
| 9321 | else { |
| 9322 | /* We do not need to compare 0 and len(substring)-1 because |
| 9323 | the if statement above ensured already that they are equal |
| 9324 | when we end up here. */ |
| 9325 | // TODO: honor direction and do a forward or backwards search |
| 9326 | for (i = 1; i < end_sub; ++i) { |
| 9327 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9328 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9329 | return 0; |
| 9330 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9331 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | } |
| 9334 | |
| 9335 | return 0; |
| 9336 | } |
| 9337 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9338 | Py_ssize_t |
| 9339 | PyUnicode_Tailmatch(PyObject *str, |
| 9340 | PyObject *substr, |
| 9341 | Py_ssize_t start, |
| 9342 | Py_ssize_t end, |
| 9343 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9345 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9347 | str = PyUnicode_FromObject(str); |
| 9348 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9349 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | substr = PyUnicode_FromObject(substr); |
| 9351 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | Py_DECREF(str); |
| 9353 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9355 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9356 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9357 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9358 | Py_DECREF(str); |
| 9359 | Py_DECREF(substr); |
| 9360 | return result; |
| 9361 | } |
| 9362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | /* Apply fixfct filter to the Unicode object self and return a |
| 9364 | reference to the modified object */ |
| 9365 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9366 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9367 | fixup(PyObject *self, |
| 9368 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9370 | PyObject *u; |
| 9371 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9372 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9374 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9376 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9377 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9378 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9379 | /* fix functions return the new maximum character in a string, |
| 9380 | if the kind of the resulting unicode object does not change, |
| 9381 | everything is fine. Otherwise we need to change the string kind |
| 9382 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9383 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9384 | |
| 9385 | if (maxchar_new == 0) { |
| 9386 | /* no changes */; |
| 9387 | if (PyUnicode_CheckExact(self)) { |
| 9388 | Py_DECREF(u); |
| 9389 | Py_INCREF(self); |
| 9390 | return self; |
| 9391 | } |
| 9392 | else |
| 9393 | return u; |
| 9394 | } |
| 9395 | |
| 9396 | if (maxchar_new <= 127) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9397 | maxchar_new = 127; |
| 9398 | else if (maxchar_new <= 255) |
| 9399 | maxchar_new = 255; |
| 9400 | else if (maxchar_new <= 65535) |
| 9401 | maxchar_new = 65535; |
| 9402 | else |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 9403 | maxchar_new = MAX_UNICODE; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9404 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9405 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9406 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9407 | |
| 9408 | /* In case the maximum character changed, we need to |
| 9409 | convert the string to the new category. */ |
| 9410 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9411 | if (v == NULL) { |
| 9412 | Py_DECREF(u); |
| 9413 | return NULL; |
| 9414 | } |
| 9415 | if (maxchar_new > maxchar_old) { |
| 9416 | /* If the maxchar increased so that the kind changed, not all |
| 9417 | characters are representable anymore and we need to fix the |
| 9418 | string again. This only happens in very few cases. */ |
| 9419 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
| 9420 | maxchar_old = fixfct(v); |
| 9421 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9422 | } |
| 9423 | else { |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9424 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9426 | Py_DECREF(u); |
| 9427 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9428 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9429 | } |
| 9430 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9431 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9432 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9434 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9435 | called as a callback from fixup() which does it already. */ |
| 9436 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9437 | const int kind = PyUnicode_KIND(self); |
| 9438 | void *data = PyUnicode_DATA(self); |
| 9439 | int touched = 0; |
| 9440 | Py_UCS4 maxchar = 0; |
| 9441 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | for (i = 0; i < len; ++i) { |
| 9444 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9445 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9446 | if (up != ch) { |
| 9447 | if (up > maxchar) |
| 9448 | maxchar = up; |
| 9449 | PyUnicode_WRITE(kind, data, i, up); |
| 9450 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9451 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | else if (ch > maxchar) |
| 9453 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9454 | } |
| 9455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9456 | if (touched) |
| 9457 | return maxchar; |
| 9458 | else |
| 9459 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9460 | } |
| 9461 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9462 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9463 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9464 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9466 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9467 | const int kind = PyUnicode_KIND(self); |
| 9468 | void *data = PyUnicode_DATA(self); |
| 9469 | int touched = 0; |
| 9470 | Py_UCS4 maxchar = 0; |
| 9471 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9472 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9473 | for(i = 0; i < len; ++i) { |
| 9474 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9475 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9476 | if (lo != ch) { |
| 9477 | if (lo > maxchar) |
| 9478 | maxchar = lo; |
| 9479 | PyUnicode_WRITE(kind, data, i, lo); |
| 9480 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | else if (ch > maxchar) |
| 9483 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9486 | if (touched) |
| 9487 | return maxchar; |
| 9488 | else |
| 9489 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9490 | } |
| 9491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9492 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9493 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9496 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9497 | const int kind = PyUnicode_KIND(self); |
| 9498 | void *data = PyUnicode_DATA(self); |
| 9499 | int touched = 0; |
| 9500 | Py_UCS4 maxchar = 0; |
| 9501 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9502 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | for(i = 0; i < len; ++i) { |
| 9504 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9505 | Py_UCS4 nu = 0; |
| 9506 | |
| 9507 | if (Py_UNICODE_ISUPPER(ch)) |
| 9508 | nu = Py_UNICODE_TOLOWER(ch); |
| 9509 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9510 | nu = Py_UNICODE_TOUPPER(ch); |
| 9511 | |
| 9512 | if (nu != 0) { |
| 9513 | if (nu > maxchar) |
| 9514 | maxchar = nu; |
| 9515 | PyUnicode_WRITE(kind, data, i, nu); |
| 9516 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9518 | else if (ch > maxchar) |
| 9519 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9522 | if (touched) |
| 9523 | return maxchar; |
| 9524 | else |
| 9525 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9526 | } |
| 9527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9528 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9529 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9530 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9531 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9532 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9533 | const int kind = PyUnicode_KIND(self); |
| 9534 | void *data = PyUnicode_DATA(self); |
| 9535 | int touched = 0; |
| 9536 | Py_UCS4 maxchar = 0; |
| 9537 | Py_ssize_t i = 0; |
| 9538 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9539 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9540 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9541 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9542 | |
| 9543 | ch = PyUnicode_READ(kind, data, i); |
| 9544 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9545 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9546 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9547 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9549 | ++i; |
| 9550 | for(; i < len; ++i) { |
| 9551 | ch = PyUnicode_READ(kind, data, i); |
| 9552 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9553 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9554 | if (lo > maxchar) |
| 9555 | maxchar = lo; |
| 9556 | PyUnicode_WRITE(kind, data, i, lo); |
| 9557 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9558 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9559 | else if (ch > maxchar) |
| 9560 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9562 | |
| 9563 | if (touched) |
| 9564 | return maxchar; |
| 9565 | else |
| 9566 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9567 | } |
| 9568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9569 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9570 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9571 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9572 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9573 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9574 | const int kind = PyUnicode_KIND(self); |
| 9575 | void *data = PyUnicode_DATA(self); |
| 9576 | Py_UCS4 maxchar = 0; |
| 9577 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | int previous_is_cased; |
| 9579 | |
| 9580 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9581 | if (len == 1) { |
| 9582 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9583 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9584 | if (ti != ch) { |
| 9585 | PyUnicode_WRITE(kind, data, i, ti); |
| 9586 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9587 | } |
| 9588 | else |
| 9589 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9591 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9592 | for(; i < len; ++i) { |
| 9593 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9594 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9596 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9597 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9598 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9599 | nu = Py_UNICODE_TOTITLE(ch); |
| 9600 | |
| 9601 | if (nu > maxchar) |
| 9602 | maxchar = nu; |
| 9603 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9605 | if (Py_UNICODE_ISLOWER(ch) || |
| 9606 | Py_UNICODE_ISUPPER(ch) || |
| 9607 | Py_UNICODE_ISTITLE(ch)) |
| 9608 | previous_is_cased = 1; |
| 9609 | else |
| 9610 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9611 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9612 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9613 | } |
| 9614 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9615 | PyObject * |
| 9616 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9617 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9618 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9619 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9620 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9621 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9622 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9623 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9624 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9625 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9626 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9627 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9628 | int use_memcpy; |
| 9629 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9630 | PyObject *last_obj; |
| 9631 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9632 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9633 | fseq = PySequence_Fast(seq, ""); |
| 9634 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9635 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9636 | } |
| 9637 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9638 | /* NOTE: the following code can't call back into Python code, |
| 9639 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9640 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9641 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9642 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9643 | /* If empty sequence, return u"". */ |
| 9644 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9645 | Py_DECREF(fseq); |
| 9646 | Py_INCREF(unicode_empty); |
| 9647 | res = unicode_empty; |
| 9648 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9649 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9650 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9651 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9652 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9653 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9654 | if (seqlen == 1) { |
| 9655 | if (PyUnicode_CheckExact(items[0])) { |
| 9656 | res = items[0]; |
| 9657 | Py_INCREF(res); |
| 9658 | Py_DECREF(fseq); |
| 9659 | return res; |
| 9660 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9661 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9662 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9663 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9664 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9665 | /* Set up sep and seplen */ |
| 9666 | if (separator == NULL) { |
| 9667 | /* fall back to a blank space separator */ |
| 9668 | sep = PyUnicode_FromOrdinal(' '); |
| 9669 | if (!sep) |
| 9670 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9671 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9672 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9673 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9674 | else { |
| 9675 | if (!PyUnicode_Check(separator)) { |
| 9676 | PyErr_Format(PyExc_TypeError, |
| 9677 | "separator: expected str instance," |
| 9678 | " %.80s found", |
| 9679 | Py_TYPE(separator)->tp_name); |
| 9680 | goto onError; |
| 9681 | } |
| 9682 | if (PyUnicode_READY(separator)) |
| 9683 | goto onError; |
| 9684 | sep = separator; |
| 9685 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9686 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9687 | /* inc refcount to keep this code path symmetric with the |
| 9688 | above case of a blank separator */ |
| 9689 | Py_INCREF(sep); |
| 9690 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9691 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9692 | } |
| 9693 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9694 | /* There are at least two things to join, or else we have a subclass |
| 9695 | * of str in the sequence. |
| 9696 | * Do a pre-pass to figure out the total amount of space we'll |
| 9697 | * need (sz), and see whether all argument are strings. |
| 9698 | */ |
| 9699 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9700 | #ifdef Py_DEBUG |
| 9701 | use_memcpy = 0; |
| 9702 | #else |
| 9703 | use_memcpy = 1; |
| 9704 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9705 | for (i = 0; i < seqlen; i++) { |
| 9706 | const Py_ssize_t old_sz = sz; |
| 9707 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9708 | if (!PyUnicode_Check(item)) { |
| 9709 | PyErr_Format(PyExc_TypeError, |
| 9710 | "sequence item %zd: expected str instance," |
| 9711 | " %.80s found", |
| 9712 | i, Py_TYPE(item)->tp_name); |
| 9713 | goto onError; |
| 9714 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9715 | if (PyUnicode_READY(item) == -1) |
| 9716 | goto onError; |
| 9717 | sz += PyUnicode_GET_LENGTH(item); |
| 9718 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9719 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9720 | if (i != 0) |
| 9721 | sz += seplen; |
| 9722 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9723 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9724 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9725 | goto onError; |
| 9726 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9727 | if (use_memcpy && last_obj != NULL) { |
| 9728 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9729 | use_memcpy = 0; |
| 9730 | } |
| 9731 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9732 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9735 | if (res == NULL) |
| 9736 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9737 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9738 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9739 | #ifdef Py_DEBUG |
| 9740 | use_memcpy = 0; |
| 9741 | #else |
| 9742 | if (use_memcpy) { |
| 9743 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9744 | kind = PyUnicode_KIND(res); |
| 9745 | if (seplen != 0) |
| 9746 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9747 | } |
| 9748 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9749 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9750 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9751 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9752 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9753 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9754 | if (use_memcpy) { |
| 9755 | Py_MEMCPY(res_data, |
| 9756 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9757 | kind * seplen); |
| 9758 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9759 | } |
| 9760 | else { |
| 9761 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9762 | res_offset += seplen; |
| 9763 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9764 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9765 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9766 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9767 | if (use_memcpy) { |
| 9768 | Py_MEMCPY(res_data, |
| 9769 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9770 | kind * itemlen); |
| 9771 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9772 | } |
| 9773 | else { |
| 9774 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9775 | res_offset += itemlen; |
| 9776 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9777 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9778 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9779 | if (use_memcpy) |
| 9780 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9781 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9782 | else |
| 9783 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9784 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9785 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9786 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9787 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9790 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9791 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9793 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9794 | return NULL; |
| 9795 | } |
| 9796 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | #define FILL(kind, data, value, start, length) \ |
| 9798 | do { \ |
| 9799 | Py_ssize_t i_ = 0; \ |
| 9800 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9801 | switch ((kind)) { \ |
| 9802 | case PyUnicode_1BYTE_KIND: { \ |
| 9803 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9804 | memset(to_, (unsigned char)value, length); \ |
| 9805 | break; \ |
| 9806 | } \ |
| 9807 | case PyUnicode_2BYTE_KIND: { \ |
| 9808 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9809 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9810 | break; \ |
| 9811 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9812 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9813 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9814 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9815 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9816 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9817 | } \ |
| 9818 | } \ |
| 9819 | } while (0) |
| 9820 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9821 | static PyObject * |
| 9822 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9823 | Py_ssize_t left, |
| 9824 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9826 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9827 | PyObject *u; |
| 9828 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9829 | int kind; |
| 9830 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9831 | |
| 9832 | if (left < 0) |
| 9833 | left = 0; |
| 9834 | if (right < 0) |
| 9835 | right = 0; |
| 9836 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9837 | if (left == 0 && right == 0) |
| 9838 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9840 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9841 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9842 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9843 | return NULL; |
| 9844 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9845 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9846 | if (fill > maxchar) |
| 9847 | maxchar = fill; |
| 9848 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9849 | if (!u) |
| 9850 | return NULL; |
| 9851 | |
| 9852 | kind = PyUnicode_KIND(u); |
| 9853 | data = PyUnicode_DATA(u); |
| 9854 | if (left) |
| 9855 | FILL(kind, data, fill, 0, left); |
| 9856 | if (right) |
| 9857 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9858 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9859 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9860 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9861 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9864 | PyObject * |
| 9865 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9866 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9867 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9868 | |
| 9869 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9870 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9871 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9872 | if (PyUnicode_READY(string) == -1) { |
| 9873 | Py_DECREF(string); |
| 9874 | return NULL; |
| 9875 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9876 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9877 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9878 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9879 | if (PyUnicode_IS_ASCII(string)) |
| 9880 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9881 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9882 | PyUnicode_GET_LENGTH(string), keepends); |
| 9883 | else |
| 9884 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9885 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9886 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | break; |
| 9888 | case PyUnicode_2BYTE_KIND: |
| 9889 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9890 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | PyUnicode_GET_LENGTH(string), keepends); |
| 9892 | break; |
| 9893 | case PyUnicode_4BYTE_KIND: |
| 9894 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9895 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | PyUnicode_GET_LENGTH(string), keepends); |
| 9897 | break; |
| 9898 | default: |
| 9899 | assert(0); |
| 9900 | list = 0; |
| 9901 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9902 | Py_DECREF(string); |
| 9903 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | } |
| 9905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9906 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9907 | split(PyObject *self, |
| 9908 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9909 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9910 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | int kind1, kind2, kind; |
| 9912 | void *buf1, *buf2; |
| 9913 | Py_ssize_t len1, len2; |
| 9914 | PyObject* out; |
| 9915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9916 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9917 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9919 | if (PyUnicode_READY(self) == -1) |
| 9920 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9923 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9925 | if (PyUnicode_IS_ASCII(self)) |
| 9926 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9927 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9928 | PyUnicode_GET_LENGTH(self), maxcount |
| 9929 | ); |
| 9930 | else |
| 9931 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9932 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9933 | PyUnicode_GET_LENGTH(self), maxcount |
| 9934 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | case PyUnicode_2BYTE_KIND: |
| 9936 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9937 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | PyUnicode_GET_LENGTH(self), maxcount |
| 9939 | ); |
| 9940 | case PyUnicode_4BYTE_KIND: |
| 9941 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9942 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | PyUnicode_GET_LENGTH(self), maxcount |
| 9944 | ); |
| 9945 | default: |
| 9946 | assert(0); |
| 9947 | return NULL; |
| 9948 | } |
| 9949 | |
| 9950 | if (PyUnicode_READY(substring) == -1) |
| 9951 | return NULL; |
| 9952 | |
| 9953 | kind1 = PyUnicode_KIND(self); |
| 9954 | kind2 = PyUnicode_KIND(substring); |
| 9955 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9956 | buf1 = PyUnicode_DATA(self); |
| 9957 | buf2 = PyUnicode_DATA(substring); |
| 9958 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9959 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9960 | if (!buf1) |
| 9961 | return NULL; |
| 9962 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9963 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9964 | if (!buf2) { |
| 9965 | if (kind1 != kind) PyMem_Free(buf1); |
| 9966 | return NULL; |
| 9967 | } |
| 9968 | len1 = PyUnicode_GET_LENGTH(self); |
| 9969 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9970 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9971 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9973 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9974 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9975 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9976 | else |
| 9977 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9978 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9979 | break; |
| 9980 | case PyUnicode_2BYTE_KIND: |
| 9981 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9982 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | break; |
| 9984 | case PyUnicode_4BYTE_KIND: |
| 9985 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9986 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9987 | break; |
| 9988 | default: |
| 9989 | out = NULL; |
| 9990 | } |
| 9991 | if (kind1 != kind) |
| 9992 | PyMem_Free(buf1); |
| 9993 | if (kind2 != kind) |
| 9994 | PyMem_Free(buf2); |
| 9995 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9996 | } |
| 9997 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9998 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9999 | rsplit(PyObject *self, |
| 10000 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10001 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10002 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 | int kind1, kind2, kind; |
| 10004 | void *buf1, *buf2; |
| 10005 | Py_ssize_t len1, len2; |
| 10006 | PyObject* out; |
| 10007 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10008 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10009 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10011 | if (PyUnicode_READY(self) == -1) |
| 10012 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10015 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10016 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10017 | if (PyUnicode_IS_ASCII(self)) |
| 10018 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10019 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10020 | PyUnicode_GET_LENGTH(self), maxcount |
| 10021 | ); |
| 10022 | else |
| 10023 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10024 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10025 | PyUnicode_GET_LENGTH(self), maxcount |
| 10026 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10027 | case PyUnicode_2BYTE_KIND: |
| 10028 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10029 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | PyUnicode_GET_LENGTH(self), maxcount |
| 10031 | ); |
| 10032 | case PyUnicode_4BYTE_KIND: |
| 10033 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10034 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | PyUnicode_GET_LENGTH(self), maxcount |
| 10036 | ); |
| 10037 | default: |
| 10038 | assert(0); |
| 10039 | return NULL; |
| 10040 | } |
| 10041 | |
| 10042 | if (PyUnicode_READY(substring) == -1) |
| 10043 | return NULL; |
| 10044 | |
| 10045 | kind1 = PyUnicode_KIND(self); |
| 10046 | kind2 = PyUnicode_KIND(substring); |
| 10047 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10048 | buf1 = PyUnicode_DATA(self); |
| 10049 | buf2 = PyUnicode_DATA(substring); |
| 10050 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10051 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | if (!buf1) |
| 10053 | return NULL; |
| 10054 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10055 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10056 | if (!buf2) { |
| 10057 | if (kind1 != kind) PyMem_Free(buf1); |
| 10058 | return NULL; |
| 10059 | } |
| 10060 | len1 = PyUnicode_GET_LENGTH(self); |
| 10061 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10062 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10063 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10065 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10066 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10067 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10068 | else |
| 10069 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10070 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | break; |
| 10072 | case PyUnicode_2BYTE_KIND: |
| 10073 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10074 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | break; |
| 10076 | case PyUnicode_4BYTE_KIND: |
| 10077 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10078 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10079 | break; |
| 10080 | default: |
| 10081 | out = NULL; |
| 10082 | } |
| 10083 | if (kind1 != kind) |
| 10084 | PyMem_Free(buf1); |
| 10085 | if (kind2 != kind) |
| 10086 | PyMem_Free(buf2); |
| 10087 | return out; |
| 10088 | } |
| 10089 | |
| 10090 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10091 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10092 | 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] | 10093 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10094 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10095 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10096 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10097 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10098 | else |
| 10099 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | case PyUnicode_2BYTE_KIND: |
| 10101 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10102 | case PyUnicode_4BYTE_KIND: |
| 10103 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10104 | } |
| 10105 | assert(0); |
| 10106 | return -1; |
| 10107 | } |
| 10108 | |
| 10109 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10110 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10111 | 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] | 10112 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10113 | switch (kind) { |
| 10114 | case PyUnicode_1BYTE_KIND: |
| 10115 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10116 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10117 | else |
| 10118 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10119 | case PyUnicode_2BYTE_KIND: |
| 10120 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10121 | case PyUnicode_4BYTE_KIND: |
| 10122 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10123 | } |
| 10124 | assert(0); |
| 10125 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10126 | } |
| 10127 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10128 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10129 | replace(PyObject *self, PyObject *str1, |
| 10130 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10131 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | PyObject *u; |
| 10133 | char *sbuf = PyUnicode_DATA(self); |
| 10134 | char *buf1 = PyUnicode_DATA(str1); |
| 10135 | char *buf2 = PyUnicode_DATA(str2); |
| 10136 | int srelease = 0, release1 = 0, release2 = 0; |
| 10137 | int skind = PyUnicode_KIND(self); |
| 10138 | int kind1 = PyUnicode_KIND(str1); |
| 10139 | int kind2 = PyUnicode_KIND(str2); |
| 10140 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10141 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10142 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10143 | int mayshrink; |
| 10144 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10145 | |
| 10146 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10147 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10149 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10151 | if (str1 == str2) |
| 10152 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | if (skind < kind1) |
| 10154 | /* substring too wide to be present */ |
| 10155 | goto nothing; |
| 10156 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10157 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10158 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10159 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10160 | result string. */ |
| 10161 | mayshrink = (maxchar_str2 < maxchar); |
| 10162 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10165 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10167 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10169 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10170 | Py_UCS4 u1, u2; |
| 10171 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10172 | Py_ssize_t index, pos; |
| 10173 | char *src; |
| 10174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10176 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10177 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10178 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10181 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10183 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10184 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10185 | |
| 10186 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10187 | index = 0; |
| 10188 | src = sbuf; |
| 10189 | while (--maxcount) |
| 10190 | { |
| 10191 | pos++; |
| 10192 | src += pos * PyUnicode_KIND(self); |
| 10193 | slen -= pos; |
| 10194 | index += pos; |
| 10195 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10196 | if (pos < 0) |
| 10197 | break; |
| 10198 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10199 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10200 | } |
| 10201 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | int rkind = skind; |
| 10203 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10204 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10205 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | if (kind1 < rkind) { |
| 10207 | /* widen substring */ |
| 10208 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10209 | if (!buf1) goto error; |
| 10210 | release1 = 1; |
| 10211 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10212 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10213 | if (i < 0) |
| 10214 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | if (rkind > kind2) { |
| 10216 | /* widen replacement */ |
| 10217 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10218 | if (!buf2) goto error; |
| 10219 | release2 = 1; |
| 10220 | } |
| 10221 | else if (rkind < kind2) { |
| 10222 | /* widen self and buf1 */ |
| 10223 | rkind = kind2; |
| 10224 | if (release1) PyMem_Free(buf1); |
| 10225 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10226 | if (!sbuf) goto error; |
| 10227 | srelease = 1; |
| 10228 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10229 | if (!buf1) goto error; |
| 10230 | release1 = 1; |
| 10231 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10232 | u = PyUnicode_New(slen, maxchar); |
| 10233 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10235 | assert(PyUnicode_KIND(u) == rkind); |
| 10236 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10237 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10238 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10239 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10240 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10242 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10244 | |
| 10245 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10246 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10247 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10248 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10249 | if (i == -1) |
| 10250 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10251 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10252 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10253 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10254 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10255 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10256 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10257 | } |
| 10258 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | Py_ssize_t n, i, j, ires; |
| 10260 | Py_ssize_t product, new_size; |
| 10261 | int rkind = skind; |
| 10262 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10264 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10265 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10267 | if (!buf1) goto error; |
| 10268 | release1 = 1; |
| 10269 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10270 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10271 | if (n == 0) |
| 10272 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10273 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10274 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10275 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10276 | if (!buf2) goto error; |
| 10277 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10278 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10280 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | rkind = kind2; |
| 10282 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10283 | if (!sbuf) goto error; |
| 10284 | srelease = 1; |
| 10285 | if (release1) PyMem_Free(buf1); |
| 10286 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10287 | if (!buf1) goto error; |
| 10288 | release1 = 1; |
| 10289 | } |
| 10290 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10291 | PyUnicode_GET_LENGTH(str1))); */ |
| 10292 | product = n * (len2-len1); |
| 10293 | if ((product / (len2-len1)) != n) { |
| 10294 | PyErr_SetString(PyExc_OverflowError, |
| 10295 | "replace string is too long"); |
| 10296 | goto error; |
| 10297 | } |
| 10298 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10299 | if (new_size == 0) { |
| 10300 | Py_INCREF(unicode_empty); |
| 10301 | u = unicode_empty; |
| 10302 | goto done; |
| 10303 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10304 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10305 | PyErr_SetString(PyExc_OverflowError, |
| 10306 | "replace string is too long"); |
| 10307 | goto error; |
| 10308 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10309 | u = PyUnicode_New(new_size, maxchar); |
| 10310 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10312 | assert(PyUnicode_KIND(u) == rkind); |
| 10313 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10314 | ires = i = 0; |
| 10315 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10316 | while (n-- > 0) { |
| 10317 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10318 | j = 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 (j == -1) |
| 10322 | break; |
| 10323 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10324 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10325 | memcpy(res + rkind * ires, |
| 10326 | sbuf + rkind * i, |
| 10327 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10328 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10329 | } |
| 10330 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10332 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10334 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10335 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10336 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10340 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10341 | memcpy(res + rkind * ires, |
| 10342 | sbuf + rkind * i, |
| 10343 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10344 | } |
| 10345 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10346 | /* interleave */ |
| 10347 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10348 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10349 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10350 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10352 | if (--n <= 0) |
| 10353 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10354 | memcpy(res + rkind * ires, |
| 10355 | sbuf + rkind * i, |
| 10356 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | ires++; |
| 10358 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10359 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10360 | memcpy(res + rkind * ires, |
| 10361 | sbuf + rkind * i, |
| 10362 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10363 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10364 | } |
| 10365 | |
| 10366 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10367 | unicode_adjust_maxchar(&u); |
| 10368 | if (u == NULL) |
| 10369 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10371 | |
| 10372 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10373 | if (srelease) |
| 10374 | PyMem_FREE(sbuf); |
| 10375 | if (release1) |
| 10376 | PyMem_FREE(buf1); |
| 10377 | if (release2) |
| 10378 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10379 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10382 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10383 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10384 | if (srelease) |
| 10385 | PyMem_FREE(sbuf); |
| 10386 | if (release1) |
| 10387 | PyMem_FREE(buf1); |
| 10388 | if (release2) |
| 10389 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10390 | return unicode_result_unchanged(self); |
| 10391 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10392 | error: |
| 10393 | if (srelease && sbuf) |
| 10394 | PyMem_FREE(sbuf); |
| 10395 | if (release1 && buf1) |
| 10396 | PyMem_FREE(buf1); |
| 10397 | if (release2 && buf2) |
| 10398 | PyMem_FREE(buf2); |
| 10399 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | } |
| 10401 | |
| 10402 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10404 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10405 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10406 | \n\ |
| 10407 | 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] | 10408 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10409 | |
| 10410 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10411 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10412 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10413 | return fixup(self, fixtitle); |
| 10414 | } |
| 10415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10416 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10417 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10418 | \n\ |
| 10419 | 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] | 10420 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | |
| 10422 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10423 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10424 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10425 | return fixup(self, fixcapitalize); |
| 10426 | } |
| 10427 | |
| 10428 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10429 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10430 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10431 | \n\ |
| 10432 | Apply .capitalize() to all words in S and return the result with\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10433 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10434 | |
| 10435 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10436 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10437 | { |
| 10438 | PyObject *list; |
| 10439 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10440 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | /* Split into words */ |
| 10443 | list = split(self, NULL, -1); |
| 10444 | if (!list) |
| 10445 | return NULL; |
| 10446 | |
| 10447 | /* Capitalize each word */ |
| 10448 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10449 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10450 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10451 | if (item == NULL) |
| 10452 | goto onError; |
| 10453 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10454 | PyList_SET_ITEM(list, i, item); |
| 10455 | } |
| 10456 | |
| 10457 | /* Join the words to form a new string */ |
| 10458 | item = PyUnicode_Join(NULL, list); |
| 10459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10460 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10461 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10462 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10463 | } |
| 10464 | #endif |
| 10465 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10466 | /* Argument converter. Coerces to a single unicode character */ |
| 10467 | |
| 10468 | static int |
| 10469 | convert_uc(PyObject *obj, void *addr) |
| 10470 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10472 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10473 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10474 | uniobj = PyUnicode_FromObject(obj); |
| 10475 | if (uniobj == NULL) { |
| 10476 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10477 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10478 | return 0; |
| 10479 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10481 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10482 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10483 | Py_DECREF(uniobj); |
| 10484 | return 0; |
| 10485 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10487 | Py_DECREF(uniobj); |
| 10488 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10489 | } |
| 10490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10491 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10492 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10494 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10495 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10496 | |
| 10497 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10498 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10499 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10500 | Py_ssize_t marg, left; |
| 10501 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10502 | Py_UCS4 fillchar = ' '; |
| 10503 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10504 | 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] | 10505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10507 | if (PyUnicode_READY(self) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10508 | return NULL; |
| 10509 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10510 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10511 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10513 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 | left = marg / 2 + (marg & width & 1); |
| 10515 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10516 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | } |
| 10518 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10519 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10520 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10521 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10522 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10524 | int kind1, kind2; |
| 10525 | void *data1, *data2; |
| 10526 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10528 | kind1 = PyUnicode_KIND(str1); |
| 10529 | kind2 = PyUnicode_KIND(str2); |
| 10530 | data1 = PyUnicode_DATA(str1); |
| 10531 | data2 = PyUnicode_DATA(str2); |
| 10532 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10533 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10534 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10536 | Py_UCS4 c1, c2; |
| 10537 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10538 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10539 | |
| 10540 | if (c1 != c2) |
| 10541 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10542 | } |
| 10543 | |
| 10544 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10545 | } |
| 10546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10547 | int |
| 10548 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10551 | if (PyUnicode_READY(left) == -1 || |
| 10552 | PyUnicode_READY(right) == -1) |
| 10553 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10554 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10556 | PyErr_Format(PyExc_TypeError, |
| 10557 | "Can't compare %.100s and %.100s", |
| 10558 | left->ob_type->tp_name, |
| 10559 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10560 | return -1; |
| 10561 | } |
| 10562 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10563 | int |
| 10564 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10565 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | Py_ssize_t i; |
| 10567 | int kind; |
| 10568 | void *data; |
| 10569 | Py_UCS4 chr; |
| 10570 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10571 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (PyUnicode_READY(uni) == -1) |
| 10573 | return -1; |
| 10574 | kind = PyUnicode_KIND(uni); |
| 10575 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10576 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10577 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10578 | if (chr != str[i]) |
| 10579 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10580 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10581 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10582 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10583 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10584 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10585 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10586 | return 0; |
| 10587 | } |
| 10588 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10591 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10592 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10593 | PyObject * |
| 10594 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10595 | { |
| 10596 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10597 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10598 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10599 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10600 | if (PyUnicode_READY(left) == -1 || |
| 10601 | PyUnicode_READY(right) == -1) |
| 10602 | return NULL; |
| 10603 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10604 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10605 | if (op == Py_EQ) { |
| 10606 | Py_INCREF(Py_False); |
| 10607 | return Py_False; |
| 10608 | } |
| 10609 | if (op == Py_NE) { |
| 10610 | Py_INCREF(Py_True); |
| 10611 | return Py_True; |
| 10612 | } |
| 10613 | } |
| 10614 | if (left == right) |
| 10615 | result = 0; |
| 10616 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10617 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10618 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10619 | /* Convert the return value to a Boolean */ |
| 10620 | switch (op) { |
| 10621 | case Py_EQ: |
| 10622 | v = TEST_COND(result == 0); |
| 10623 | break; |
| 10624 | case Py_NE: |
| 10625 | v = TEST_COND(result != 0); |
| 10626 | break; |
| 10627 | case Py_LE: |
| 10628 | v = TEST_COND(result <= 0); |
| 10629 | break; |
| 10630 | case Py_GE: |
| 10631 | v = TEST_COND(result >= 0); |
| 10632 | break; |
| 10633 | case Py_LT: |
| 10634 | v = TEST_COND(result == -1); |
| 10635 | break; |
| 10636 | case Py_GT: |
| 10637 | v = TEST_COND(result == 1); |
| 10638 | break; |
| 10639 | default: |
| 10640 | PyErr_BadArgument(); |
| 10641 | return NULL; |
| 10642 | } |
| 10643 | Py_INCREF(v); |
| 10644 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10645 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10646 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10647 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10650 | int |
| 10651 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10652 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10653 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10654 | int kind1, kind2, kind; |
| 10655 | void *buf1, *buf2; |
| 10656 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10657 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10658 | |
| 10659 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10660 | sub = PyUnicode_FromObject(element); |
| 10661 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10662 | PyErr_Format(PyExc_TypeError, |
| 10663 | "'in <string>' requires string as left operand, not %s", |
| 10664 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10665 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10666 | } |
| 10667 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10668 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10669 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10670 | Py_DECREF(sub); |
| 10671 | return -1; |
| 10672 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10673 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10674 | Py_DECREF(sub); |
| 10675 | Py_DECREF(str); |
| 10676 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10678 | kind1 = PyUnicode_KIND(str); |
| 10679 | kind2 = PyUnicode_KIND(sub); |
| 10680 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10681 | buf1 = PyUnicode_DATA(str); |
| 10682 | buf2 = PyUnicode_DATA(sub); |
| 10683 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10684 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10685 | if (!buf1) { |
| 10686 | Py_DECREF(sub); |
| 10687 | return -1; |
| 10688 | } |
| 10689 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10690 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | if (!buf2) { |
| 10692 | Py_DECREF(sub); |
| 10693 | if (kind1 != kind) PyMem_Free(buf1); |
| 10694 | return -1; |
| 10695 | } |
| 10696 | len1 = PyUnicode_GET_LENGTH(str); |
| 10697 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10698 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10699 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | case PyUnicode_1BYTE_KIND: |
| 10701 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10702 | break; |
| 10703 | case PyUnicode_2BYTE_KIND: |
| 10704 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10705 | break; |
| 10706 | case PyUnicode_4BYTE_KIND: |
| 10707 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10708 | break; |
| 10709 | default: |
| 10710 | result = -1; |
| 10711 | assert(0); |
| 10712 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10713 | |
| 10714 | Py_DECREF(str); |
| 10715 | Py_DECREF(sub); |
| 10716 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | if (kind1 != kind) |
| 10718 | PyMem_Free(buf1); |
| 10719 | if (kind2 != kind) |
| 10720 | PyMem_Free(buf2); |
| 10721 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10722 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10723 | } |
| 10724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10726 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10727 | PyObject * |
| 10728 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10730 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10731 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10732 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10733 | |
| 10734 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10735 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10737 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10740 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10741 | |
| 10742 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10743 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10744 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10745 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10747 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10748 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10749 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 | } |
| 10751 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10752 | u_len = PyUnicode_GET_LENGTH(u); |
| 10753 | v_len = PyUnicode_GET_LENGTH(v); |
| 10754 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10755 | PyErr_SetString(PyExc_OverflowError, |
| 10756 | "strings are too large to concat"); |
| 10757 | goto onError; |
| 10758 | } |
| 10759 | new_len = u_len + v_len; |
| 10760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10761 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10762 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10763 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10764 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10765 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10766 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | goto onError; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10769 | copy_characters(w, 0, u, 0, u_len); |
| 10770 | copy_characters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | Py_DECREF(u); |
| 10772 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10773 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10774 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10776 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10777 | Py_XDECREF(u); |
| 10778 | Py_XDECREF(v); |
| 10779 | return NULL; |
| 10780 | } |
| 10781 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10782 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10783 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10784 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10785 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10786 | Py_UCS4 maxchar, maxchar2; |
| 10787 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10788 | |
| 10789 | if (p_left == NULL) { |
| 10790 | if (!PyErr_Occurred()) |
| 10791 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10792 | return; |
| 10793 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10794 | left = *p_left; |
| 10795 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10796 | if (!PyErr_Occurred()) |
| 10797 | PyErr_BadInternalCall(); |
| 10798 | goto error; |
| 10799 | } |
| 10800 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10801 | if (PyUnicode_READY(left)) |
| 10802 | goto error; |
| 10803 | if (PyUnicode_READY(right)) |
| 10804 | goto error; |
| 10805 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10806 | /* Shortcuts */ |
| 10807 | if (left == unicode_empty) { |
| 10808 | Py_DECREF(left); |
| 10809 | Py_INCREF(right); |
| 10810 | *p_left = right; |
| 10811 | return; |
| 10812 | } |
| 10813 | if (right == unicode_empty) |
| 10814 | return; |
| 10815 | |
| 10816 | left_len = PyUnicode_GET_LENGTH(left); |
| 10817 | right_len = PyUnicode_GET_LENGTH(right); |
| 10818 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10819 | PyErr_SetString(PyExc_OverflowError, |
| 10820 | "strings are too large to concat"); |
| 10821 | goto error; |
| 10822 | } |
| 10823 | new_len = left_len + right_len; |
| 10824 | |
| 10825 | if (unicode_modifiable(left) |
| 10826 | && PyUnicode_CheckExact(right) |
| 10827 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10828 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10829 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10830 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10831 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10832 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10833 | { |
| 10834 | /* append inplace */ |
| 10835 | if (unicode_resize(p_left, new_len) != 0) { |
| 10836 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10837 | * deallocated so it cannot be put back into |
| 10838 | * 'variable'. The MemoryError is raised when there |
| 10839 | * is no value in 'variable', which might (very |
| 10840 | * remotely) be a cause of incompatibilities. |
| 10841 | */ |
| 10842 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10843 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10844 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10845 | copy_characters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10846 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10847 | else { |
| 10848 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10849 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
| 10850 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10851 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10852 | /* Concat the two Unicode strings */ |
| 10853 | res = PyUnicode_New(new_len, maxchar); |
| 10854 | if (res == NULL) |
| 10855 | goto error; |
| 10856 | copy_characters(res, 0, left, 0, left_len); |
| 10857 | copy_characters(res, left_len, right, 0, right_len); |
| 10858 | Py_DECREF(left); |
| 10859 | *p_left = res; |
| 10860 | } |
| 10861 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10862 | return; |
| 10863 | |
| 10864 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10865 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10866 | } |
| 10867 | |
| 10868 | void |
| 10869 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10870 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10871 | PyUnicode_Append(pleft, right); |
| 10872 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10873 | } |
| 10874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10875 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10878 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10879 | 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] | 10880 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10881 | |
| 10882 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10883 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10884 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10885 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10886 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10887 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10888 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10889 | int kind1, kind2, kind; |
| 10890 | void *buf1, *buf2; |
| 10891 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10892 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10893 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10894 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10895 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10896 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 | kind1 = PyUnicode_KIND(self); |
| 10898 | kind2 = PyUnicode_KIND(substring); |
| 10899 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10900 | buf1 = PyUnicode_DATA(self); |
| 10901 | buf2 = PyUnicode_DATA(substring); |
| 10902 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10903 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10904 | if (!buf1) { |
| 10905 | Py_DECREF(substring); |
| 10906 | return NULL; |
| 10907 | } |
| 10908 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10909 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10910 | if (!buf2) { |
| 10911 | Py_DECREF(substring); |
| 10912 | if (kind1 != kind) PyMem_Free(buf1); |
| 10913 | return NULL; |
| 10914 | } |
| 10915 | len1 = PyUnicode_GET_LENGTH(self); |
| 10916 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10917 | |
| 10918 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10919 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10920 | case PyUnicode_1BYTE_KIND: |
| 10921 | iresult = ucs1lib_count( |
| 10922 | ((Py_UCS1*)buf1) + start, end - start, |
| 10923 | buf2, len2, PY_SSIZE_T_MAX |
| 10924 | ); |
| 10925 | break; |
| 10926 | case PyUnicode_2BYTE_KIND: |
| 10927 | iresult = ucs2lib_count( |
| 10928 | ((Py_UCS2*)buf1) + start, end - start, |
| 10929 | buf2, len2, PY_SSIZE_T_MAX |
| 10930 | ); |
| 10931 | break; |
| 10932 | case PyUnicode_4BYTE_KIND: |
| 10933 | iresult = ucs4lib_count( |
| 10934 | ((Py_UCS4*)buf1) + start, end - start, |
| 10935 | buf2, len2, PY_SSIZE_T_MAX |
| 10936 | ); |
| 10937 | break; |
| 10938 | default: |
| 10939 | assert(0); iresult = 0; |
| 10940 | } |
| 10941 | |
| 10942 | result = PyLong_FromSsize_t(iresult); |
| 10943 | |
| 10944 | if (kind1 != kind) |
| 10945 | PyMem_Free(buf1); |
| 10946 | if (kind2 != kind) |
| 10947 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10948 | |
| 10949 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10951 | return result; |
| 10952 | } |
| 10953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10954 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10955 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10957 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10958 | 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] | 10959 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10960 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10961 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10962 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | |
| 10964 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10965 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10967 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | char *encoding = NULL; |
| 10969 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10970 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10971 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10972 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10974 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10975 | } |
| 10976 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10977 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10978 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10979 | \n\ |
| 10980 | 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] | 10981 | 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] | 10982 | |
| 10983 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10984 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10986 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10987 | Py_UCS4 ch; |
| 10988 | PyObject *u; |
| 10989 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10990 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10991 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10992 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 | |
| 10994 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10995 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10996 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10997 | if (PyUnicode_READY(self) == -1) |
| 10998 | return NULL; |
| 10999 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11000 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11001 | src_len = PyUnicode_GET_LENGTH(self); |
| 11002 | i = j = line_pos = 0; |
| 11003 | kind = PyUnicode_KIND(self); |
| 11004 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11005 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11006 | for (; i < src_len; i++) { |
| 11007 | ch = PyUnicode_READ(kind, src_data, i); |
| 11008 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11009 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11010 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11011 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11012 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11013 | goto overflow; |
| 11014 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11015 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11016 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11017 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11018 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11019 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11020 | goto overflow; |
| 11021 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11022 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11023 | if (ch == '\n' || ch == '\r') |
| 11024 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11025 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11026 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11027 | if (!found) |
| 11028 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11031 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | if (!u) |
| 11033 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11034 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11035 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11036 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11038 | for (; i < src_len; i++) { |
| 11039 | ch = PyUnicode_READ(kind, src_data, i); |
| 11040 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11041 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11042 | incr = tabsize - (line_pos % tabsize); |
| 11043 | line_pos += incr; |
| 11044 | while (incr--) { |
| 11045 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11046 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11047 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11048 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11049 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11050 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11051 | line_pos++; |
| 11052 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11053 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11054 | if (ch == '\n' || ch == '\r') |
| 11055 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11056 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11057 | } |
| 11058 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11059 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11060 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11061 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11062 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11063 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | } |
| 11065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11066 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11067 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | \n\ |
| 11069 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11070 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | arguments start and end are interpreted as in slice notation.\n\ |
| 11072 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11073 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | |
| 11075 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11076 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11078 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11079 | Py_ssize_t start; |
| 11080 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11081 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11082 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11083 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11084 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11086 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11087 | if (PyUnicode_READY(self) == -1) |
| 11088 | return NULL; |
| 11089 | if (PyUnicode_READY(substring) == -1) |
| 11090 | return NULL; |
| 11091 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11092 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | |
| 11094 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11096 | if (result == -2) |
| 11097 | return NULL; |
| 11098 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11099 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11100 | } |
| 11101 | |
| 11102 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11103 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11105 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11106 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11107 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11108 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | } |
| 11110 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11111 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11112 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11113 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11114 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11116 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11117 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | if (_PyUnicode_HASH(self) != -1) |
| 11120 | return _PyUnicode_HASH(self); |
| 11121 | if (PyUnicode_READY(self) == -1) |
| 11122 | return -1; |
| 11123 | len = PyUnicode_GET_LENGTH(self); |
| 11124 | |
| 11125 | /* The hash function as a macro, gets expanded three times below. */ |
| 11126 | #define HASH(P) \ |
| 11127 | x = (Py_uhash_t)*P << 7; \ |
| 11128 | while (--len >= 0) \ |
| 11129 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11130 | |
| 11131 | switch (PyUnicode_KIND(self)) { |
| 11132 | case PyUnicode_1BYTE_KIND: { |
| 11133 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11134 | HASH(c); |
| 11135 | break; |
| 11136 | } |
| 11137 | case PyUnicode_2BYTE_KIND: { |
| 11138 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11139 | HASH(s); |
| 11140 | break; |
| 11141 | } |
| 11142 | default: { |
| 11143 | Py_UCS4 *l; |
| 11144 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11145 | "Impossible switch case in unicode_hash"); |
| 11146 | l = PyUnicode_4BYTE_DATA(self); |
| 11147 | HASH(l); |
| 11148 | break; |
| 11149 | } |
| 11150 | } |
| 11151 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11152 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11153 | if (x == -1) |
| 11154 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11156 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11157 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11158 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11160 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11161 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11163 | 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] | 11164 | |
| 11165 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11166 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11167 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11168 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11169 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11170 | Py_ssize_t start; |
| 11171 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11173 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11174 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (PyUnicode_READY(self) == -1) |
| 11178 | return NULL; |
| 11179 | if (PyUnicode_READY(substring) == -1) |
| 11180 | return NULL; |
| 11181 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11182 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | |
| 11184 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11185 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | if (result == -2) |
| 11187 | return NULL; |
| 11188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | if (result < 0) { |
| 11190 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11191 | return NULL; |
| 11192 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11193 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11194 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | } |
| 11196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11197 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11198 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11200 | 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] | 11201 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | |
| 11203 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11204 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | Py_ssize_t i, length; |
| 11207 | int kind; |
| 11208 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | int cased; |
| 11210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11211 | if (PyUnicode_READY(self) == -1) |
| 11212 | return NULL; |
| 11213 | length = PyUnicode_GET_LENGTH(self); |
| 11214 | kind = PyUnicode_KIND(self); |
| 11215 | data = PyUnicode_DATA(self); |
| 11216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11217 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | if (length == 1) |
| 11219 | return PyBool_FromLong( |
| 11220 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11222 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11223 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11224 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11226 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | for (i = 0; i < length; i++) { |
| 11228 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11230 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11231 | return PyBool_FromLong(0); |
| 11232 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11233 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11235 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | } |
| 11237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11238 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11239 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11241 | 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] | 11242 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | |
| 11244 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11245 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11247 | Py_ssize_t i, length; |
| 11248 | int kind; |
| 11249 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | int cased; |
| 11251 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11252 | if (PyUnicode_READY(self) == -1) |
| 11253 | return NULL; |
| 11254 | length = PyUnicode_GET_LENGTH(self); |
| 11255 | kind = PyUnicode_KIND(self); |
| 11256 | data = PyUnicode_DATA(self); |
| 11257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11259 | if (length == 1) |
| 11260 | return PyBool_FromLong( |
| 11261 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11263 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11264 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11265 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | for (i = 0; i < length; i++) { |
| 11269 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11270 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11271 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11272 | return PyBool_FromLong(0); |
| 11273 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11274 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11276 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11277 | } |
| 11278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11279 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11280 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11282 | Return True if S is a titlecased string and there is at least one\n\ |
| 11283 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11284 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11285 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | |
| 11287 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11288 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11290 | Py_ssize_t i, length; |
| 11291 | int kind; |
| 11292 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | int cased, previous_is_cased; |
| 11294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | if (PyUnicode_READY(self) == -1) |
| 11296 | return NULL; |
| 11297 | length = PyUnicode_GET_LENGTH(self); |
| 11298 | kind = PyUnicode_KIND(self); |
| 11299 | data = PyUnicode_DATA(self); |
| 11300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11301 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | if (length == 1) { |
| 11303 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11304 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11305 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11306 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11308 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11310 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | cased = 0; |
| 11313 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | for (i = 0; i < length; i++) { |
| 11315 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11317 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11318 | if (previous_is_cased) |
| 11319 | return PyBool_FromLong(0); |
| 11320 | previous_is_cased = 1; |
| 11321 | cased = 1; |
| 11322 | } |
| 11323 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11324 | if (!previous_is_cased) |
| 11325 | return PyBool_FromLong(0); |
| 11326 | previous_is_cased = 1; |
| 11327 | cased = 1; |
| 11328 | } |
| 11329 | else |
| 11330 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11332 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | } |
| 11334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11335 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11336 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11337 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11338 | Return True if all characters in S are whitespace\n\ |
| 11339 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11340 | |
| 11341 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11342 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11343 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11344 | Py_ssize_t i, length; |
| 11345 | int kind; |
| 11346 | void *data; |
| 11347 | |
| 11348 | if (PyUnicode_READY(self) == -1) |
| 11349 | return NULL; |
| 11350 | length = PyUnicode_GET_LENGTH(self); |
| 11351 | kind = PyUnicode_KIND(self); |
| 11352 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | if (length == 1) |
| 11356 | return PyBool_FromLong( |
| 11357 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11359 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11360 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11361 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | for (i = 0; i < length; i++) { |
| 11364 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11365 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11366 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11368 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | } |
| 11370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11371 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11372 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11373 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11374 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11375 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11376 | |
| 11377 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11378 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11379 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11380 | Py_ssize_t i, length; |
| 11381 | int kind; |
| 11382 | void *data; |
| 11383 | |
| 11384 | if (PyUnicode_READY(self) == -1) |
| 11385 | return NULL; |
| 11386 | length = PyUnicode_GET_LENGTH(self); |
| 11387 | kind = PyUnicode_KIND(self); |
| 11388 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11389 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11390 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11391 | if (length == 1) |
| 11392 | return PyBool_FromLong( |
| 11393 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11394 | |
| 11395 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11397 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | for (i = 0; i < length; i++) { |
| 11400 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11401 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11402 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11403 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11404 | } |
| 11405 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11406 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11407 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11408 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11409 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11411 | |
| 11412 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11413 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11414 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | int kind; |
| 11416 | void *data; |
| 11417 | Py_ssize_t len, i; |
| 11418 | |
| 11419 | if (PyUnicode_READY(self) == -1) |
| 11420 | return NULL; |
| 11421 | |
| 11422 | kind = PyUnicode_KIND(self); |
| 11423 | data = PyUnicode_DATA(self); |
| 11424 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11425 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11426 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11427 | if (len == 1) { |
| 11428 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11429 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11430 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11431 | |
| 11432 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11433 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | for (i = 0; i < len; i++) { |
| 11437 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11438 | if (!Py_UNICODE_ISALNUM(ch)) |
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(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11445 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11446 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11447 | 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] | 11448 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | |
| 11450 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11451 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | Py_ssize_t i, length; |
| 11454 | int kind; |
| 11455 | void *data; |
| 11456 | |
| 11457 | if (PyUnicode_READY(self) == -1) |
| 11458 | return NULL; |
| 11459 | length = PyUnicode_GET_LENGTH(self); |
| 11460 | kind = PyUnicode_KIND(self); |
| 11461 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | if (length == 1) |
| 11465 | return PyBool_FromLong( |
| 11466 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11468 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11472 | for (i = 0; i < length; i++) { |
| 11473 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11474 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11476 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | } |
| 11478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11479 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11480 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11482 | Return True if all characters in S are digits\n\ |
| 11483 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | |
| 11485 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11486 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11488 | Py_ssize_t i, length; |
| 11489 | int kind; |
| 11490 | void *data; |
| 11491 | |
| 11492 | if (PyUnicode_READY(self) == -1) |
| 11493 | return NULL; |
| 11494 | length = PyUnicode_GET_LENGTH(self); |
| 11495 | kind = PyUnicode_KIND(self); |
| 11496 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | if (length == 1) { |
| 11500 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11501 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11502 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11504 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11507 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | for (i = 0; i < length; i++) { |
| 11509 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11510 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11512 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | } |
| 11514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11515 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11516 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11518 | Return True if there are only numeric characters in S,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | |
| 11521 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11522 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | Py_ssize_t i, length; |
| 11525 | int kind; |
| 11526 | void *data; |
| 11527 | |
| 11528 | if (PyUnicode_READY(self) == -1) |
| 11529 | return NULL; |
| 11530 | length = PyUnicode_GET_LENGTH(self); |
| 11531 | kind = PyUnicode_KIND(self); |
| 11532 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | if (length == 1) |
| 11536 | return PyBool_FromLong( |
| 11537 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11538 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11539 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11541 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11542 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | for (i = 0; i < length; i++) { |
| 11544 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11545 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11547 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11548 | } |
| 11549 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11550 | int |
| 11551 | PyUnicode_IsIdentifier(PyObject *self) |
| 11552 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | int kind; |
| 11554 | void *data; |
| 11555 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11556 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11557 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11558 | if (PyUnicode_READY(self) == -1) { |
| 11559 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11560 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11561 | } |
| 11562 | |
| 11563 | /* Special case for empty strings */ |
| 11564 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11565 | return 0; |
| 11566 | kind = PyUnicode_KIND(self); |
| 11567 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11568 | |
| 11569 | /* PEP 3131 says that the first character must be in |
| 11570 | XID_Start and subsequent characters in XID_Continue, |
| 11571 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11572 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11573 | letters, digits, underscore). However, given the current |
| 11574 | definition of XID_Start and XID_Continue, it is sufficient |
| 11575 | to check just for these, except that _ must be allowed |
| 11576 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11577 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11578 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11579 | return 0; |
| 11580 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11581 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11582 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11583 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11584 | return 1; |
| 11585 | } |
| 11586 | |
| 11587 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11588 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11589 | \n\ |
| 11590 | Return True if S is a valid identifier according\n\ |
| 11591 | to the language definition."); |
| 11592 | |
| 11593 | static PyObject* |
| 11594 | unicode_isidentifier(PyObject *self) |
| 11595 | { |
| 11596 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11597 | } |
| 11598 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11599 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11600 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11601 | \n\ |
| 11602 | Return True if all characters in S are considered\n\ |
| 11603 | printable in repr() or S is empty, False otherwise."); |
| 11604 | |
| 11605 | static PyObject* |
| 11606 | unicode_isprintable(PyObject *self) |
| 11607 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11608 | Py_ssize_t i, length; |
| 11609 | int kind; |
| 11610 | void *data; |
| 11611 | |
| 11612 | if (PyUnicode_READY(self) == -1) |
| 11613 | return NULL; |
| 11614 | length = PyUnicode_GET_LENGTH(self); |
| 11615 | kind = PyUnicode_KIND(self); |
| 11616 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11617 | |
| 11618 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11619 | if (length == 1) |
| 11620 | return PyBool_FromLong( |
| 11621 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11623 | for (i = 0; i < length; i++) { |
| 11624 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11625 | Py_RETURN_FALSE; |
| 11626 | } |
| 11627 | } |
| 11628 | Py_RETURN_TRUE; |
| 11629 | } |
| 11630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11631 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11632 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11633 | \n\ |
| 11634 | 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] | 11635 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11636 | |
| 11637 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11638 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11640 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11641 | } |
| 11642 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11643 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11644 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11646 | if (PyUnicode_READY(self) == -1) |
| 11647 | return -1; |
| 11648 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11649 | } |
| 11650 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11651 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11652 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11654 | 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] | 11655 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | |
| 11657 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11658 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11660 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11661 | Py_UCS4 fillchar = ' '; |
| 11662 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11663 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11664 | return NULL; |
| 11665 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11666 | if (PyUnicode_READY(self) < 0) |
| 11667 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11668 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11669 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11670 | return unicode_result_unchanged(self); |
| 11671 | |
| 11672 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11675 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11676 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11678 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | |
| 11680 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11681 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11682 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11683 | return fixup(self, fixlower); |
| 11684 | } |
| 11685 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11686 | #define LEFTSTRIP 0 |
| 11687 | #define RIGHTSTRIP 1 |
| 11688 | #define BOTHSTRIP 2 |
| 11689 | |
| 11690 | /* Arrays indexed by above */ |
| 11691 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11692 | |
| 11693 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11694 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11695 | /* externally visible for str.strip(unicode) */ |
| 11696 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11697 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11698 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | void *data; |
| 11700 | int kind; |
| 11701 | Py_ssize_t i, j, len; |
| 11702 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11704 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11705 | return NULL; |
| 11706 | |
| 11707 | kind = PyUnicode_KIND(self); |
| 11708 | data = PyUnicode_DATA(self); |
| 11709 | len = PyUnicode_GET_LENGTH(self); |
| 11710 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11711 | PyUnicode_DATA(sepobj), |
| 11712 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11713 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11714 | i = 0; |
| 11715 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11716 | while (i < len && |
| 11717 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11718 | i++; |
| 11719 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11720 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11721 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11722 | j = len; |
| 11723 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11724 | do { |
| 11725 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11726 | } while (j >= i && |
| 11727 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11728 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11729 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11730 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11731 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | } |
| 11733 | |
| 11734 | PyObject* |
| 11735 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11736 | { |
| 11737 | unsigned char *data; |
| 11738 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11739 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11740 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11741 | if (PyUnicode_READY(self) == -1) |
| 11742 | return NULL; |
| 11743 | |
| 11744 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11745 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11746 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11747 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11748 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11749 | length = end - start; |
| 11750 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11751 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11753 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11754 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11755 | return NULL; |
| 11756 | } |
| 11757 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11758 | if (PyUnicode_IS_ASCII(self)) { |
| 11759 | kind = PyUnicode_KIND(self); |
| 11760 | data = PyUnicode_1BYTE_DATA(self); |
| 11761 | return unicode_fromascii(data + start, length); |
| 11762 | } |
| 11763 | else { |
| 11764 | kind = PyUnicode_KIND(self); |
| 11765 | data = PyUnicode_1BYTE_DATA(self); |
| 11766 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11767 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11768 | length); |
| 11769 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11770 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | |
| 11772 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11773 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | int kind; |
| 11776 | void *data; |
| 11777 | Py_ssize_t len, i, j; |
| 11778 | |
| 11779 | if (PyUnicode_READY(self) == -1) |
| 11780 | return NULL; |
| 11781 | |
| 11782 | kind = PyUnicode_KIND(self); |
| 11783 | data = PyUnicode_DATA(self); |
| 11784 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11785 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11786 | i = 0; |
| 11787 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11788 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11789 | i++; |
| 11790 | } |
| 11791 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11792 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11793 | j = len; |
| 11794 | if (striptype != LEFTSTRIP) { |
| 11795 | do { |
| 11796 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11797 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11798 | j++; |
| 11799 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11800 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11801 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | } |
| 11803 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11804 | |
| 11805 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11806 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11807 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11808 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11809 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11810 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11811 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11812 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11813 | if (sep != NULL && sep != Py_None) { |
| 11814 | if (PyUnicode_Check(sep)) |
| 11815 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11816 | else { |
| 11817 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11818 | "%s arg must be None or str", |
| 11819 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11820 | return NULL; |
| 11821 | } |
| 11822 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11823 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11824 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11825 | } |
| 11826 | |
| 11827 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11828 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11829 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11830 | \n\ |
| 11831 | Return a copy of the string S with leading and trailing\n\ |
| 11832 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11833 | 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] | 11834 | |
| 11835 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11836 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11837 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11838 | if (PyTuple_GET_SIZE(args) == 0) |
| 11839 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11840 | else |
| 11841 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11842 | } |
| 11843 | |
| 11844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11845 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11846 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11847 | \n\ |
| 11848 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11849 | 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] | 11850 | |
| 11851 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11852 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11853 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11854 | if (PyTuple_GET_SIZE(args) == 0) |
| 11855 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11856 | else |
| 11857 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11858 | } |
| 11859 | |
| 11860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11861 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11862 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11863 | \n\ |
| 11864 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11865 | 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] | 11866 | |
| 11867 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11868 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11869 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11870 | if (PyTuple_GET_SIZE(args) == 0) |
| 11871 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11872 | else |
| 11873 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11874 | } |
| 11875 | |
| 11876 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11878 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11880 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11881 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11883 | if (len < 1) { |
| 11884 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11885 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11886 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11888 | /* no repeat, return original string */ |
| 11889 | if (len == 1) |
| 11890 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11891 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11892 | if (PyUnicode_READY(str) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | return NULL; |
| 11894 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11895 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11896 | PyErr_SetString(PyExc_OverflowError, |
| 11897 | "repeated string is too long"); |
| 11898 | return NULL; |
| 11899 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11901 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11902 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11903 | if (!u) |
| 11904 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11905 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11907 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11908 | const int kind = PyUnicode_KIND(str); |
| 11909 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11910 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11911 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11912 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11913 | } |
| 11914 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11915 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11916 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11917 | ucs2[n] = fill_char; |
| 11918 | } else { |
| 11919 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11920 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11921 | for (n = 0; n < len; ++n) |
| 11922 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | } |
| 11925 | else { |
| 11926 | /* number of characters copied this far */ |
| 11927 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11928 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11929 | char *to = (char *) PyUnicode_DATA(u); |
| 11930 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11931 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11932 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11933 | n = (done <= nchars-done) ? done : nchars-done; |
| 11934 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11935 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11936 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11937 | } |
| 11938 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11939 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11940 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | } |
| 11942 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11943 | PyObject * |
| 11944 | PyUnicode_Replace(PyObject *obj, |
| 11945 | PyObject *subobj, |
| 11946 | PyObject *replobj, |
| 11947 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11948 | { |
| 11949 | PyObject *self; |
| 11950 | PyObject *str1; |
| 11951 | PyObject *str2; |
| 11952 | PyObject *result; |
| 11953 | |
| 11954 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11955 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11958 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11959 | Py_DECREF(self); |
| 11960 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11961 | } |
| 11962 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11963 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11964 | Py_DECREF(self); |
| 11965 | Py_DECREF(str1); |
| 11966 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11967 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11968 | if (PyUnicode_READY(self) == -1 || |
| 11969 | PyUnicode_READY(str1) == -1 || |
| 11970 | PyUnicode_READY(str2) == -1) |
| 11971 | result = NULL; |
| 11972 | else |
| 11973 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | Py_DECREF(self); |
| 11975 | Py_DECREF(str1); |
| 11976 | Py_DECREF(str2); |
| 11977 | return result; |
| 11978 | } |
| 11979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11980 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11981 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | \n\ |
| 11983 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11984 | old replaced by new. If the optional argument count is\n\ |
| 11985 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11986 | |
| 11987 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11988 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11989 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | PyObject *str1; |
| 11991 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11992 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11993 | PyObject *result; |
| 11994 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11995 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11996 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11997 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11998 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12000 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12001 | return NULL; |
| 12002 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12003 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | Py_DECREF(str1); |
| 12005 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12006 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12007 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12008 | result = NULL; |
| 12009 | else |
| 12010 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12011 | |
| 12012 | Py_DECREF(str1); |
| 12013 | Py_DECREF(str2); |
| 12014 | return result; |
| 12015 | } |
| 12016 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12017 | static PyObject * |
| 12018 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12019 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12020 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12021 | Py_ssize_t isize; |
| 12022 | Py_ssize_t osize, squote, dquote, i, o; |
| 12023 | Py_UCS4 max, quote; |
| 12024 | int ikind, okind; |
| 12025 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12028 | return NULL; |
| 12029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12031 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12033 | /* Compute length of output, quote characters, and |
| 12034 | maximum character */ |
| 12035 | osize = 2; /* quotes */ |
| 12036 | max = 127; |
| 12037 | squote = dquote = 0; |
| 12038 | ikind = PyUnicode_KIND(unicode); |
| 12039 | for (i = 0; i < isize; i++) { |
| 12040 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12041 | switch (ch) { |
| 12042 | case '\'': squote++; osize++; break; |
| 12043 | case '"': dquote++; osize++; break; |
| 12044 | case '\\': case '\t': case '\r': case '\n': |
| 12045 | osize += 2; break; |
| 12046 | default: |
| 12047 | /* Fast-path ASCII */ |
| 12048 | if (ch < ' ' || ch == 0x7f) |
| 12049 | osize += 4; /* \xHH */ |
| 12050 | else if (ch < 0x7f) |
| 12051 | osize++; |
| 12052 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12053 | osize++; |
| 12054 | max = ch > max ? ch : max; |
| 12055 | } |
| 12056 | else if (ch < 0x100) |
| 12057 | osize += 4; /* \xHH */ |
| 12058 | else if (ch < 0x10000) |
| 12059 | osize += 6; /* \uHHHH */ |
| 12060 | else |
| 12061 | osize += 10; /* \uHHHHHHHH */ |
| 12062 | } |
| 12063 | } |
| 12064 | |
| 12065 | quote = '\''; |
| 12066 | if (squote) { |
| 12067 | if (dquote) |
| 12068 | /* Both squote and dquote present. Use squote, |
| 12069 | and escape them */ |
| 12070 | osize += squote; |
| 12071 | else |
| 12072 | quote = '"'; |
| 12073 | } |
| 12074 | |
| 12075 | repr = PyUnicode_New(osize, max); |
| 12076 | if (repr == NULL) |
| 12077 | return NULL; |
| 12078 | okind = PyUnicode_KIND(repr); |
| 12079 | odata = PyUnicode_DATA(repr); |
| 12080 | |
| 12081 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12082 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12083 | |
| 12084 | for (i = 0, o = 1; i < isize; i++) { |
| 12085 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12086 | |
| 12087 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12088 | if ((ch == quote) || (ch == '\\')) { |
| 12089 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12090 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12091 | continue; |
| 12092 | } |
| 12093 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12094 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12095 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12096 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12097 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12098 | } |
| 12099 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12100 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12101 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12102 | } |
| 12103 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12104 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12106 | } |
| 12107 | |
| 12108 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12109 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12110 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12111 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12112 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12113 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12114 | } |
| 12115 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12116 | /* Copy ASCII characters as-is */ |
| 12117 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12118 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12119 | } |
| 12120 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12121 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12122 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12123 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12124 | (categories Z* and C* except ASCII space) |
| 12125 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12126 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12127 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | if (ch <= 0xff) { |
| 12129 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12130 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12131 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12132 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12133 | } |
| 12134 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12135 | else if (ch >= 0x10000) { |
| 12136 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12137 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12138 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12139 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12140 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12141 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12142 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12143 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12144 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12145 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12146 | } |
| 12147 | /* Map 16-bit characters to '\uxxxx' */ |
| 12148 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12149 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12150 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12151 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12152 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12153 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12154 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12155 | } |
| 12156 | } |
| 12157 | /* Copy characters as-is */ |
| 12158 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12159 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12160 | } |
| 12161 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12162 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12163 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12164 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12165 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | } |
| 12167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12168 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12169 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | \n\ |
| 12171 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12172 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | arguments start and end are interpreted as in slice notation.\n\ |
| 12174 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12175 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12176 | |
| 12177 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12178 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12179 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12180 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12181 | Py_ssize_t start; |
| 12182 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12183 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12184 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12185 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12186 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12187 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | if (PyUnicode_READY(self) == -1) |
| 12190 | return NULL; |
| 12191 | if (PyUnicode_READY(substring) == -1) |
| 12192 | return NULL; |
| 12193 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12194 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | |
| 12196 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12197 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12198 | if (result == -2) |
| 12199 | return NULL; |
| 12200 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12201 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | } |
| 12203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12204 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12205 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12207 | 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] | 12208 | |
| 12209 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12210 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12212 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12213 | Py_ssize_t start; |
| 12214 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12215 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12217 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12218 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12219 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | if (PyUnicode_READY(self) == -1) |
| 12222 | return NULL; |
| 12223 | if (PyUnicode_READY(substring) == -1) |
| 12224 | return NULL; |
| 12225 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12226 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | |
| 12228 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12230 | if (result == -2) |
| 12231 | return NULL; |
| 12232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12233 | if (result < 0) { |
| 12234 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12235 | return NULL; |
| 12236 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12237 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12238 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | } |
| 12240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12241 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12242 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12244 | 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] | 12245 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12246 | |
| 12247 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12248 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12250 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12251 | Py_UCS4 fillchar = ' '; |
| 12252 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12253 | 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] | 12254 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12255 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12256 | if (PyUnicode_READY(self) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | return NULL; |
| 12258 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12259 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12260 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12262 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | } |
| 12264 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12265 | PyObject * |
| 12266 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | { |
| 12268 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12270 | s = PyUnicode_FromObject(s); |
| 12271 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12272 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12273 | if (sep != NULL) { |
| 12274 | sep = PyUnicode_FromObject(sep); |
| 12275 | if (sep == NULL) { |
| 12276 | Py_DECREF(s); |
| 12277 | return NULL; |
| 12278 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | } |
| 12280 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12281 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12282 | |
| 12283 | Py_DECREF(s); |
| 12284 | Py_XDECREF(sep); |
| 12285 | return result; |
| 12286 | } |
| 12287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12288 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12289 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12290 | \n\ |
| 12291 | Return a list of the words in S, using sep as the\n\ |
| 12292 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12293 | 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] | 12294 | whitespace string is a separator and empty strings are\n\ |
| 12295 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | |
| 12297 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12298 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | { |
| 12300 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12301 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12302 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12303 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | return NULL; |
| 12305 | |
| 12306 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12309 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12310 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12311 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | } |
| 12313 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12314 | PyObject * |
| 12315 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12316 | { |
| 12317 | PyObject* str_obj; |
| 12318 | PyObject* sep_obj; |
| 12319 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12320 | int kind1, kind2, kind; |
| 12321 | void *buf1 = NULL, *buf2 = NULL; |
| 12322 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12323 | |
| 12324 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12325 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12326 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12327 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12328 | if (!sep_obj) { |
| 12329 | Py_DECREF(str_obj); |
| 12330 | return NULL; |
| 12331 | } |
| 12332 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12333 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12334 | Py_DECREF(str_obj); |
| 12335 | return NULL; |
| 12336 | } |
| 12337 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12338 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12340 | kind = Py_MAX(kind1, kind2); |
| 12341 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12342 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12343 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | if (!buf1) |
| 12345 | goto onError; |
| 12346 | buf2 = PyUnicode_DATA(sep_obj); |
| 12347 | if (kind2 != kind) |
| 12348 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12349 | if (!buf2) |
| 12350 | goto onError; |
| 12351 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12352 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12353 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12354 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12355 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12356 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12357 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12358 | else |
| 12359 | 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] | 12360 | break; |
| 12361 | case PyUnicode_2BYTE_KIND: |
| 12362 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12363 | break; |
| 12364 | case PyUnicode_4BYTE_KIND: |
| 12365 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12366 | break; |
| 12367 | default: |
| 12368 | assert(0); |
| 12369 | out = 0; |
| 12370 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12371 | |
| 12372 | Py_DECREF(sep_obj); |
| 12373 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12374 | if (kind1 != kind) |
| 12375 | PyMem_Free(buf1); |
| 12376 | if (kind2 != kind) |
| 12377 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12378 | |
| 12379 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12380 | onError: |
| 12381 | Py_DECREF(sep_obj); |
| 12382 | Py_DECREF(str_obj); |
| 12383 | if (kind1 != kind && buf1) |
| 12384 | PyMem_Free(buf1); |
| 12385 | if (kind2 != kind && buf2) |
| 12386 | PyMem_Free(buf2); |
| 12387 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12388 | } |
| 12389 | |
| 12390 | |
| 12391 | PyObject * |
| 12392 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12393 | { |
| 12394 | PyObject* str_obj; |
| 12395 | PyObject* sep_obj; |
| 12396 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12397 | int kind1, kind2, kind; |
| 12398 | void *buf1 = NULL, *buf2 = NULL; |
| 12399 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12400 | |
| 12401 | str_obj = PyUnicode_FromObject(str_in); |
| 12402 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12403 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12404 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12405 | if (!sep_obj) { |
| 12406 | Py_DECREF(str_obj); |
| 12407 | return NULL; |
| 12408 | } |
| 12409 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | kind1 = PyUnicode_KIND(str_in); |
| 12411 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12412 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12413 | buf1 = PyUnicode_DATA(str_in); |
| 12414 | if (kind1 != kind) |
| 12415 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12416 | if (!buf1) |
| 12417 | goto onError; |
| 12418 | buf2 = PyUnicode_DATA(sep_obj); |
| 12419 | if (kind2 != kind) |
| 12420 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12421 | if (!buf2) |
| 12422 | goto onError; |
| 12423 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12424 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12425 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12426 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12428 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12429 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12430 | else |
| 12431 | 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] | 12432 | break; |
| 12433 | case PyUnicode_2BYTE_KIND: |
| 12434 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12435 | break; |
| 12436 | case PyUnicode_4BYTE_KIND: |
| 12437 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12438 | break; |
| 12439 | default: |
| 12440 | assert(0); |
| 12441 | out = 0; |
| 12442 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12443 | |
| 12444 | Py_DECREF(sep_obj); |
| 12445 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12446 | if (kind1 != kind) |
| 12447 | PyMem_Free(buf1); |
| 12448 | if (kind2 != kind) |
| 12449 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12450 | |
| 12451 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | onError: |
| 12453 | Py_DECREF(sep_obj); |
| 12454 | Py_DECREF(str_obj); |
| 12455 | if (kind1 != kind && buf1) |
| 12456 | PyMem_Free(buf1); |
| 12457 | if (kind2 != kind && buf2) |
| 12458 | PyMem_Free(buf2); |
| 12459 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12460 | } |
| 12461 | |
| 12462 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12463 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12464 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12465 | 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] | 12466 | 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] | 12467 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12468 | |
| 12469 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12470 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12471 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12472 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12473 | } |
| 12474 | |
| 12475 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12476 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12477 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12478 | 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] | 12479 | 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] | 12480 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12481 | |
| 12482 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12483 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12484 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12485 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12486 | } |
| 12487 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12488 | PyObject * |
| 12489 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12490 | { |
| 12491 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12492 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12493 | s = PyUnicode_FromObject(s); |
| 12494 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12495 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12496 | if (sep != NULL) { |
| 12497 | sep = PyUnicode_FromObject(sep); |
| 12498 | if (sep == NULL) { |
| 12499 | Py_DECREF(s); |
| 12500 | return NULL; |
| 12501 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12502 | } |
| 12503 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12504 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12505 | |
| 12506 | Py_DECREF(s); |
| 12507 | Py_XDECREF(sep); |
| 12508 | return result; |
| 12509 | } |
| 12510 | |
| 12511 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12512 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12513 | \n\ |
| 12514 | Return a list of the words in S, using sep as the\n\ |
| 12515 | delimiter string, starting at the end of the string and\n\ |
| 12516 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12517 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12518 | is a separator."); |
| 12519 | |
| 12520 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12521 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12522 | { |
| 12523 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12524 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12525 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12526 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12527 | return NULL; |
| 12528 | |
| 12529 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12530 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12531 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12532 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12533 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12534 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12535 | } |
| 12536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12537 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12538 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | \n\ |
| 12540 | 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] | 12541 | 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] | 12542 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
| 12544 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12545 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12547 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12548 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12550 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12551 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | return NULL; |
| 12553 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12554 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12555 | } |
| 12556 | |
| 12557 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12558 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12559 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12560 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | } |
| 12562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12563 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12564 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12565 | \n\ |
| 12566 | 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] | 12567 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12568 | |
| 12569 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12570 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12571 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | return fixup(self, fixswapcase); |
| 12573 | } |
| 12574 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12575 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12576 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12577 | \n\ |
| 12578 | Return a translation table usable for str.translate().\n\ |
| 12579 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12580 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12581 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12582 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12583 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12584 | character at the same position in y. If there is a third argument, it\n\ |
| 12585 | must be a string, whose characters will be mapped to None in the result."); |
| 12586 | |
| 12587 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12588 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12589 | { |
| 12590 | PyObject *x, *y = NULL, *z = NULL; |
| 12591 | PyObject *new = NULL, *key, *value; |
| 12592 | Py_ssize_t i = 0; |
| 12593 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12594 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12595 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12596 | return NULL; |
| 12597 | new = PyDict_New(); |
| 12598 | if (!new) |
| 12599 | return NULL; |
| 12600 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12601 | int x_kind, y_kind, z_kind; |
| 12602 | void *x_data, *y_data, *z_data; |
| 12603 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12604 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12605 | if (!PyUnicode_Check(x)) { |
| 12606 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12607 | "be a string if there is a second argument"); |
| 12608 | goto err; |
| 12609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12610 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12611 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12612 | "arguments must have equal length"); |
| 12613 | goto err; |
| 12614 | } |
| 12615 | /* 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] | 12616 | x_kind = PyUnicode_KIND(x); |
| 12617 | y_kind = PyUnicode_KIND(y); |
| 12618 | x_data = PyUnicode_DATA(x); |
| 12619 | y_data = PyUnicode_DATA(y); |
| 12620 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12621 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12622 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12623 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12624 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12625 | if (!value) { |
| 12626 | Py_DECREF(key); |
| 12627 | goto err; |
| 12628 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12629 | res = PyDict_SetItem(new, key, value); |
| 12630 | Py_DECREF(key); |
| 12631 | Py_DECREF(value); |
| 12632 | if (res < 0) |
| 12633 | goto err; |
| 12634 | } |
| 12635 | /* create entries for deleting chars in z */ |
| 12636 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12637 | z_kind = PyUnicode_KIND(z); |
| 12638 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12639 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12640 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12641 | if (!key) |
| 12642 | goto err; |
| 12643 | res = PyDict_SetItem(new, key, Py_None); |
| 12644 | Py_DECREF(key); |
| 12645 | if (res < 0) |
| 12646 | goto err; |
| 12647 | } |
| 12648 | } |
| 12649 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12650 | int kind; |
| 12651 | void *data; |
| 12652 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12653 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12654 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12655 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12656 | "to maketrans it must be a dict"); |
| 12657 | goto err; |
| 12658 | } |
| 12659 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12660 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12661 | if (PyUnicode_Check(key)) { |
| 12662 | /* convert string keys to integer keys */ |
| 12663 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12664 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12665 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12666 | "table must be of length 1"); |
| 12667 | goto err; |
| 12668 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12669 | kind = PyUnicode_KIND(key); |
| 12670 | data = PyUnicode_DATA(key); |
| 12671 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12672 | if (!newkey) |
| 12673 | goto err; |
| 12674 | res = PyDict_SetItem(new, newkey, value); |
| 12675 | Py_DECREF(newkey); |
| 12676 | if (res < 0) |
| 12677 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12678 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12679 | /* just keep integer keys */ |
| 12680 | if (PyDict_SetItem(new, key, value) < 0) |
| 12681 | goto err; |
| 12682 | } else { |
| 12683 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12684 | "be strings or integers"); |
| 12685 | goto err; |
| 12686 | } |
| 12687 | } |
| 12688 | } |
| 12689 | return new; |
| 12690 | err: |
| 12691 | Py_DECREF(new); |
| 12692 | return NULL; |
| 12693 | } |
| 12694 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12695 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12696 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | \n\ |
| 12698 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12699 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12700 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12701 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12702 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12703 | |
| 12704 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12706 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12707 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12708 | } |
| 12709 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12710 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12711 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12712 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12713 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12714 | |
| 12715 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12716 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12717 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12718 | return fixup(self, fixupper); |
| 12719 | } |
| 12720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12721 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12722 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12724 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12725 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12726 | |
| 12727 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12728 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12730 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12731 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12732 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12733 | int kind; |
| 12734 | void *data; |
| 12735 | Py_UCS4 chr; |
| 12736 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12737 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12738 | return NULL; |
| 12739 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12740 | if (PyUnicode_READY(self) < 0) |
| 12741 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12742 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12743 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12744 | return unicode_result_unchanged(self); |
| 12745 | |
| 12746 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | |
| 12748 | u = pad(self, fill, 0, '0'); |
| 12749 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12750 | if (u == NULL) |
| 12751 | return NULL; |
| 12752 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12753 | kind = PyUnicode_KIND(u); |
| 12754 | data = PyUnicode_DATA(u); |
| 12755 | chr = PyUnicode_READ(kind, data, fill); |
| 12756 | |
| 12757 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12758 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12759 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12760 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12761 | } |
| 12762 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12763 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12764 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12765 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12766 | |
| 12767 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12768 | static PyObject * |
| 12769 | unicode__decimal2ascii(PyObject *self) |
| 12770 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12771 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12772 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12773 | #endif |
| 12774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12775 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12776 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12777 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12778 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12779 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12780 | With optional end, stop comparing S at that position.\n\ |
| 12781 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12782 | |
| 12783 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12784 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12785 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12786 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12787 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12788 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12789 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12790 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12791 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12792 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12793 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12794 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12795 | if (PyTuple_Check(subobj)) { |
| 12796 | Py_ssize_t i; |
| 12797 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12798 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12799 | if (substring == NULL) |
| 12800 | return NULL; |
| 12801 | result = tailmatch(self, substring, start, end, -1); |
| 12802 | Py_DECREF(substring); |
| 12803 | if (result) { |
| 12804 | Py_RETURN_TRUE; |
| 12805 | } |
| 12806 | } |
| 12807 | /* nothing matched */ |
| 12808 | Py_RETURN_FALSE; |
| 12809 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12810 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12811 | if (substring == NULL) { |
| 12812 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12813 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12814 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12815 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12816 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12817 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12818 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12819 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12820 | } |
| 12821 | |
| 12822 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12823 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12825 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12826 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12827 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12828 | With optional end, stop comparing S at that position.\n\ |
| 12829 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12830 | |
| 12831 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12832 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12833 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12834 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12835 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12836 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12837 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12838 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12839 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12841 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12842 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12843 | if (PyTuple_Check(subobj)) { |
| 12844 | Py_ssize_t i; |
| 12845 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12846 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12847 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12848 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12849 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12850 | result = tailmatch(self, substring, start, end, +1); |
| 12851 | Py_DECREF(substring); |
| 12852 | if (result) { |
| 12853 | Py_RETURN_TRUE; |
| 12854 | } |
| 12855 | } |
| 12856 | Py_RETURN_FALSE; |
| 12857 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12858 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12859 | if (substring == NULL) { |
| 12860 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12861 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12862 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12863 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12864 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12865 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12866 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12867 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12868 | } |
| 12869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12870 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12871 | |
| 12872 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12873 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12874 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12875 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12876 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12877 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12878 | PyDoc_STRVAR(format_map__doc__, |
| 12879 | "S.format_map(mapping) -> str\n\ |
| 12880 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12881 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12882 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12883 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12884 | static PyObject * |
| 12885 | unicode__format__(PyObject* self, PyObject* args) |
| 12886 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12887 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12888 | |
| 12889 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12890 | return NULL; |
| 12891 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12892 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12893 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12894 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12895 | } |
| 12896 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12897 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12898 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12899 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12900 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12901 | |
| 12902 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12903 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12904 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12905 | Py_ssize_t size; |
| 12906 | |
| 12907 | /* If it's a compact object, account for base structure + |
| 12908 | character data. */ |
| 12909 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12910 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12911 | else if (PyUnicode_IS_COMPACT(v)) |
| 12912 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12913 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12914 | else { |
| 12915 | /* If it is a two-block object, account for base object, and |
| 12916 | for character block if present. */ |
| 12917 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12918 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12920 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12921 | } |
| 12922 | /* 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] | 12923 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12924 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12925 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12926 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12927 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12928 | |
| 12929 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12930 | } |
| 12931 | |
| 12932 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12933 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12934 | |
| 12935 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12936 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12937 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 12938 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12939 | if (!copy) |
| 12940 | return NULL; |
| 12941 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12942 | } |
| 12943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12944 | static PyMethodDef unicode_methods[] = { |
| 12945 | |
| 12946 | /* Order is according to common usage: often used methods should |
| 12947 | appear first, since lookup is done sequentially. */ |
| 12948 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12949 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12950 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12951 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12952 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12953 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12954 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12955 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12956 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12957 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12958 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12959 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12960 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12961 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12962 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12963 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12964 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12965 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12966 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12967 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12968 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12969 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12970 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12971 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12972 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12973 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12974 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12975 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12976 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12977 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12978 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12979 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12980 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12981 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12982 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12983 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12984 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12985 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12986 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12987 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12988 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12989 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12990 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12991 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12992 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12993 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12994 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12995 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12996 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12997 | #endif |
| 12998 | |
| 12999 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13000 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13001 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13002 | #endif |
| 13003 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13004 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13005 | {NULL, NULL} |
| 13006 | }; |
| 13007 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13008 | static PyObject * |
| 13009 | unicode_mod(PyObject *v, PyObject *w) |
| 13010 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13011 | if (!PyUnicode_Check(v)) |
| 13012 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13013 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13014 | } |
| 13015 | |
| 13016 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13017 | 0, /*nb_add*/ |
| 13018 | 0, /*nb_subtract*/ |
| 13019 | 0, /*nb_multiply*/ |
| 13020 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13021 | }; |
| 13022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13023 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13024 | (lenfunc) unicode_length, /* sq_length */ |
| 13025 | PyUnicode_Concat, /* sq_concat */ |
| 13026 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13027 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13028 | 0, /* sq_slice */ |
| 13029 | 0, /* sq_ass_item */ |
| 13030 | 0, /* sq_ass_slice */ |
| 13031 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13032 | }; |
| 13033 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13034 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13035 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13036 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13037 | if (PyUnicode_READY(self) == -1) |
| 13038 | return NULL; |
| 13039 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13040 | if (PyIndex_Check(item)) { |
| 13041 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13042 | if (i == -1 && PyErr_Occurred()) |
| 13043 | return NULL; |
| 13044 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13045 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13046 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13047 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13048 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13049 | PyObject *result; |
| 13050 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13051 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13052 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13054 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13055 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13056 | return NULL; |
| 13057 | } |
| 13058 | |
| 13059 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13060 | Py_INCREF(unicode_empty); |
| 13061 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13062 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13063 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13064 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13065 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13066 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13067 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13068 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13069 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13070 | src_kind = PyUnicode_KIND(self); |
| 13071 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13072 | if (!PyUnicode_IS_ASCII(self)) { |
| 13073 | kind_limit = kind_maxchar_limit(src_kind); |
| 13074 | max_char = 0; |
| 13075 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13076 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13077 | if (ch > max_char) { |
| 13078 | max_char = ch; |
| 13079 | if (max_char >= kind_limit) |
| 13080 | break; |
| 13081 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13082 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13083 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13084 | else |
| 13085 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13086 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13087 | if (result == NULL) |
| 13088 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13089 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13090 | dest_data = PyUnicode_DATA(result); |
| 13091 | |
| 13092 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13093 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13094 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13095 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13096 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13097 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13098 | } else { |
| 13099 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13100 | return NULL; |
| 13101 | } |
| 13102 | } |
| 13103 | |
| 13104 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13105 | (lenfunc)unicode_length, /* mp_length */ |
| 13106 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13107 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13108 | }; |
| 13109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | /* Helpers for PyUnicode_Format() */ |
| 13112 | |
| 13113 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13114 | 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] | 13115 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13116 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13117 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13118 | (*p_argidx)++; |
| 13119 | if (arglen < 0) |
| 13120 | return args; |
| 13121 | else |
| 13122 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13123 | } |
| 13124 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13125 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13126 | return NULL; |
| 13127 | } |
| 13128 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13129 | /* 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] | 13130 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13131 | static PyObject * |
| 13132 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13133 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13134 | char *p; |
| 13135 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13136 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13137 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13138 | x = PyFloat_AsDouble(v); |
| 13139 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13140 | return NULL; |
| 13141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13142 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13143 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13144 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13145 | p = PyOS_double_to_string(x, type, prec, |
| 13146 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13147 | if (p == NULL) |
| 13148 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13149 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13150 | PyMem_Free(p); |
| 13151 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13152 | } |
| 13153 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13154 | static PyObject* |
| 13155 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13156 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13157 | char *buf; |
| 13158 | int len; |
| 13159 | PyObject *str; /* temporary string object. */ |
| 13160 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13161 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13162 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13163 | if (!str) |
| 13164 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13165 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13166 | Py_DECREF(str); |
| 13167 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13168 | } |
| 13169 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13170 | static Py_UCS4 |
| 13171 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13172 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13173 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13174 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13175 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13176 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13177 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13178 | goto onError; |
| 13179 | } |
| 13180 | else { |
| 13181 | /* Integer input truncated to a character */ |
| 13182 | long x; |
| 13183 | x = PyLong_AsLong(v); |
| 13184 | if (x == -1 && PyErr_Occurred()) |
| 13185 | goto onError; |
| 13186 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13187 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13188 | PyErr_SetString(PyExc_OverflowError, |
| 13189 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13190 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13191 | } |
| 13192 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13193 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13194 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13196 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13197 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13198 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13199 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13200 | } |
| 13201 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13202 | static int |
| 13203 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13204 | { |
| 13205 | int r; |
| 13206 | assert(count > 0); |
| 13207 | assert(PyUnicode_Check(obj)); |
| 13208 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13209 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13210 | if (repeated == NULL) |
| 13211 | return -1; |
| 13212 | r = _PyAccu_Accumulate(acc, repeated); |
| 13213 | Py_DECREF(repeated); |
| 13214 | return r; |
| 13215 | } |
| 13216 | else { |
| 13217 | do { |
| 13218 | if (_PyAccu_Accumulate(acc, obj)) |
| 13219 | return -1; |
| 13220 | } while (--count); |
| 13221 | return 0; |
| 13222 | } |
| 13223 | } |
| 13224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13225 | PyObject * |
| 13226 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13227 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13228 | void *fmt; |
| 13229 | int fmtkind; |
| 13230 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13231 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13232 | int r; |
| 13233 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13234 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13236 | PyObject *temp = NULL; |
| 13237 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13238 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13239 | _PyAccu acc; |
| 13240 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13241 | |
| 13242 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13243 | return NULL; |
| 13244 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13245 | return NULL; |
| 13246 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13247 | return NULL; |
| 13248 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13249 | return NULL; |
| 13250 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13251 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13253 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13254 | PyErr_BadInternalCall(); |
| 13255 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13256 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13257 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13258 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13259 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13260 | if (PyUnicode_READY(uformat) == -1) |
| 13261 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13262 | if (_PyAccu_Init(&acc)) |
| 13263 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13264 | fmt = PyUnicode_DATA(uformat); |
| 13265 | fmtkind = PyUnicode_KIND(uformat); |
| 13266 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13267 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13269 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13270 | arglen = PyTuple_Size(args); |
| 13271 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13272 | } |
| 13273 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13274 | arglen = -1; |
| 13275 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13276 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13277 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13278 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13279 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13280 | |
| 13281 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13282 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13283 | PyObject *nonfmt; |
| 13284 | Py_ssize_t nonfmtpos; |
| 13285 | nonfmtpos = fmtpos++; |
| 13286 | while (fmtcnt >= 0 && |
| 13287 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13288 | fmtpos++; |
| 13289 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13290 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13291 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13292 | if (nonfmt == NULL) |
| 13293 | goto onError; |
| 13294 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13295 | Py_DECREF(nonfmt); |
| 13296 | if (r) |
| 13297 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13298 | } |
| 13299 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13300 | /* Got a format specifier */ |
| 13301 | int flags = 0; |
| 13302 | Py_ssize_t width = -1; |
| 13303 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13304 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13305 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13306 | int isnumok; |
| 13307 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13308 | void *pbuf = NULL; |
| 13309 | Py_ssize_t pindex, len; |
| 13310 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13312 | fmtpos++; |
| 13313 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13314 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13315 | Py_ssize_t keylen; |
| 13316 | PyObject *key; |
| 13317 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13318 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13319 | if (dict == NULL) { |
| 13320 | PyErr_SetString(PyExc_TypeError, |
| 13321 | "format requires a mapping"); |
| 13322 | goto onError; |
| 13323 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13324 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13325 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13326 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13327 | /* Skip over balanced parentheses */ |
| 13328 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13329 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13330 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13331 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13332 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13333 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13334 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13335 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13336 | if (fmtcnt < 0 || pcount > 0) { |
| 13337 | PyErr_SetString(PyExc_ValueError, |
| 13338 | "incomplete format key"); |
| 13339 | goto onError; |
| 13340 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13341 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13342 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13343 | if (key == NULL) |
| 13344 | goto onError; |
| 13345 | if (args_owned) { |
| 13346 | Py_DECREF(args); |
| 13347 | args_owned = 0; |
| 13348 | } |
| 13349 | args = PyObject_GetItem(dict, key); |
| 13350 | Py_DECREF(key); |
| 13351 | if (args == NULL) { |
| 13352 | goto onError; |
| 13353 | } |
| 13354 | args_owned = 1; |
| 13355 | arglen = -1; |
| 13356 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13358 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13359 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13360 | case '-': flags |= F_LJUST; continue; |
| 13361 | case '+': flags |= F_SIGN; continue; |
| 13362 | case ' ': flags |= F_BLANK; continue; |
| 13363 | case '#': flags |= F_ALT; continue; |
| 13364 | case '0': flags |= F_ZERO; continue; |
| 13365 | } |
| 13366 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13367 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13368 | if (c == '*') { |
| 13369 | v = getnextarg(args, arglen, &argidx); |
| 13370 | if (v == NULL) |
| 13371 | goto onError; |
| 13372 | if (!PyLong_Check(v)) { |
| 13373 | PyErr_SetString(PyExc_TypeError, |
| 13374 | "* wants int"); |
| 13375 | goto onError; |
| 13376 | } |
| 13377 | width = PyLong_AsLong(v); |
| 13378 | if (width == -1 && PyErr_Occurred()) |
| 13379 | goto onError; |
| 13380 | if (width < 0) { |
| 13381 | flags |= F_LJUST; |
| 13382 | width = -width; |
| 13383 | } |
| 13384 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13385 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13386 | } |
| 13387 | else if (c >= '0' && c <= '9') { |
| 13388 | width = c - '0'; |
| 13389 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13390 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13391 | if (c < '0' || c > '9') |
| 13392 | break; |
| 13393 | if ((width*10) / 10 != width) { |
| 13394 | PyErr_SetString(PyExc_ValueError, |
| 13395 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13396 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | } |
| 13398 | width = width*10 + (c - '0'); |
| 13399 | } |
| 13400 | } |
| 13401 | if (c == '.') { |
| 13402 | prec = 0; |
| 13403 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13404 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13405 | if (c == '*') { |
| 13406 | v = getnextarg(args, arglen, &argidx); |
| 13407 | if (v == NULL) |
| 13408 | goto onError; |
| 13409 | if (!PyLong_Check(v)) { |
| 13410 | PyErr_SetString(PyExc_TypeError, |
| 13411 | "* wants int"); |
| 13412 | goto onError; |
| 13413 | } |
| 13414 | prec = PyLong_AsLong(v); |
| 13415 | if (prec == -1 && PyErr_Occurred()) |
| 13416 | goto onError; |
| 13417 | if (prec < 0) |
| 13418 | prec = 0; |
| 13419 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13420 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13421 | } |
| 13422 | else if (c >= '0' && c <= '9') { |
| 13423 | prec = c - '0'; |
| 13424 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13425 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13426 | if (c < '0' || c > '9') |
| 13427 | break; |
| 13428 | if ((prec*10) / 10 != prec) { |
| 13429 | PyErr_SetString(PyExc_ValueError, |
| 13430 | "prec too big"); |
| 13431 | goto onError; |
| 13432 | } |
| 13433 | prec = prec*10 + (c - '0'); |
| 13434 | } |
| 13435 | } |
| 13436 | } /* prec */ |
| 13437 | if (fmtcnt >= 0) { |
| 13438 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13439 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13440 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13441 | } |
| 13442 | } |
| 13443 | if (fmtcnt < 0) { |
| 13444 | PyErr_SetString(PyExc_ValueError, |
| 13445 | "incomplete format"); |
| 13446 | goto onError; |
| 13447 | } |
| 13448 | if (c != '%') { |
| 13449 | v = getnextarg(args, arglen, &argidx); |
| 13450 | if (v == NULL) |
| 13451 | goto onError; |
| 13452 | } |
| 13453 | sign = 0; |
| 13454 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13455 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13456 | switch (c) { |
| 13457 | |
| 13458 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13459 | _PyAccu_Accumulate(&acc, percent); |
| 13460 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13461 | |
| 13462 | case 's': |
| 13463 | case 'r': |
| 13464 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13465 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13466 | temp = v; |
| 13467 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13468 | } |
| 13469 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | if (c == 's') |
| 13471 | temp = PyObject_Str(v); |
| 13472 | else if (c == 'r') |
| 13473 | temp = PyObject_Repr(v); |
| 13474 | else |
| 13475 | temp = PyObject_ASCII(v); |
| 13476 | if (temp == NULL) |
| 13477 | goto onError; |
| 13478 | if (PyUnicode_Check(temp)) |
| 13479 | /* nothing to do */; |
| 13480 | else { |
| 13481 | Py_DECREF(temp); |
| 13482 | PyErr_SetString(PyExc_TypeError, |
| 13483 | "%s argument has non-string str()"); |
| 13484 | goto onError; |
| 13485 | } |
| 13486 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13487 | if (PyUnicode_READY(temp) == -1) { |
| 13488 | Py_CLEAR(temp); |
| 13489 | goto onError; |
| 13490 | } |
| 13491 | pbuf = PyUnicode_DATA(temp); |
| 13492 | kind = PyUnicode_KIND(temp); |
| 13493 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13494 | if (prec >= 0 && len > prec) |
| 13495 | len = prec; |
| 13496 | break; |
| 13497 | |
| 13498 | case 'i': |
| 13499 | case 'd': |
| 13500 | case 'u': |
| 13501 | case 'o': |
| 13502 | case 'x': |
| 13503 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13504 | isnumok = 0; |
| 13505 | if (PyNumber_Check(v)) { |
| 13506 | PyObject *iobj=NULL; |
| 13507 | |
| 13508 | if (PyLong_Check(v)) { |
| 13509 | iobj = v; |
| 13510 | Py_INCREF(iobj); |
| 13511 | } |
| 13512 | else { |
| 13513 | iobj = PyNumber_Long(v); |
| 13514 | } |
| 13515 | if (iobj!=NULL) { |
| 13516 | if (PyLong_Check(iobj)) { |
| 13517 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13518 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13519 | Py_DECREF(iobj); |
| 13520 | if (!temp) |
| 13521 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13522 | if (PyUnicode_READY(temp) == -1) { |
| 13523 | Py_CLEAR(temp); |
| 13524 | goto onError; |
| 13525 | } |
| 13526 | pbuf = PyUnicode_DATA(temp); |
| 13527 | kind = PyUnicode_KIND(temp); |
| 13528 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13529 | sign = 1; |
| 13530 | } |
| 13531 | else { |
| 13532 | Py_DECREF(iobj); |
| 13533 | } |
| 13534 | } |
| 13535 | } |
| 13536 | if (!isnumok) { |
| 13537 | PyErr_Format(PyExc_TypeError, |
| 13538 | "%%%c format: a number is required, " |
| 13539 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13540 | goto onError; |
| 13541 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13542 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13543 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13544 | fillobj = zero; |
| 13545 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13546 | break; |
| 13547 | |
| 13548 | case 'e': |
| 13549 | case 'E': |
| 13550 | case 'f': |
| 13551 | case 'F': |
| 13552 | case 'g': |
| 13553 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13554 | temp = formatfloat(v, flags, prec, c); |
| 13555 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13556 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13557 | if (PyUnicode_READY(temp) == -1) { |
| 13558 | Py_CLEAR(temp); |
| 13559 | goto onError; |
| 13560 | } |
| 13561 | pbuf = PyUnicode_DATA(temp); |
| 13562 | kind = PyUnicode_KIND(temp); |
| 13563 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13564 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13565 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13566 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13567 | fillobj = zero; |
| 13568 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13569 | break; |
| 13570 | |
| 13571 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13572 | { |
| 13573 | Py_UCS4 ch = formatchar(v); |
| 13574 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13576 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13577 | if (temp == NULL) |
| 13578 | goto onError; |
| 13579 | pbuf = PyUnicode_DATA(temp); |
| 13580 | kind = PyUnicode_KIND(temp); |
| 13581 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13583 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13584 | |
| 13585 | default: |
| 13586 | PyErr_Format(PyExc_ValueError, |
| 13587 | "unsupported format character '%c' (0x%x) " |
| 13588 | "at index %zd", |
| 13589 | (31<=c && c<=126) ? (char)c : '?', |
| 13590 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13591 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13592 | goto onError; |
| 13593 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13594 | /* pbuf is initialized here. */ |
| 13595 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13596 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13597 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13598 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13599 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13600 | pindex++; |
| 13601 | } |
| 13602 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13603 | signobj = plus; |
| 13604 | len--; |
| 13605 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13606 | } |
| 13607 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13608 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13609 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13610 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13611 | else |
| 13612 | sign = 0; |
| 13613 | } |
| 13614 | if (width < len) |
| 13615 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13616 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13617 | if (fill != ' ') { |
| 13618 | assert(signobj != NULL); |
| 13619 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13620 | goto onError; |
| 13621 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13622 | if (width > len) |
| 13623 | width--; |
| 13624 | } |
| 13625 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13626 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13627 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13628 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13629 | second = get_latin1_char( |
| 13630 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13631 | pindex += 2; |
| 13632 | if (second == NULL || |
| 13633 | _PyAccu_Accumulate(&acc, zero) || |
| 13634 | _PyAccu_Accumulate(&acc, second)) |
| 13635 | goto onError; |
| 13636 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13637 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13638 | width -= 2; |
| 13639 | if (width < 0) |
| 13640 | width = 0; |
| 13641 | len -= 2; |
| 13642 | } |
| 13643 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13644 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13645 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13646 | goto onError; |
| 13647 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13648 | } |
| 13649 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13650 | if (sign) { |
| 13651 | assert(signobj != NULL); |
| 13652 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13653 | goto onError; |
| 13654 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13655 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13656 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13657 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13658 | second = get_latin1_char( |
| 13659 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13660 | pindex += 2; |
| 13661 | if (second == NULL || |
| 13662 | _PyAccu_Accumulate(&acc, zero) || |
| 13663 | _PyAccu_Accumulate(&acc, second)) |
| 13664 | goto onError; |
| 13665 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13666 | } |
| 13667 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13668 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13669 | if (temp != NULL) { |
| 13670 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13671 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13672 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13673 | else { |
| 13674 | const char *p = (const char *) pbuf; |
| 13675 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13676 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13677 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13678 | } |
| 13679 | if (v == NULL) |
| 13680 | goto onError; |
| 13681 | r = _PyAccu_Accumulate(&acc, v); |
| 13682 | Py_DECREF(v); |
| 13683 | if (r) |
| 13684 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13685 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13686 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13687 | if (dict && (argidx < arglen) && c != '%') { |
| 13688 | PyErr_SetString(PyExc_TypeError, |
| 13689 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13690 | goto onError; |
| 13691 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13692 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13693 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13694 | } /* until end */ |
| 13695 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13696 | PyErr_SetString(PyExc_TypeError, |
| 13697 | "not all arguments converted during string formatting"); |
| 13698 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13699 | } |
| 13700 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13701 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13702 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13703 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13704 | } |
| 13705 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13706 | Py_XDECREF(temp); |
| 13707 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13708 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13709 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13710 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13711 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13712 | Py_XDECREF(temp); |
| 13713 | Py_XDECREF(second); |
| 13714 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13715 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13716 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13717 | } |
| 13718 | return NULL; |
| 13719 | } |
| 13720 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13721 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13722 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13723 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13724 | static PyObject * |
| 13725 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13726 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13727 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13728 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13729 | char *encoding = NULL; |
| 13730 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13731 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13732 | if (type != &PyUnicode_Type) |
| 13733 | return unicode_subtype_new(type, args, kwds); |
| 13734 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13735 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13736 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13737 | if (x == NULL) { |
| 13738 | Py_INCREF(unicode_empty); |
| 13739 | return unicode_empty; |
| 13740 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13741 | if (encoding == NULL && errors == NULL) |
| 13742 | return PyObject_Str(x); |
| 13743 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13744 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13745 | } |
| 13746 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13747 | static PyObject * |
| 13748 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13749 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13750 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13751 | Py_ssize_t length, char_size; |
| 13752 | int share_wstr, share_utf8; |
| 13753 | unsigned int kind; |
| 13754 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13756 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13757 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13758 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13759 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13760 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13761 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13762 | if (PyUnicode_READY(unicode)) { |
| 13763 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13764 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13765 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13766 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13767 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13768 | if (self == NULL) { |
| 13769 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13770 | return NULL; |
| 13771 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13772 | kind = PyUnicode_KIND(unicode); |
| 13773 | length = PyUnicode_GET_LENGTH(unicode); |
| 13774 | |
| 13775 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13776 | #ifdef Py_DEBUG |
| 13777 | _PyUnicode_HASH(self) = -1; |
| 13778 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13779 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13780 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13781 | _PyUnicode_STATE(self).interned = 0; |
| 13782 | _PyUnicode_STATE(self).kind = kind; |
| 13783 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13784 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13785 | _PyUnicode_STATE(self).ready = 1; |
| 13786 | _PyUnicode_WSTR(self) = NULL; |
| 13787 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13788 | _PyUnicode_UTF8(self) = NULL; |
| 13789 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13790 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13791 | |
| 13792 | share_utf8 = 0; |
| 13793 | share_wstr = 0; |
| 13794 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13795 | char_size = 1; |
| 13796 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13797 | share_utf8 = 1; |
| 13798 | } |
| 13799 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13800 | char_size = 2; |
| 13801 | if (sizeof(wchar_t) == 2) |
| 13802 | share_wstr = 1; |
| 13803 | } |
| 13804 | else { |
| 13805 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13806 | char_size = 4; |
| 13807 | if (sizeof(wchar_t) == 4) |
| 13808 | share_wstr = 1; |
| 13809 | } |
| 13810 | |
| 13811 | /* Ensure we won't overflow the length. */ |
| 13812 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13813 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13814 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13815 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13816 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13817 | if (data == NULL) { |
| 13818 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13819 | goto onError; |
| 13820 | } |
| 13821 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13822 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13823 | if (share_utf8) { |
| 13824 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13825 | _PyUnicode_UTF8(self) = data; |
| 13826 | } |
| 13827 | if (share_wstr) { |
| 13828 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13829 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13830 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13831 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13832 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13833 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13834 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13835 | #ifdef Py_DEBUG |
| 13836 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13837 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13838 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13839 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13840 | |
| 13841 | onError: |
| 13842 | Py_DECREF(unicode); |
| 13843 | Py_DECREF(self); |
| 13844 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13845 | } |
| 13846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13847 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13848 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13849 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13850 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13851 | encoding defaults to the current default string encoding.\n\ |
| 13852 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13853 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13854 | static PyObject *unicode_iter(PyObject *seq); |
| 13855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13856 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13857 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13858 | "str", /* tp_name */ |
| 13859 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13860 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13861 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13862 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13863 | 0, /* tp_print */ |
| 13864 | 0, /* tp_getattr */ |
| 13865 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13866 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13867 | unicode_repr, /* tp_repr */ |
| 13868 | &unicode_as_number, /* tp_as_number */ |
| 13869 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13870 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13871 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13872 | 0, /* tp_call*/ |
| 13873 | (reprfunc) unicode_str, /* tp_str */ |
| 13874 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13875 | 0, /* tp_setattro */ |
| 13876 | 0, /* tp_as_buffer */ |
| 13877 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13878 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13879 | unicode_doc, /* tp_doc */ |
| 13880 | 0, /* tp_traverse */ |
| 13881 | 0, /* tp_clear */ |
| 13882 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13883 | 0, /* tp_weaklistoffset */ |
| 13884 | unicode_iter, /* tp_iter */ |
| 13885 | 0, /* tp_iternext */ |
| 13886 | unicode_methods, /* tp_methods */ |
| 13887 | 0, /* tp_members */ |
| 13888 | 0, /* tp_getset */ |
| 13889 | &PyBaseObject_Type, /* tp_base */ |
| 13890 | 0, /* tp_dict */ |
| 13891 | 0, /* tp_descr_get */ |
| 13892 | 0, /* tp_descr_set */ |
| 13893 | 0, /* tp_dictoffset */ |
| 13894 | 0, /* tp_init */ |
| 13895 | 0, /* tp_alloc */ |
| 13896 | unicode_new, /* tp_new */ |
| 13897 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13898 | }; |
| 13899 | |
| 13900 | /* Initialize the Unicode implementation */ |
| 13901 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13902 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13903 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13904 | int i; |
| 13905 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13906 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13907 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13908 | 0x000A, /* LINE FEED */ |
| 13909 | 0x000D, /* CARRIAGE RETURN */ |
| 13910 | 0x001C, /* FILE SEPARATOR */ |
| 13911 | 0x001D, /* GROUP SEPARATOR */ |
| 13912 | 0x001E, /* RECORD SEPARATOR */ |
| 13913 | 0x0085, /* NEXT LINE */ |
| 13914 | 0x2028, /* LINE SEPARATOR */ |
| 13915 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13916 | }; |
| 13917 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13918 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13919 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13920 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13921 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 13922 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13923 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13924 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13925 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13926 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13927 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13928 | |
| 13929 | /* initialize the linebreak bloom filter */ |
| 13930 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13931 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13932 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13933 | |
| 13934 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13935 | |
| 13936 | #ifdef HAVE_MBCS |
| 13937 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13938 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13939 | PyErr_SetFromWindowsErr(0); |
| 13940 | return -1; |
| 13941 | } |
| 13942 | #endif |
| 13943 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13944 | } |
| 13945 | |
| 13946 | /* Finalize the Unicode implementation */ |
| 13947 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13948 | int |
| 13949 | PyUnicode_ClearFreeList(void) |
| 13950 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13951 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13952 | } |
| 13953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13954 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13955 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13956 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13957 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13958 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13959 | Py_XDECREF(unicode_empty); |
| 13960 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13961 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13962 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13963 | if (unicode_latin1[i]) { |
| 13964 | Py_DECREF(unicode_latin1[i]); |
| 13965 | unicode_latin1[i] = NULL; |
| 13966 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13967 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13968 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13969 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13970 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13971 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13972 | void |
| 13973 | PyUnicode_InternInPlace(PyObject **p) |
| 13974 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13975 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13976 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13977 | #ifdef Py_DEBUG |
| 13978 | assert(s != NULL); |
| 13979 | assert(_PyUnicode_CHECK(s)); |
| 13980 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13981 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13982 | return; |
| 13983 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13984 | /* If it's a subclass, we don't really know what putting |
| 13985 | it in the interned dict might do. */ |
| 13986 | if (!PyUnicode_CheckExact(s)) |
| 13987 | return; |
| 13988 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13989 | return; |
| 13990 | if (interned == NULL) { |
| 13991 | interned = PyDict_New(); |
| 13992 | if (interned == NULL) { |
| 13993 | PyErr_Clear(); /* Don't leave an exception */ |
| 13994 | return; |
| 13995 | } |
| 13996 | } |
| 13997 | /* It might be that the GetItem call fails even |
| 13998 | though the key is present in the dictionary, |
| 13999 | namely when this happens during a stack overflow. */ |
| 14000 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14001 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14002 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14004 | if (t) { |
| 14005 | Py_INCREF(t); |
| 14006 | Py_DECREF(*p); |
| 14007 | *p = t; |
| 14008 | return; |
| 14009 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14010 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14011 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14012 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14013 | PyErr_Clear(); |
| 14014 | PyThreadState_GET()->recursion_critical = 0; |
| 14015 | return; |
| 14016 | } |
| 14017 | PyThreadState_GET()->recursion_critical = 0; |
| 14018 | /* The two references in interned are not counted by refcnt. |
| 14019 | The deallocator will take care of this */ |
| 14020 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14021 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14022 | } |
| 14023 | |
| 14024 | void |
| 14025 | PyUnicode_InternImmortal(PyObject **p) |
| 14026 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14027 | PyUnicode_InternInPlace(p); |
| 14028 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14029 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14030 | Py_INCREF(*p); |
| 14031 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14032 | } |
| 14033 | |
| 14034 | PyObject * |
| 14035 | PyUnicode_InternFromString(const char *cp) |
| 14036 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14037 | PyObject *s = PyUnicode_FromString(cp); |
| 14038 | if (s == NULL) |
| 14039 | return NULL; |
| 14040 | PyUnicode_InternInPlace(&s); |
| 14041 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14042 | } |
| 14043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14044 | void |
| 14045 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14046 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14047 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14048 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14049 | Py_ssize_t i, n; |
| 14050 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14051 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14052 | if (interned == NULL || !PyDict_Check(interned)) |
| 14053 | return; |
| 14054 | keys = PyDict_Keys(interned); |
| 14055 | if (keys == NULL || !PyList_Check(keys)) { |
| 14056 | PyErr_Clear(); |
| 14057 | return; |
| 14058 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14059 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14060 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14061 | detector, interned unicode strings are not forcibly deallocated; |
| 14062 | rather, we give them their stolen references back, and then clear |
| 14063 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14064 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14065 | n = PyList_GET_SIZE(keys); |
| 14066 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14067 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14068 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14069 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14070 | if (PyUnicode_READY(s) == -1) { |
| 14071 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14072 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14073 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14074 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14075 | case SSTATE_NOT_INTERNED: |
| 14076 | /* XXX Shouldn't happen */ |
| 14077 | break; |
| 14078 | case SSTATE_INTERNED_IMMORTAL: |
| 14079 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14080 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14081 | break; |
| 14082 | case SSTATE_INTERNED_MORTAL: |
| 14083 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14084 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14085 | break; |
| 14086 | default: |
| 14087 | Py_FatalError("Inconsistent interned string state."); |
| 14088 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14089 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14090 | } |
| 14091 | fprintf(stderr, "total size of all interned strings: " |
| 14092 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14093 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14094 | Py_DECREF(keys); |
| 14095 | PyDict_Clear(interned); |
| 14096 | Py_DECREF(interned); |
| 14097 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14098 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14099 | |
| 14100 | |
| 14101 | /********************* Unicode Iterator **************************/ |
| 14102 | |
| 14103 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14104 | PyObject_HEAD |
| 14105 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14106 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14107 | } unicodeiterobject; |
| 14108 | |
| 14109 | static void |
| 14110 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14111 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14112 | _PyObject_GC_UNTRACK(it); |
| 14113 | Py_XDECREF(it->it_seq); |
| 14114 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14115 | } |
| 14116 | |
| 14117 | static int |
| 14118 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14119 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14120 | Py_VISIT(it->it_seq); |
| 14121 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14122 | } |
| 14123 | |
| 14124 | static PyObject * |
| 14125 | unicodeiter_next(unicodeiterobject *it) |
| 14126 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14127 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14128 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14129 | assert(it != NULL); |
| 14130 | seq = it->it_seq; |
| 14131 | if (seq == NULL) |
| 14132 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14133 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14134 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14135 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14136 | int kind = PyUnicode_KIND(seq); |
| 14137 | void *data = PyUnicode_DATA(seq); |
| 14138 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14139 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14140 | if (item != NULL) |
| 14141 | ++it->it_index; |
| 14142 | return item; |
| 14143 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14144 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14145 | Py_DECREF(seq); |
| 14146 | it->it_seq = NULL; |
| 14147 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14148 | } |
| 14149 | |
| 14150 | static PyObject * |
| 14151 | unicodeiter_len(unicodeiterobject *it) |
| 14152 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14153 | Py_ssize_t len = 0; |
| 14154 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14155 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14156 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14157 | } |
| 14158 | |
| 14159 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14160 | |
| 14161 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14162 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14163 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14164 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14165 | }; |
| 14166 | |
| 14167 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14168 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14169 | "str_iterator", /* tp_name */ |
| 14170 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14171 | 0, /* tp_itemsize */ |
| 14172 | /* methods */ |
| 14173 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14174 | 0, /* tp_print */ |
| 14175 | 0, /* tp_getattr */ |
| 14176 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14177 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14178 | 0, /* tp_repr */ |
| 14179 | 0, /* tp_as_number */ |
| 14180 | 0, /* tp_as_sequence */ |
| 14181 | 0, /* tp_as_mapping */ |
| 14182 | 0, /* tp_hash */ |
| 14183 | 0, /* tp_call */ |
| 14184 | 0, /* tp_str */ |
| 14185 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14186 | 0, /* tp_setattro */ |
| 14187 | 0, /* tp_as_buffer */ |
| 14188 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14189 | 0, /* tp_doc */ |
| 14190 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14191 | 0, /* tp_clear */ |
| 14192 | 0, /* tp_richcompare */ |
| 14193 | 0, /* tp_weaklistoffset */ |
| 14194 | PyObject_SelfIter, /* tp_iter */ |
| 14195 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14196 | unicodeiter_methods, /* tp_methods */ |
| 14197 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14198 | }; |
| 14199 | |
| 14200 | static PyObject * |
| 14201 | unicode_iter(PyObject *seq) |
| 14202 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14203 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14204 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14205 | if (!PyUnicode_Check(seq)) { |
| 14206 | PyErr_BadInternalCall(); |
| 14207 | return NULL; |
| 14208 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14209 | if (PyUnicode_READY(seq) == -1) |
| 14210 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14211 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14212 | if (it == NULL) |
| 14213 | return NULL; |
| 14214 | it->it_index = 0; |
| 14215 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14216 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14217 | _PyObject_GC_TRACK(it); |
| 14218 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14219 | } |
| 14220 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14221 | |
| 14222 | size_t |
| 14223 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14224 | { |
| 14225 | int res = 0; |
| 14226 | while(*u++) |
| 14227 | res++; |
| 14228 | return res; |
| 14229 | } |
| 14230 | |
| 14231 | Py_UNICODE* |
| 14232 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14233 | { |
| 14234 | Py_UNICODE *u = s1; |
| 14235 | while ((*u++ = *s2++)); |
| 14236 | return s1; |
| 14237 | } |
| 14238 | |
| 14239 | Py_UNICODE* |
| 14240 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14241 | { |
| 14242 | Py_UNICODE *u = s1; |
| 14243 | while ((*u++ = *s2++)) |
| 14244 | if (n-- == 0) |
| 14245 | break; |
| 14246 | return s1; |
| 14247 | } |
| 14248 | |
| 14249 | Py_UNICODE* |
| 14250 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14251 | { |
| 14252 | Py_UNICODE *u1 = s1; |
| 14253 | u1 += Py_UNICODE_strlen(u1); |
| 14254 | Py_UNICODE_strcpy(u1, s2); |
| 14255 | return s1; |
| 14256 | } |
| 14257 | |
| 14258 | int |
| 14259 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14260 | { |
| 14261 | while (*s1 && *s2 && *s1 == *s2) |
| 14262 | s1++, s2++; |
| 14263 | if (*s1 && *s2) |
| 14264 | return (*s1 < *s2) ? -1 : +1; |
| 14265 | if (*s1) |
| 14266 | return 1; |
| 14267 | if (*s2) |
| 14268 | return -1; |
| 14269 | return 0; |
| 14270 | } |
| 14271 | |
| 14272 | int |
| 14273 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14274 | { |
| 14275 | register Py_UNICODE u1, u2; |
| 14276 | for (; n != 0; n--) { |
| 14277 | u1 = *s1; |
| 14278 | u2 = *s2; |
| 14279 | if (u1 != u2) |
| 14280 | return (u1 < u2) ? -1 : +1; |
| 14281 | if (u1 == '\0') |
| 14282 | return 0; |
| 14283 | s1++; |
| 14284 | s2++; |
| 14285 | } |
| 14286 | return 0; |
| 14287 | } |
| 14288 | |
| 14289 | Py_UNICODE* |
| 14290 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14291 | { |
| 14292 | const Py_UNICODE *p; |
| 14293 | for (p = s; *p; p++) |
| 14294 | if (*p == c) |
| 14295 | return (Py_UNICODE*)p; |
| 14296 | return NULL; |
| 14297 | } |
| 14298 | |
| 14299 | Py_UNICODE* |
| 14300 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14301 | { |
| 14302 | const Py_UNICODE *p; |
| 14303 | p = s + Py_UNICODE_strlen(s); |
| 14304 | while (p != s) { |
| 14305 | p--; |
| 14306 | if (*p == c) |
| 14307 | return (Py_UNICODE*)p; |
| 14308 | } |
| 14309 | return NULL; |
| 14310 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14311 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14312 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14313 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14314 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14315 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14316 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14317 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14318 | if (!PyUnicode_Check(unicode)) { |
| 14319 | PyErr_BadArgument(); |
| 14320 | return NULL; |
| 14321 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14322 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14323 | if (u == NULL) |
| 14324 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14325 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14326 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14327 | PyErr_NoMemory(); |
| 14328 | return NULL; |
| 14329 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14330 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14331 | size *= sizeof(Py_UNICODE); |
| 14332 | copy = PyMem_Malloc(size); |
| 14333 | if (copy == NULL) { |
| 14334 | PyErr_NoMemory(); |
| 14335 | return NULL; |
| 14336 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14337 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14338 | return copy; |
| 14339 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14340 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14341 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14342 | to the string.Formatter class implemented in Python. */ |
| 14343 | |
| 14344 | static PyMethodDef _string_methods[] = { |
| 14345 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14346 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14347 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14348 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14349 | {NULL, NULL} |
| 14350 | }; |
| 14351 | |
| 14352 | static struct PyModuleDef _string_module = { |
| 14353 | PyModuleDef_HEAD_INIT, |
| 14354 | "_string", |
| 14355 | PyDoc_STR("string helper module"), |
| 14356 | 0, |
| 14357 | _string_methods, |
| 14358 | NULL, |
| 14359 | NULL, |
| 14360 | NULL, |
| 14361 | NULL |
| 14362 | }; |
| 14363 | |
| 14364 | PyMODINIT_FUNC |
| 14365 | PyInit__string(void) |
| 14366 | { |
| 14367 | return PyModule_Create(&_string_module); |
| 14368 | } |
| 14369 | |
| 14370 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14371 | #ifdef __cplusplus |
| 14372 | } |
| 14373 | #endif |