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 | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 69 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 70 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 71 | #else |
| 72 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 73 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 74 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 75 | #define _PyUnicode_UTF8(op) \ |
| 76 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 77 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 78 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 79 | assert(PyUnicode_IS_READY(op)), \ |
| 80 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 81 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 82 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 83 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 84 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 85 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 86 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 87 | assert(PyUnicode_IS_READY(op)), \ |
| 88 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 89 | ((PyASCIIObject*)(op))->length : \ |
| 90 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 91 | #define _PyUnicode_WSTR(op) \ |
| 92 | (((PyASCIIObject*)(op))->wstr) |
| 93 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 94 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 95 | #define _PyUnicode_LENGTH(op) \ |
| 96 | (((PyASCIIObject *)(op))->length) |
| 97 | #define _PyUnicode_STATE(op) \ |
| 98 | (((PyASCIIObject *)(op))->state) |
| 99 | #define _PyUnicode_HASH(op) \ |
| 100 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 101 | #define _PyUnicode_KIND(op) \ |
| 102 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 103 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 104 | #define _PyUnicode_GET_LENGTH(op) \ |
| 105 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 107 | #define _PyUnicode_DATA_ANY(op) \ |
| 108 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 110 | #undef PyUnicode_READY |
| 111 | #define PyUnicode_READY(op) \ |
| 112 | (assert(_PyUnicode_CHECK(op)), \ |
| 113 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 114 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 115 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 116 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 117 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 118 | (assert(_PyUnicode_CHECK(op)), \ |
| 119 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 120 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 121 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 122 | (assert(_PyUnicode_CHECK(op)), \ |
| 123 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 124 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 125 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 126 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 127 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 128 | (assert(_PyUnicode_CHECK(op)), \ |
| 129 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 130 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 131 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 132 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 133 | /* true if the Unicode object has an allocated wstr memory block |
| 134 | (not shared with other data) */ |
| 135 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 136 | (assert(_PyUnicode_CHECK(op)), \ |
| 137 | (_PyUnicode_WSTR(op) && \ |
| 138 | (!PyUnicode_IS_READY(op) || \ |
| 139 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 140 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 141 | /* Generic helper macro to convert characters of different types. |
| 142 | from_type and to_type have to be valid type names, begin and end |
| 143 | are pointers to the source characters which should be of type |
| 144 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 145 | buffer where the result characters are written to. */ |
| 146 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 147 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 148 | to_type *_to = (to_type *) to; \ |
| 149 | const from_type *_iter = (begin); \ |
| 150 | const from_type *_end = (end); \ |
| 151 | Py_ssize_t n = (_end) - (_iter); \ |
| 152 | const from_type *_unrolled_end = \ |
| 153 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 154 | while (_iter < (_unrolled_end)) { \ |
| 155 | _to[0] = (to_type) _iter[0]; \ |
| 156 | _to[1] = (to_type) _iter[1]; \ |
| 157 | _to[2] = (to_type) _iter[2]; \ |
| 158 | _to[3] = (to_type) _iter[3]; \ |
| 159 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 160 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 161 | while (_iter < (_end)) \ |
| 162 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 163 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 164 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 165 | /* The Unicode string has been modified: reset the hash */ |
| 166 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 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 | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 226 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 227 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 228 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 229 | static PyObject * |
| 230 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 231 | static PyObject * |
| 232 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 233 | static PyObject * |
| 234 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 235 | |
| 236 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 237 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 238 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 239 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 240 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 241 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 242 | static void |
| 243 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 244 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 245 | PyObject *unicode, |
| 246 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 247 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 248 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 249 | /* Same for linebreaks */ |
| 250 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 251 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 252 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 253 | /* 0x000B, * LINE TABULATION */ |
| 254 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 255 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 256 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 257 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 258 | /* 0x001C, * FILE SEPARATOR */ |
| 259 | /* 0x001D, * GROUP SEPARATOR */ |
| 260 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 261 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 262 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 263 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 264 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 266 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 277 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 278 | 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] | 279 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 280 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 281 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 282 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 283 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 284 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 285 | /* This is actually an illegal character, so it should |
| 286 | not be passed to unichr. */ |
| 287 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 288 | #endif |
| 289 | } |
| 290 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 291 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 292 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 293 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 294 | { |
| 295 | PyASCIIObject *ascii; |
| 296 | unsigned int kind; |
| 297 | |
| 298 | assert(PyUnicode_Check(op)); |
| 299 | |
| 300 | ascii = (PyASCIIObject *)op; |
| 301 | kind = ascii->state.kind; |
| 302 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 303 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 304 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | assert(ascii->state.ready == 1); |
| 306 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 307 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 308 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 309 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 310 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 311 | if (ascii->state.compact == 1) { |
| 312 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | assert(kind == PyUnicode_1BYTE_KIND |
| 314 | || kind == PyUnicode_2BYTE_KIND |
| 315 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 316 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 317 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 318 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 319 | } |
| 320 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 322 | |
| 323 | data = unicode->data.any; |
| 324 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 325 | assert(ascii->length == 0); |
| 326 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 327 | assert(ascii->state.compact == 0); |
| 328 | assert(ascii->state.ascii == 0); |
| 329 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 330 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 331 | assert(ascii->wstr != NULL); |
| 332 | assert(data == NULL); |
| 333 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | } |
| 335 | else { |
| 336 | assert(kind == PyUnicode_1BYTE_KIND |
| 337 | || kind == PyUnicode_2BYTE_KIND |
| 338 | || kind == PyUnicode_4BYTE_KIND); |
| 339 | assert(ascii->state.compact == 0); |
| 340 | assert(ascii->state.ready == 1); |
| 341 | assert(data != NULL); |
| 342 | if (ascii->state.ascii) { |
| 343 | assert (compact->utf8 == data); |
| 344 | assert (compact->utf8_length == ascii->length); |
| 345 | } |
| 346 | else |
| 347 | assert (compact->utf8 != data); |
| 348 | } |
| 349 | } |
| 350 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 351 | if ( |
| 352 | #if SIZEOF_WCHAR_T == 2 |
| 353 | kind == PyUnicode_2BYTE_KIND |
| 354 | #else |
| 355 | kind == PyUnicode_4BYTE_KIND |
| 356 | #endif |
| 357 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 358 | { |
| 359 | assert(ascii->wstr == data); |
| 360 | assert(compact->wstr_length == ascii->length); |
| 361 | } else |
| 362 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 363 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 364 | |
| 365 | if (compact->utf8 == NULL) |
| 366 | assert(compact->utf8_length == 0); |
| 367 | if (ascii->wstr == NULL) |
| 368 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 369 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 370 | /* check that the best kind is used */ |
| 371 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 372 | { |
| 373 | Py_ssize_t i; |
| 374 | Py_UCS4 maxchar = 0; |
| 375 | void *data = PyUnicode_DATA(ascii); |
| 376 | for (i=0; i < ascii->length; i++) |
| 377 | { |
| 378 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 379 | if (ch > maxchar) |
| 380 | maxchar = ch; |
| 381 | } |
Victor Stinner | da29cc3 | 2011-11-21 14:31:41 +0100 | [diff] [blame] | 382 | if (maxchar > 0x10FFFF) { |
| 383 | printf("Invalid Unicode string! {"); |
| 384 | for (i=0; i < ascii->length; i++) |
| 385 | { |
| 386 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 387 | if (i) |
| 388 | printf(", U+%04x", ch); |
| 389 | else |
| 390 | printf("U+%04x", ch); |
| 391 | } |
Victor Stinner | 5bbe5e7 | 2011-11-21 22:54:05 +0100 | [diff] [blame] | 392 | printf("} (len=%lu)\n", ascii->length); |
Victor Stinner | da29cc3 | 2011-11-21 14:31:41 +0100 | [diff] [blame] | 393 | abort(); |
| 394 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 395 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 396 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 397 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 398 | assert(maxchar <= 255); |
| 399 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 400 | else |
| 401 | assert(maxchar < 128); |
| 402 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 403 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 404 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 405 | assert(maxchar <= 0xFFFF); |
| 406 | } |
| 407 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 408 | assert(maxchar >= 0x10000); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 409 | assert(maxchar <= 0x10FFFF); |
| 410 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 411 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 412 | return 1; |
| 413 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 414 | #endif |
| 415 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 416 | static PyObject* |
| 417 | unicode_result_wchar(PyObject *unicode) |
| 418 | { |
| 419 | #ifndef Py_DEBUG |
| 420 | Py_ssize_t len; |
| 421 | |
| 422 | assert(Py_REFCNT(unicode) == 1); |
| 423 | |
| 424 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 425 | if (len == 0) { |
| 426 | Py_INCREF(unicode_empty); |
| 427 | Py_DECREF(unicode); |
| 428 | return unicode_empty; |
| 429 | } |
| 430 | |
| 431 | if (len == 1) { |
| 432 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 433 | if (ch < 256) { |
| 434 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 435 | Py_DECREF(unicode); |
| 436 | return latin1_char; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (_PyUnicode_Ready(unicode) < 0) { |
| 441 | Py_XDECREF(unicode); |
| 442 | return NULL; |
| 443 | } |
| 444 | #else |
| 445 | /* don't make the result ready in debug mode to ensure that the caller |
| 446 | makes the string ready before using it */ |
| 447 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 448 | #endif |
| 449 | return unicode; |
| 450 | } |
| 451 | |
| 452 | static PyObject* |
| 453 | unicode_result_ready(PyObject *unicode) |
| 454 | { |
| 455 | Py_ssize_t length; |
| 456 | |
| 457 | length = PyUnicode_GET_LENGTH(unicode); |
| 458 | if (length == 0) { |
| 459 | if (unicode != unicode_empty) { |
| 460 | Py_INCREF(unicode_empty); |
| 461 | Py_DECREF(unicode); |
| 462 | } |
| 463 | return unicode_empty; |
| 464 | } |
| 465 | |
| 466 | if (length == 1) { |
| 467 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 468 | if (ch < 256) { |
| 469 | PyObject *latin1_char = unicode_latin1[ch]; |
| 470 | if (latin1_char != NULL) { |
| 471 | if (unicode != latin1_char) { |
| 472 | Py_INCREF(latin1_char); |
| 473 | Py_DECREF(unicode); |
| 474 | } |
| 475 | return latin1_char; |
| 476 | } |
| 477 | else { |
| 478 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 479 | Py_INCREF(unicode); |
| 480 | unicode_latin1[ch] = unicode; |
| 481 | return unicode; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 487 | return unicode; |
| 488 | } |
| 489 | |
| 490 | static PyObject* |
| 491 | unicode_result(PyObject *unicode) |
| 492 | { |
| 493 | assert(_PyUnicode_CHECK(unicode)); |
| 494 | if (PyUnicode_IS_READY(unicode)) |
| 495 | return unicode_result_ready(unicode); |
| 496 | else |
| 497 | return unicode_result_wchar(unicode); |
| 498 | } |
| 499 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 500 | #ifdef HAVE_MBCS |
| 501 | static OSVERSIONINFOEX winver; |
| 502 | #endif |
| 503 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 504 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 505 | |
| 506 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 507 | to keep things simple, we use a single bitmask, using the least 5 |
| 508 | bits from each unicode characters as the bit index. */ |
| 509 | |
| 510 | /* the linebreak mask is set up by Unicode_Init below */ |
| 511 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 512 | #if LONG_BIT >= 128 |
| 513 | #define BLOOM_WIDTH 128 |
| 514 | #elif LONG_BIT >= 64 |
| 515 | #define BLOOM_WIDTH 64 |
| 516 | #elif LONG_BIT >= 32 |
| 517 | #define BLOOM_WIDTH 32 |
| 518 | #else |
| 519 | #error "LONG_BIT is smaller than 32" |
| 520 | #endif |
| 521 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 522 | #define BLOOM_MASK unsigned long |
| 523 | |
| 524 | static BLOOM_MASK bloom_linebreak; |
| 525 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 526 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 527 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 529 | #define BLOOM_LINEBREAK(ch) \ |
| 530 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 531 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 532 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 533 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 534 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 535 | { |
| 536 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 537 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 538 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | Py_ssize_t i; |
| 540 | |
| 541 | mask = 0; |
| 542 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 543 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | |
| 545 | return mask; |
| 546 | } |
| 547 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 548 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 549 | (BLOOM(mask, chr) \ |
| 550 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 552 | /* Compilation of templated routines */ |
| 553 | |
| 554 | #include "stringlib/asciilib.h" |
| 555 | #include "stringlib/fastsearch.h" |
| 556 | #include "stringlib/partition.h" |
| 557 | #include "stringlib/split.h" |
| 558 | #include "stringlib/count.h" |
| 559 | #include "stringlib/find.h" |
| 560 | #include "stringlib/find_max_char.h" |
| 561 | #include "stringlib/localeutil.h" |
| 562 | #include "stringlib/undef.h" |
| 563 | |
| 564 | #include "stringlib/ucs1lib.h" |
| 565 | #include "stringlib/fastsearch.h" |
| 566 | #include "stringlib/partition.h" |
| 567 | #include "stringlib/split.h" |
| 568 | #include "stringlib/count.h" |
| 569 | #include "stringlib/find.h" |
| 570 | #include "stringlib/find_max_char.h" |
| 571 | #include "stringlib/localeutil.h" |
| 572 | #include "stringlib/undef.h" |
| 573 | |
| 574 | #include "stringlib/ucs2lib.h" |
| 575 | #include "stringlib/fastsearch.h" |
| 576 | #include "stringlib/partition.h" |
| 577 | #include "stringlib/split.h" |
| 578 | #include "stringlib/count.h" |
| 579 | #include "stringlib/find.h" |
| 580 | #include "stringlib/find_max_char.h" |
| 581 | #include "stringlib/localeutil.h" |
| 582 | #include "stringlib/undef.h" |
| 583 | |
| 584 | #include "stringlib/ucs4lib.h" |
| 585 | #include "stringlib/fastsearch.h" |
| 586 | #include "stringlib/partition.h" |
| 587 | #include "stringlib/split.h" |
| 588 | #include "stringlib/count.h" |
| 589 | #include "stringlib/find.h" |
| 590 | #include "stringlib/find_max_char.h" |
| 591 | #include "stringlib/localeutil.h" |
| 592 | #include "stringlib/undef.h" |
| 593 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 594 | #include "stringlib/unicodedefs.h" |
| 595 | #include "stringlib/fastsearch.h" |
| 596 | #include "stringlib/count.h" |
| 597 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 598 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 600 | /* --- Unicode Object ----------------------------------------------------- */ |
| 601 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 602 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 603 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 604 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 605 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 606 | Py_ssize_t size, Py_UCS4 ch, |
| 607 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 608 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 609 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 610 | |
| 611 | switch (kind) { |
| 612 | case PyUnicode_1BYTE_KIND: |
| 613 | { |
| 614 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 615 | if (ch1 == ch) |
| 616 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 617 | else |
| 618 | return -1; |
| 619 | } |
| 620 | case PyUnicode_2BYTE_KIND: |
| 621 | { |
| 622 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 623 | if (ch2 == ch) |
| 624 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 625 | else |
| 626 | return -1; |
| 627 | } |
| 628 | case PyUnicode_4BYTE_KIND: |
| 629 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 630 | default: |
| 631 | assert(0); |
| 632 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 633 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 636 | static PyObject* |
| 637 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 638 | { |
| 639 | Py_ssize_t char_size; |
| 640 | Py_ssize_t struct_size; |
| 641 | Py_ssize_t new_size; |
| 642 | int share_wstr; |
| 643 | |
| 644 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 645 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 646 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 647 | struct_size = sizeof(PyASCIIObject); |
| 648 | else |
| 649 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 650 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 651 | |
| 652 | _Py_DEC_REFTOTAL; |
| 653 | _Py_ForgetReference(unicode); |
| 654 | |
| 655 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 656 | PyErr_NoMemory(); |
| 657 | return NULL; |
| 658 | } |
| 659 | new_size = (struct_size + (length + 1) * char_size); |
| 660 | |
| 661 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 662 | if (unicode == NULL) { |
| 663 | PyObject_Del(unicode); |
| 664 | PyErr_NoMemory(); |
| 665 | return NULL; |
| 666 | } |
| 667 | _Py_NewReference(unicode); |
| 668 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 669 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 670 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 671 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 672 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 673 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 674 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 675 | length, 0); |
| 676 | return unicode; |
| 677 | } |
| 678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 679 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 680 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 681 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 682 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 683 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 684 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 685 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 686 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 687 | |
| 688 | if (PyUnicode_IS_READY(unicode)) { |
| 689 | Py_ssize_t char_size; |
| 690 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 691 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 692 | void *data; |
| 693 | |
| 694 | data = _PyUnicode_DATA_ANY(unicode); |
| 695 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 696 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 697 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 698 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 699 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 700 | { |
| 701 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 702 | _PyUnicode_UTF8(unicode) = NULL; |
| 703 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 704 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 705 | |
| 706 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 707 | PyErr_NoMemory(); |
| 708 | return -1; |
| 709 | } |
| 710 | new_size = (length + 1) * char_size; |
| 711 | |
| 712 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 713 | if (data == NULL) { |
| 714 | PyErr_NoMemory(); |
| 715 | return -1; |
| 716 | } |
| 717 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 718 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 719 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 720 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 721 | } |
| 722 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 723 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 724 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 725 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 726 | _PyUnicode_LENGTH(unicode) = length; |
| 727 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 728 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 729 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 730 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 731 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 733 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 734 | |
| 735 | /* check for integer overflow */ |
| 736 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 737 | PyErr_NoMemory(); |
| 738 | return -1; |
| 739 | } |
| 740 | wstr = _PyUnicode_WSTR(unicode); |
| 741 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 742 | if (!wstr) { |
| 743 | PyErr_NoMemory(); |
| 744 | return -1; |
| 745 | } |
| 746 | _PyUnicode_WSTR(unicode) = wstr; |
| 747 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 748 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 749 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 753 | static PyObject* |
| 754 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 755 | { |
| 756 | Py_ssize_t copy_length; |
| 757 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 758 | PyObject *copy; |
| 759 | assert(PyUnicode_IS_READY(unicode)); |
| 760 | |
| 761 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 762 | if (copy == NULL) |
| 763 | return NULL; |
| 764 | |
| 765 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 766 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 767 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 768 | } |
| 769 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 770 | PyObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 771 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 772 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 773 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 774 | if (w == NULL) |
| 775 | return NULL; |
| 776 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 777 | copy_length = Py_MIN(copy_length, length); |
| 778 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 779 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 780 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 784 | /* 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] | 785 | Ux0000 terminated; some code (e.g. new_identifier) |
| 786 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 787 | |
| 788 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 789 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 790 | |
| 791 | */ |
| 792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 793 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 794 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 795 | #endif |
| 796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 797 | static PyUnicodeObject * |
| 798 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 799 | { |
| 800 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 801 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 802 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 803 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 804 | if (length == 0 && unicode_empty != NULL) { |
| 805 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 806 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 809 | /* Ensure we won't overflow the size. */ |
| 810 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 811 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 812 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | if (length < 0) { |
| 814 | PyErr_SetString(PyExc_SystemError, |
| 815 | "Negative size passed to _PyUnicode_New"); |
| 816 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 819 | #ifdef Py_DEBUG |
| 820 | ++unicode_old_new_calls; |
| 821 | #endif |
| 822 | |
| 823 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 824 | if (unicode == NULL) |
| 825 | return NULL; |
| 826 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 827 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 828 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 829 | PyErr_NoMemory(); |
| 830 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 831 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 832 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 833 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 834 | * the caller fails before initializing str -- unicode_resize() |
| 835 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 836 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 837 | * We don't want unicode_resize to read uninitialized memory in |
| 838 | * that case. |
| 839 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 840 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 841 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 842 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 843 | _PyUnicode_HASH(unicode) = -1; |
| 844 | _PyUnicode_STATE(unicode).interned = 0; |
| 845 | _PyUnicode_STATE(unicode).kind = 0; |
| 846 | _PyUnicode_STATE(unicode).compact = 0; |
| 847 | _PyUnicode_STATE(unicode).ready = 0; |
| 848 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 849 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 850 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 851 | _PyUnicode_UTF8(unicode) = NULL; |
| 852 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 853 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 854 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 855 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 856 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 857 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 858 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 859 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 860 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 861 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 864 | static const char* |
| 865 | unicode_kind_name(PyObject *unicode) |
| 866 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 867 | /* don't check consistency: unicode_kind_name() is called from |
| 868 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 869 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 870 | { |
| 871 | if (!PyUnicode_IS_READY(unicode)) |
| 872 | return "wstr"; |
| 873 | switch(PyUnicode_KIND(unicode)) |
| 874 | { |
| 875 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 876 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 877 | return "legacy ascii"; |
| 878 | else |
| 879 | return "legacy latin1"; |
| 880 | case PyUnicode_2BYTE_KIND: |
| 881 | return "legacy UCS2"; |
| 882 | case PyUnicode_4BYTE_KIND: |
| 883 | return "legacy UCS4"; |
| 884 | default: |
| 885 | return "<legacy invalid kind>"; |
| 886 | } |
| 887 | } |
| 888 | assert(PyUnicode_IS_READY(unicode)); |
| 889 | switch(PyUnicode_KIND(unicode)) |
| 890 | { |
| 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))); |
| 1091 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1092 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1093 | { |
| 1094 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1095 | iter += 2; |
| 1096 | } |
| 1097 | else { |
| 1098 | *ucs4_out++ = *iter; |
| 1099 | iter++; |
| 1100 | } |
| 1101 | } |
| 1102 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1103 | _PyUnicode_GET_LENGTH(unicode))); |
| 1104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1105 | } |
| 1106 | #endif |
| 1107 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1108 | static int |
| 1109 | _PyUnicode_Dirty(PyObject *unicode) |
| 1110 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1111 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1112 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1113 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1114 | "Cannot modify a string having more than 1 reference"); |
| 1115 | return -1; |
| 1116 | } |
| 1117 | _PyUnicode_DIRTY(unicode); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1121 | static int |
| 1122 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1123 | PyObject *from, Py_ssize_t from_start, |
| 1124 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1125 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1126 | unsigned int from_kind, to_kind; |
| 1127 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1128 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1129 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1130 | assert(PyUnicode_Check(from)); |
| 1131 | assert(PyUnicode_Check(to)); |
| 1132 | assert(PyUnicode_IS_READY(from)); |
| 1133 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1135 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1136 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1137 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1138 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1139 | if (how_many == 0) |
| 1140 | return 0; |
| 1141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1143 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1145 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1147 | #ifdef Py_DEBUG |
| 1148 | if (!check_maxchar |
| 1149 | && (from_kind > to_kind |
| 1150 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1151 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1152 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1153 | Py_UCS4 ch; |
| 1154 | Py_ssize_t i; |
| 1155 | for (i=0; i < how_many; i++) { |
| 1156 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1157 | assert(ch <= to_maxchar); |
| 1158 | } |
| 1159 | } |
| 1160 | #endif |
| 1161 | fast = (from_kind == to_kind); |
| 1162 | if (check_maxchar |
| 1163 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1164 | { |
| 1165 | /* deny latin1 => ascii */ |
| 1166 | fast = 0; |
| 1167 | } |
| 1168 | |
| 1169 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1170 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1171 | (char*)from_data + from_kind * from_start, |
| 1172 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1174 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1175 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1176 | { |
| 1177 | _PyUnicode_CONVERT_BYTES( |
| 1178 | Py_UCS1, Py_UCS2, |
| 1179 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1180 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1181 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1182 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1183 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1184 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1185 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1186 | { |
| 1187 | _PyUnicode_CONVERT_BYTES( |
| 1188 | Py_UCS1, Py_UCS4, |
| 1189 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1190 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1191 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1192 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1193 | } |
| 1194 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1195 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1196 | { |
| 1197 | _PyUnicode_CONVERT_BYTES( |
| 1198 | Py_UCS2, Py_UCS4, |
| 1199 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1200 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1201 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1202 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1203 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1204 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1205 | /* check if max_char(from substring) <= max_char(to) */ |
| 1206 | if (from_kind > to_kind |
| 1207 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1208 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1209 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1210 | /* slow path to check for character overflow */ |
| 1211 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1212 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1213 | Py_ssize_t i; |
| 1214 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1215 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1216 | for (i=0; i < how_many; i++) { |
| 1217 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1218 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1219 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1220 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1221 | #else |
| 1222 | if (!check_maxchar) { |
| 1223 | for (i=0; i < how_many; i++) { |
| 1224 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1225 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1226 | } |
| 1227 | } |
| 1228 | else { |
| 1229 | for (i=0; i < how_many; i++) { |
| 1230 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1231 | if (ch > to_maxchar) |
| 1232 | return 1; |
| 1233 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1237 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1238 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1239 | assert(0 && "inconsistent state"); |
| 1240 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1241 | } |
| 1242 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1248 | PyObject *from, Py_ssize_t from_start, |
| 1249 | Py_ssize_t how_many) |
| 1250 | { |
| 1251 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1252 | } |
| 1253 | |
| 1254 | Py_ssize_t |
| 1255 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1256 | PyObject *from, Py_ssize_t from_start, |
| 1257 | Py_ssize_t how_many) |
| 1258 | { |
| 1259 | int err; |
| 1260 | |
| 1261 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1262 | PyErr_BadInternalCall(); |
| 1263 | return -1; |
| 1264 | } |
| 1265 | |
| 1266 | if (PyUnicode_READY(from)) |
| 1267 | return -1; |
| 1268 | if (PyUnicode_READY(to)) |
| 1269 | return -1; |
| 1270 | |
| 1271 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1272 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1273 | PyErr_Format(PyExc_SystemError, |
| 1274 | "Cannot write %zi characters at %zi " |
| 1275 | "in a string of %zi characters", |
| 1276 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (how_many == 0) |
| 1281 | return 0; |
| 1282 | |
| 1283 | if (_PyUnicode_Dirty(to)) |
| 1284 | return -1; |
| 1285 | |
| 1286 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1287 | if (err) { |
| 1288 | PyErr_Format(PyExc_SystemError, |
| 1289 | "Cannot copy %s characters " |
| 1290 | "into a string of %s characters", |
| 1291 | unicode_kind_name(from), |
| 1292 | unicode_kind_name(to)); |
| 1293 | return -1; |
| 1294 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1295 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1298 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1299 | correct string length can be computed before converting a string to UCS4. |
| 1300 | This function counts single surrogates as a character and not as a pair. |
| 1301 | |
| 1302 | Return 0 on success, or -1 on error. */ |
| 1303 | static int |
| 1304 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1305 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1306 | { |
| 1307 | const wchar_t *iter; |
| 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; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1314 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1316 | #if SIZEOF_WCHAR_T != 2 |
| 1317 | if (*maxchar >= 0x10000) |
| 1318 | return 0; |
| 1319 | #endif |
| 1320 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1321 | #if SIZEOF_WCHAR_T == 2 |
| 1322 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1323 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1324 | { |
| 1325 | Py_UCS4 surrogate_val; |
| 1326 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1327 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1328 | ++(*num_surrogates); |
| 1329 | if (surrogate_val > *maxchar) |
| 1330 | *maxchar = surrogate_val; |
| 1331 | iter += 2; |
| 1332 | } |
| 1333 | else |
| 1334 | iter++; |
| 1335 | #else |
| 1336 | iter++; |
| 1337 | #endif |
| 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)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1501 | |
| 1502 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1503 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1504 | } |
| 1505 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1506 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1507 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1508 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1512 | #ifdef Py_DEBUG |
| 1513 | static int |
| 1514 | unicode_is_singleton(PyObject *unicode) |
| 1515 | { |
| 1516 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1517 | if (unicode == unicode_empty) |
| 1518 | return 1; |
| 1519 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1520 | { |
| 1521 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1522 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1523 | return 1; |
| 1524 | } |
| 1525 | return 0; |
| 1526 | } |
| 1527 | #endif |
| 1528 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1529 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1530 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1531 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1532 | if (Py_REFCNT(unicode) != 1) |
| 1533 | return 0; |
| 1534 | if (PyUnicode_CHECK_INTERNED(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 | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1570 | if (!unicode_resizable(unicode)) { |
| 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)) { |
| 1580 | *p_unicode = resize_compact(unicode, length); |
| 1581 | if (*p_unicode == NULL) |
| 1582 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1583 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1584 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1585 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1586 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1589 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1590 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1591 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | PyObject *unicode; |
| 1593 | if (p_unicode == NULL) { |
| 1594 | PyErr_BadInternalCall(); |
| 1595 | return -1; |
| 1596 | } |
| 1597 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1598 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1599 | { |
| 1600 | PyErr_BadInternalCall(); |
| 1601 | return -1; |
| 1602 | } |
| 1603 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1604 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1605 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1606 | static int |
Victor Stinner | 0a045ef | 2011-11-09 00:02:42 +0100 | [diff] [blame] | 1607 | unicode_widen(PyObject **p_unicode, unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1608 | { |
| 1609 | PyObject *result; |
| 1610 | assert(PyUnicode_IS_READY(*p_unicode)); |
| 1611 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1612 | return 0; |
| 1613 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1614 | maxchar); |
| 1615 | if (result == NULL) |
| 1616 | return -1; |
| 1617 | PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, |
| 1618 | PyUnicode_GET_LENGTH(*p_unicode)); |
| 1619 | Py_DECREF(*p_unicode); |
| 1620 | *p_unicode = result; |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | static int |
| 1625 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1626 | Py_UCS4 ch) |
| 1627 | { |
| 1628 | if (unicode_widen(p_unicode, ch) < 0) |
| 1629 | return -1; |
| 1630 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1631 | PyUnicode_DATA(*p_unicode), |
| 1632 | (*pos)++, ch); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1636 | static PyObject* |
| 1637 | get_latin1_char(unsigned char ch) |
| 1638 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1639 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1640 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1641 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1642 | if (!unicode) |
| 1643 | return NULL; |
| 1644 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1645 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1646 | unicode_latin1[ch] = unicode; |
| 1647 | } |
| 1648 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1649 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1650 | } |
| 1651 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1652 | PyObject * |
| 1653 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1654 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1655 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1656 | Py_UCS4 maxchar = 0; |
| 1657 | Py_ssize_t num_surrogates; |
| 1658 | |
| 1659 | if (u == NULL) |
| 1660 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1661 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1662 | /* If the Unicode data is known at construction time, we can apply |
| 1663 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1664 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1665 | /* Optimization for empty strings */ |
| 1666 | if (size == 0 && unicode_empty != NULL) { |
| 1667 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1668 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1669 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1670 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1671 | /* Single character Unicode objects in the Latin-1 range are |
| 1672 | shared when using this constructor */ |
| 1673 | if (size == 1 && *u < 256) |
| 1674 | return get_latin1_char((unsigned char)*u); |
| 1675 | |
| 1676 | /* If not empty and not single character, copy the Unicode data |
| 1677 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1678 | if (find_maxchar_surrogates(u, u + size, |
| 1679 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1680 | return NULL; |
| 1681 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1682 | unicode = PyUnicode_New(size - num_surrogates, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1683 | 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 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1725 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1726 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1727 | some optimizations which share commonly used objects. |
| 1728 | Also, this means the input must be UTF-8, so fall back to the |
| 1729 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1730 | if (u != NULL) { |
| 1731 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1732 | /* Optimization for empty strings */ |
| 1733 | if (size == 0 && unicode_empty != NULL) { |
| 1734 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1735 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1736 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1737 | |
| 1738 | /* Single characters are shared when using this constructor. |
| 1739 | Restrict to ASCII, since the input must be UTF-8. */ |
Victor Stinner | 9faa384 | 2011-10-23 20:06:00 +0200 | [diff] [blame] | 1740 | if (size == 1 && (unsigned char)*u < 128) |
| 1741 | return get_latin1_char((unsigned char)*u); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1742 | |
| 1743 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1746 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1749 | PyObject * |
| 1750 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1751 | { |
| 1752 | size_t size = strlen(u); |
| 1753 | if (size > PY_SSIZE_T_MAX) { |
| 1754 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1755 | return NULL; |
| 1756 | } |
| 1757 | |
| 1758 | return PyUnicode_FromStringAndSize(u, size); |
| 1759 | } |
| 1760 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1761 | PyObject * |
| 1762 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1763 | { |
| 1764 | if (!id->object) { |
| 1765 | id->object = PyUnicode_FromString(id->string); |
| 1766 | if (!id->object) |
| 1767 | return NULL; |
| 1768 | PyUnicode_InternInPlace(&id->object); |
| 1769 | assert(!id->next); |
| 1770 | id->next = static_strings; |
| 1771 | static_strings = id; |
| 1772 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1773 | return id->object; |
| 1774 | } |
| 1775 | |
| 1776 | void |
| 1777 | _PyUnicode_ClearStaticStrings() |
| 1778 | { |
| 1779 | _Py_Identifier *i; |
| 1780 | for (i = static_strings; i; i = i->next) { |
| 1781 | Py_DECREF(i->object); |
| 1782 | i->object = NULL; |
| 1783 | i->next = NULL; |
| 1784 | } |
| 1785 | } |
| 1786 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1787 | /* Internal function, don't check maximum character */ |
| 1788 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1789 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1790 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1791 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1792 | PyObject *res; |
| 1793 | #ifdef Py_DEBUG |
| 1794 | const unsigned char *p; |
| 1795 | const unsigned char *end = s + size; |
| 1796 | for (p=s; p < end; p++) { |
| 1797 | assert(*p < 128); |
| 1798 | } |
| 1799 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1800 | if (size == 1) |
| 1801 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1802 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1803 | if (!res) |
| 1804 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1805 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1806 | return res; |
| 1807 | } |
| 1808 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1809 | static Py_UCS4 |
| 1810 | kind_maxchar_limit(unsigned int kind) |
| 1811 | { |
| 1812 | switch(kind) { |
| 1813 | case PyUnicode_1BYTE_KIND: |
| 1814 | return 0x80; |
| 1815 | case PyUnicode_2BYTE_KIND: |
| 1816 | return 0x100; |
| 1817 | case PyUnicode_4BYTE_KIND: |
| 1818 | return 0x10000; |
| 1819 | default: |
| 1820 | assert(0 && "invalid kind"); |
| 1821 | return 0x10ffff; |
| 1822 | } |
| 1823 | } |
| 1824 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1825 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1826 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1827 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1828 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1829 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1830 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1831 | if (size == 0) { |
| 1832 | Py_INCREF(unicode_empty); |
| 1833 | return unicode_empty; |
| 1834 | } |
| 1835 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1836 | if (size == 1) |
| 1837 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1838 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1839 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1840 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1841 | if (!res) |
| 1842 | return NULL; |
| 1843 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1844 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1848 | static PyObject* |
| 1849 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1850 | { |
| 1851 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1852 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1853 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1854 | if (size == 0) { |
| 1855 | Py_INCREF(unicode_empty); |
| 1856 | return unicode_empty; |
| 1857 | } |
| 1858 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1859 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1860 | return get_latin1_char((unsigned char)u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1861 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1862 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1863 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1864 | if (!res) |
| 1865 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1866 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1867 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1868 | else { |
| 1869 | _PyUnicode_CONVERT_BYTES( |
| 1870 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1871 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1872 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | return res; |
| 1874 | } |
| 1875 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1876 | static PyObject* |
| 1877 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1878 | { |
| 1879 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1880 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1881 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1882 | if (size == 0) { |
| 1883 | Py_INCREF(unicode_empty); |
| 1884 | return unicode_empty; |
| 1885 | } |
| 1886 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1887 | if (size == 1 && u[0] < 256) |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1888 | return get_latin1_char((unsigned char)u[0]); |
| 1889 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1890 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1891 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | if (!res) |
| 1893 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1894 | if (max_char < 256) |
| 1895 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1896 | PyUnicode_1BYTE_DATA(res)); |
| 1897 | else if (max_char < 0x10000) |
| 1898 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1899 | PyUnicode_2BYTE_DATA(res)); |
| 1900 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1901 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1902 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | return res; |
| 1904 | } |
| 1905 | |
| 1906 | PyObject* |
| 1907 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1908 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1909 | if (size < 0) { |
| 1910 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1911 | return NULL; |
| 1912 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1913 | switch(kind) { |
| 1914 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1915 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1916 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1917 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1918 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1919 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1920 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1921 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1922 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1926 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1927 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1928 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1929 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1930 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1931 | { |
| 1932 | PyObject *unicode, *copy; |
| 1933 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1934 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1935 | unsigned int kind; |
| 1936 | |
| 1937 | assert(p_unicode != NULL); |
| 1938 | unicode = *p_unicode; |
| 1939 | assert(PyUnicode_IS_READY(unicode)); |
| 1940 | if (PyUnicode_IS_ASCII(unicode)) |
| 1941 | return; |
| 1942 | |
| 1943 | len = PyUnicode_GET_LENGTH(unicode); |
| 1944 | kind = PyUnicode_KIND(unicode); |
| 1945 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1946 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1947 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1948 | if (max_char >= 128) |
| 1949 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1950 | } |
| 1951 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1952 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1953 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1954 | if (max_char >= 256) |
| 1955 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1956 | } |
| 1957 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1958 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1959 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1960 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1961 | if (max_char >= 0x10000) |
| 1962 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1963 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1964 | copy = PyUnicode_New(len, max_char); |
| 1965 | copy_characters(copy, 0, unicode, 0, len); |
| 1966 | Py_DECREF(unicode); |
| 1967 | *p_unicode = copy; |
| 1968 | } |
| 1969 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1970 | PyObject* |
| 1971 | PyUnicode_Copy(PyObject *unicode) |
| 1972 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1973 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1974 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1975 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1976 | if (!PyUnicode_Check(unicode)) { |
| 1977 | PyErr_BadInternalCall(); |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | if (PyUnicode_READY(unicode)) |
| 1981 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1982 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1983 | length = PyUnicode_GET_LENGTH(unicode); |
| 1984 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1985 | if (!copy) |
| 1986 | return NULL; |
| 1987 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1988 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1989 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1990 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1991 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1992 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1996 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1997 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1998 | |
| 1999 | void* |
| 2000 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2001 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2002 | Py_ssize_t len; |
| 2003 | void *result; |
| 2004 | unsigned int skind; |
| 2005 | |
| 2006 | if (PyUnicode_READY(s)) |
| 2007 | return NULL; |
| 2008 | |
| 2009 | len = PyUnicode_GET_LENGTH(s); |
| 2010 | skind = PyUnicode_KIND(s); |
| 2011 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2012 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2013 | return NULL; |
| 2014 | } |
| 2015 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2016 | case PyUnicode_2BYTE_KIND: |
| 2017 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2018 | if (!result) |
| 2019 | return PyErr_NoMemory(); |
| 2020 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2021 | _PyUnicode_CONVERT_BYTES( |
| 2022 | Py_UCS1, Py_UCS2, |
| 2023 | PyUnicode_1BYTE_DATA(s), |
| 2024 | PyUnicode_1BYTE_DATA(s) + len, |
| 2025 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2026 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2027 | case PyUnicode_4BYTE_KIND: |
| 2028 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2029 | if (!result) |
| 2030 | return PyErr_NoMemory(); |
| 2031 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2032 | _PyUnicode_CONVERT_BYTES( |
| 2033 | Py_UCS2, Py_UCS4, |
| 2034 | PyUnicode_2BYTE_DATA(s), |
| 2035 | PyUnicode_2BYTE_DATA(s) + len, |
| 2036 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2037 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2038 | else { |
| 2039 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2040 | _PyUnicode_CONVERT_BYTES( |
| 2041 | Py_UCS1, Py_UCS4, |
| 2042 | PyUnicode_1BYTE_DATA(s), |
| 2043 | PyUnicode_1BYTE_DATA(s) + len, |
| 2044 | result); |
| 2045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2046 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2047 | default: |
| 2048 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2051 | return NULL; |
| 2052 | } |
| 2053 | |
| 2054 | static Py_UCS4* |
| 2055 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2056 | int copy_null) |
| 2057 | { |
| 2058 | int kind; |
| 2059 | void *data; |
| 2060 | Py_ssize_t len, targetlen; |
| 2061 | if (PyUnicode_READY(string) == -1) |
| 2062 | return NULL; |
| 2063 | kind = PyUnicode_KIND(string); |
| 2064 | data = PyUnicode_DATA(string); |
| 2065 | len = PyUnicode_GET_LENGTH(string); |
| 2066 | targetlen = len; |
| 2067 | if (copy_null) |
| 2068 | targetlen++; |
| 2069 | if (!target) { |
| 2070 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2071 | PyErr_NoMemory(); |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2075 | if (!target) { |
| 2076 | PyErr_NoMemory(); |
| 2077 | return NULL; |
| 2078 | } |
| 2079 | } |
| 2080 | else { |
| 2081 | if (targetsize < targetlen) { |
| 2082 | PyErr_Format(PyExc_SystemError, |
| 2083 | "string is longer than the buffer"); |
| 2084 | if (copy_null && 0 < targetsize) |
| 2085 | target[0] = 0; |
| 2086 | return NULL; |
| 2087 | } |
| 2088 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2089 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2090 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2091 | _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] | 2092 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2093 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2094 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2095 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2096 | } |
| 2097 | else { |
| 2098 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2099 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2100 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2101 | if (copy_null) |
| 2102 | target[len] = 0; |
| 2103 | return target; |
| 2104 | } |
| 2105 | |
| 2106 | Py_UCS4* |
| 2107 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2108 | int copy_null) |
| 2109 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2110 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2111 | PyErr_BadInternalCall(); |
| 2112 | return NULL; |
| 2113 | } |
| 2114 | return as_ucs4(string, target, targetsize, copy_null); |
| 2115 | } |
| 2116 | |
| 2117 | Py_UCS4* |
| 2118 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2119 | { |
| 2120 | return as_ucs4(string, NULL, 0, 1); |
| 2121 | } |
| 2122 | |
| 2123 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2124 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2125 | PyObject * |
| 2126 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2127 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2128 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2129 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2130 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2131 | PyErr_BadInternalCall(); |
| 2132 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2135 | if (size == -1) { |
| 2136 | size = wcslen(w); |
| 2137 | } |
| 2138 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2139 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2142 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2143 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2144 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2145 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2146 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2147 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2148 | *fmt++ = '%'; |
| 2149 | if (width) { |
| 2150 | if (zeropad) |
| 2151 | *fmt++ = '0'; |
| 2152 | fmt += sprintf(fmt, "%d", width); |
| 2153 | } |
| 2154 | if (precision) |
| 2155 | fmt += sprintf(fmt, ".%d", precision); |
| 2156 | if (longflag) |
| 2157 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2158 | else if (longlongflag) { |
| 2159 | /* longlongflag should only ever be nonzero on machines with |
| 2160 | HAVE_LONG_LONG defined */ |
| 2161 | #ifdef HAVE_LONG_LONG |
| 2162 | char *f = PY_FORMAT_LONG_LONG; |
| 2163 | while (*f) |
| 2164 | *fmt++ = *f++; |
| 2165 | #else |
| 2166 | /* we shouldn't ever get here */ |
| 2167 | assert(0); |
| 2168 | *fmt++ = 'l'; |
| 2169 | #endif |
| 2170 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2171 | else if (size_tflag) { |
| 2172 | char *f = PY_FORMAT_SIZE_T; |
| 2173 | while (*f) |
| 2174 | *fmt++ = *f++; |
| 2175 | } |
| 2176 | *fmt++ = c; |
| 2177 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2180 | /* helper for PyUnicode_FromFormatV() */ |
| 2181 | |
| 2182 | static const char* |
| 2183 | parse_format_flags(const char *f, |
| 2184 | int *p_width, int *p_precision, |
| 2185 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2186 | { |
| 2187 | int width, precision, longflag, longlongflag, size_tflag; |
| 2188 | |
| 2189 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2190 | f++; |
| 2191 | width = 0; |
| 2192 | while (Py_ISDIGIT((unsigned)*f)) |
| 2193 | width = (width*10) + *f++ - '0'; |
| 2194 | precision = 0; |
| 2195 | if (*f == '.') { |
| 2196 | f++; |
| 2197 | while (Py_ISDIGIT((unsigned)*f)) |
| 2198 | precision = (precision*10) + *f++ - '0'; |
| 2199 | if (*f == '%') { |
| 2200 | /* "%.3%s" => f points to "3" */ |
| 2201 | f--; |
| 2202 | } |
| 2203 | } |
| 2204 | if (*f == '\0') { |
| 2205 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2206 | f--; |
| 2207 | } |
| 2208 | if (p_width != NULL) |
| 2209 | *p_width = width; |
| 2210 | if (p_precision != NULL) |
| 2211 | *p_precision = precision; |
| 2212 | |
| 2213 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2214 | longflag = 0; |
| 2215 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2216 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2219 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2220 | longflag = 1; |
| 2221 | ++f; |
| 2222 | } |
| 2223 | #ifdef HAVE_LONG_LONG |
| 2224 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2225 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2226 | longlongflag = 1; |
| 2227 | f += 2; |
| 2228 | } |
| 2229 | #endif |
| 2230 | } |
| 2231 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2232 | 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] | 2233 | size_tflag = 1; |
| 2234 | ++f; |
| 2235 | } |
| 2236 | if (p_longflag != NULL) |
| 2237 | *p_longflag = longflag; |
| 2238 | if (p_longlongflag != NULL) |
| 2239 | *p_longlongflag = longlongflag; |
| 2240 | if (p_size_tflag != NULL) |
| 2241 | *p_size_tflag = size_tflag; |
| 2242 | return f; |
| 2243 | } |
| 2244 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2245 | /* maximum number of characters required for output of %ld. 21 characters |
| 2246 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2247 | #define MAX_LONG_CHARS 21 |
| 2248 | /* maximum number of characters required for output of %lld. |
| 2249 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2250 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2251 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2252 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2253 | PyObject * |
| 2254 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2255 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2256 | va_list count; |
| 2257 | Py_ssize_t callcount = 0; |
| 2258 | PyObject **callresults = NULL; |
| 2259 | PyObject **callresult = NULL; |
| 2260 | Py_ssize_t n = 0; |
| 2261 | int width = 0; |
| 2262 | int precision = 0; |
| 2263 | int zeropad; |
| 2264 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2265 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2266 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2267 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2268 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2269 | Py_UCS4 argmaxchar; |
| 2270 | Py_ssize_t numbersize = 0; |
| 2271 | char *numberresults = NULL; |
| 2272 | char *numberresult = NULL; |
| 2273 | Py_ssize_t i; |
| 2274 | int kind; |
| 2275 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2276 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2277 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2278 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2279 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2280 | * 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] | 2281 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2282 | * also estimate a upper bound for all the number formats in the string, |
| 2283 | * 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] | 2284 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | for (f = format; *f; f++) { |
| 2286 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2287 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2288 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2289 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2290 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2291 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2293 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2294 | #ifdef HAVE_LONG_LONG |
| 2295 | if (longlongflag) { |
| 2296 | if (width < MAX_LONG_LONG_CHARS) |
| 2297 | width = MAX_LONG_LONG_CHARS; |
| 2298 | } |
| 2299 | else |
| 2300 | #endif |
| 2301 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2302 | including sign. Decimal takes the most space. This |
| 2303 | isn't enough for octal. If a width is specified we |
| 2304 | need more (which we allocate later). */ |
| 2305 | if (width < MAX_LONG_CHARS) |
| 2306 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2307 | |
| 2308 | /* account for the size + '\0' to separate numbers |
| 2309 | inside of the numberresults buffer */ |
| 2310 | numbersize += (width + 1); |
| 2311 | } |
| 2312 | } |
| 2313 | else if ((unsigned char)*f > 127) { |
| 2314 | PyErr_Format(PyExc_ValueError, |
| 2315 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2316 | "string, got a non-ASCII byte: 0x%02x", |
| 2317 | (unsigned char)*f); |
| 2318 | return NULL; |
| 2319 | } |
| 2320 | } |
| 2321 | /* step 2: allocate memory for the results of |
| 2322 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2323 | if (callcount) { |
| 2324 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2325 | if (!callresults) { |
| 2326 | PyErr_NoMemory(); |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | callresult = callresults; |
| 2330 | } |
| 2331 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2332 | if (numbersize) { |
| 2333 | numberresults = PyObject_Malloc(numbersize); |
| 2334 | if (!numberresults) { |
| 2335 | PyErr_NoMemory(); |
| 2336 | goto fail; |
| 2337 | } |
| 2338 | numberresult = numberresults; |
| 2339 | } |
| 2340 | |
| 2341 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2342 | for (f = format; *f; f++) { |
| 2343 | if (*f == '%') { |
| 2344 | const char* p; |
| 2345 | int longflag; |
| 2346 | int longlongflag; |
| 2347 | int size_tflag; |
| 2348 | int numprinted; |
| 2349 | |
| 2350 | p = f; |
| 2351 | zeropad = (f[1] == '0'); |
| 2352 | f = parse_format_flags(f, &width, &precision, |
| 2353 | &longflag, &longlongflag, &size_tflag); |
| 2354 | switch (*f) { |
| 2355 | case 'c': |
| 2356 | { |
| 2357 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2358 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2359 | n++; |
| 2360 | break; |
| 2361 | } |
| 2362 | case '%': |
| 2363 | n++; |
| 2364 | break; |
| 2365 | case 'i': |
| 2366 | case 'd': |
| 2367 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2368 | width, precision, *f); |
| 2369 | if (longflag) |
| 2370 | numprinted = sprintf(numberresult, fmt, |
| 2371 | va_arg(count, long)); |
| 2372 | #ifdef HAVE_LONG_LONG |
| 2373 | else if (longlongflag) |
| 2374 | numprinted = sprintf(numberresult, fmt, |
| 2375 | va_arg(count, PY_LONG_LONG)); |
| 2376 | #endif |
| 2377 | else if (size_tflag) |
| 2378 | numprinted = sprintf(numberresult, fmt, |
| 2379 | va_arg(count, Py_ssize_t)); |
| 2380 | else |
| 2381 | numprinted = sprintf(numberresult, fmt, |
| 2382 | va_arg(count, int)); |
| 2383 | n += numprinted; |
| 2384 | /* advance by +1 to skip over the '\0' */ |
| 2385 | numberresult += (numprinted + 1); |
| 2386 | assert(*(numberresult - 1) == '\0'); |
| 2387 | assert(*(numberresult - 2) != '\0'); |
| 2388 | assert(numprinted >= 0); |
| 2389 | assert(numberresult <= numberresults + numbersize); |
| 2390 | break; |
| 2391 | case 'u': |
| 2392 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2393 | width, precision, 'u'); |
| 2394 | if (longflag) |
| 2395 | numprinted = sprintf(numberresult, fmt, |
| 2396 | va_arg(count, unsigned long)); |
| 2397 | #ifdef HAVE_LONG_LONG |
| 2398 | else if (longlongflag) |
| 2399 | numprinted = sprintf(numberresult, fmt, |
| 2400 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2401 | #endif |
| 2402 | else if (size_tflag) |
| 2403 | numprinted = sprintf(numberresult, fmt, |
| 2404 | va_arg(count, size_t)); |
| 2405 | else |
| 2406 | numprinted = sprintf(numberresult, fmt, |
| 2407 | va_arg(count, unsigned int)); |
| 2408 | n += numprinted; |
| 2409 | numberresult += (numprinted + 1); |
| 2410 | assert(*(numberresult - 1) == '\0'); |
| 2411 | assert(*(numberresult - 2) != '\0'); |
| 2412 | assert(numprinted >= 0); |
| 2413 | assert(numberresult <= numberresults + numbersize); |
| 2414 | break; |
| 2415 | case 'x': |
| 2416 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2417 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2418 | n += numprinted; |
| 2419 | numberresult += (numprinted + 1); |
| 2420 | assert(*(numberresult - 1) == '\0'); |
| 2421 | assert(*(numberresult - 2) != '\0'); |
| 2422 | assert(numprinted >= 0); |
| 2423 | assert(numberresult <= numberresults + numbersize); |
| 2424 | break; |
| 2425 | case 'p': |
| 2426 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2427 | /* %p is ill-defined: ensure leading 0x. */ |
| 2428 | if (numberresult[1] == 'X') |
| 2429 | numberresult[1] = 'x'; |
| 2430 | else if (numberresult[1] != 'x') { |
| 2431 | memmove(numberresult + 2, numberresult, |
| 2432 | strlen(numberresult) + 1); |
| 2433 | numberresult[0] = '0'; |
| 2434 | numberresult[1] = 'x'; |
| 2435 | numprinted += 2; |
| 2436 | } |
| 2437 | n += numprinted; |
| 2438 | numberresult += (numprinted + 1); |
| 2439 | assert(*(numberresult - 1) == '\0'); |
| 2440 | assert(*(numberresult - 2) != '\0'); |
| 2441 | assert(numprinted >= 0); |
| 2442 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2443 | break; |
| 2444 | case 's': |
| 2445 | { |
| 2446 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2447 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2448 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2449 | if (!str) |
| 2450 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2451 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2452 | unicode objects, there is no need to call ready on them */ |
| 2453 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2454 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2456 | /* Remember the str and switch to the next slot */ |
| 2457 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2458 | break; |
| 2459 | } |
| 2460 | case 'U': |
| 2461 | { |
| 2462 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2463 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2464 | if (PyUnicode_READY(obj) == -1) |
| 2465 | goto fail; |
| 2466 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2467 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2468 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2469 | break; |
| 2470 | } |
| 2471 | case 'V': |
| 2472 | { |
| 2473 | PyObject *obj = va_arg(count, PyObject *); |
| 2474 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2475 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2476 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2477 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2478 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2479 | if (PyUnicode_READY(obj) == -1) |
| 2480 | goto fail; |
| 2481 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2482 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2483 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2484 | *callresult++ = NULL; |
| 2485 | } |
| 2486 | else { |
| 2487 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2488 | if (!str_obj) |
| 2489 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2490 | if (PyUnicode_READY(str_obj)) { |
| 2491 | Py_DECREF(str_obj); |
| 2492 | goto fail; |
| 2493 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2494 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2495 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2496 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2497 | *callresult++ = str_obj; |
| 2498 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2499 | break; |
| 2500 | } |
| 2501 | case 'S': |
| 2502 | { |
| 2503 | PyObject *obj = va_arg(count, PyObject *); |
| 2504 | PyObject *str; |
| 2505 | assert(obj); |
| 2506 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2507 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2509 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2510 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2511 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2512 | /* Remember the str and switch to the next slot */ |
| 2513 | *callresult++ = str; |
| 2514 | break; |
| 2515 | } |
| 2516 | case 'R': |
| 2517 | { |
| 2518 | PyObject *obj = va_arg(count, PyObject *); |
| 2519 | PyObject *repr; |
| 2520 | assert(obj); |
| 2521 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2523 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2524 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2525 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2526 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2527 | /* Remember the repr and switch to the next slot */ |
| 2528 | *callresult++ = repr; |
| 2529 | break; |
| 2530 | } |
| 2531 | case 'A': |
| 2532 | { |
| 2533 | PyObject *obj = va_arg(count, PyObject *); |
| 2534 | PyObject *ascii; |
| 2535 | assert(obj); |
| 2536 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2537 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2538 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2539 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2540 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2541 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2542 | /* Remember the repr and switch to the next slot */ |
| 2543 | *callresult++ = ascii; |
| 2544 | break; |
| 2545 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2546 | default: |
| 2547 | /* if we stumble upon an unknown |
| 2548 | formatting code, copy the rest of |
| 2549 | the format string to the output |
| 2550 | string. (we cannot just skip the |
| 2551 | code, since there's no way to know |
| 2552 | what's in the argument list) */ |
| 2553 | n += strlen(p); |
| 2554 | goto expand; |
| 2555 | } |
| 2556 | } else |
| 2557 | n++; |
| 2558 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2559 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2560 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2561 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2562 | we don't have to resize the string. |
| 2563 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2564 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | if (!string) |
| 2566 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2567 | kind = PyUnicode_KIND(string); |
| 2568 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2569 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2570 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2571 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2572 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2573 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2574 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2575 | |
| 2576 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2578 | /* checking for == because the last argument could be a empty |
| 2579 | string, which causes i to point to end, the assert at the end of |
| 2580 | the loop */ |
| 2581 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2582 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2583 | switch (*f) { |
| 2584 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2586 | const int ordinal = va_arg(vargs, int); |
| 2587 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2589 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2590 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2592 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2593 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2594 | case 'p': |
| 2595 | /* unused, since we already have the result */ |
| 2596 | if (*f == 'p') |
| 2597 | (void) va_arg(vargs, void *); |
| 2598 | else |
| 2599 | (void) va_arg(vargs, int); |
| 2600 | /* extract the result from numberresults and append. */ |
| 2601 | for (; *numberresult; ++i, ++numberresult) |
| 2602 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2603 | /* skip over the separating '\0' */ |
| 2604 | assert(*numberresult == '\0'); |
| 2605 | numberresult++; |
| 2606 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2607 | break; |
| 2608 | case 's': |
| 2609 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2610 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2611 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2612 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2613 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2614 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2615 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2617 | /* We're done with the unicode()/repr() => forget it */ |
| 2618 | Py_DECREF(*callresult); |
| 2619 | /* switch to next unicode()/repr() result */ |
| 2620 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | case 'U': |
| 2624 | { |
| 2625 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2626 | Py_ssize_t size; |
| 2627 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2628 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2629 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2630 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2631 | break; |
| 2632 | } |
| 2633 | case 'V': |
| 2634 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2636 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2637 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | size = PyUnicode_GET_LENGTH(obj); |
| 2640 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2641 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2642 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2643 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2644 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2645 | assert(PyUnicode_KIND(*callresult) <= |
| 2646 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2647 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2648 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2649 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2650 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2651 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2652 | break; |
| 2653 | } |
| 2654 | case 'S': |
| 2655 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2656 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2657 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2658 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2659 | /* unused, since we already have the result */ |
| 2660 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2661 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2662 | copy_characters(string, i, *callresult, 0, size); |
| 2663 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2664 | /* We're done with the unicode()/repr() => forget it */ |
| 2665 | Py_DECREF(*callresult); |
| 2666 | /* switch to next unicode()/repr() result */ |
| 2667 | ++callresult; |
| 2668 | break; |
| 2669 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2670 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2671 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2674 | for (; *p; ++p, ++i) |
| 2675 | PyUnicode_WRITE(kind, data, i, *p); |
| 2676 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | goto end; |
| 2678 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2679 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2680 | else { |
| 2681 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2682 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2683 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2685 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2687 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2688 | if (callresults) |
| 2689 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2690 | if (numberresults) |
| 2691 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2692 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | if (callresults) { |
| 2695 | PyObject **callresult2 = callresults; |
| 2696 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2697 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2698 | ++callresult2; |
| 2699 | } |
| 2700 | PyObject_Free(callresults); |
| 2701 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2702 | if (numberresults) |
| 2703 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2704 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2707 | PyObject * |
| 2708 | PyUnicode_FromFormat(const char *format, ...) |
| 2709 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2710 | PyObject* ret; |
| 2711 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2712 | |
| 2713 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2714 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2715 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2716 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2717 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2718 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2719 | va_end(vargs); |
| 2720 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | #ifdef HAVE_WCHAR_H |
| 2724 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2725 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2726 | convert a Unicode object to a wide character string. |
| 2727 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2728 | - 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] | 2729 | character) required to convert the unicode object. Ignore size argument. |
| 2730 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2731 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2732 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2733 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2734 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2735 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2736 | wchar_t *w, |
| 2737 | Py_ssize_t size) |
| 2738 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2739 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2740 | const wchar_t *wstr; |
| 2741 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2742 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2743 | if (wstr == NULL) |
| 2744 | return -1; |
| 2745 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2746 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2747 | if (size > res) |
| 2748 | size = res + 1; |
| 2749 | else |
| 2750 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2752 | return res; |
| 2753 | } |
| 2754 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2755 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2759 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2760 | wchar_t *w, |
| 2761 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | { |
| 2763 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2764 | PyErr_BadInternalCall(); |
| 2765 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2767 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2770 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2771 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2772 | Py_ssize_t *size) |
| 2773 | { |
| 2774 | wchar_t* buffer; |
| 2775 | Py_ssize_t buflen; |
| 2776 | |
| 2777 | if (unicode == NULL) { |
| 2778 | PyErr_BadInternalCall(); |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2782 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | if (buflen == -1) |
| 2784 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2785 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2786 | PyErr_NoMemory(); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2790 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2791 | if (buffer == NULL) { |
| 2792 | PyErr_NoMemory(); |
| 2793 | return NULL; |
| 2794 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2795 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2796 | if (buflen == -1) |
| 2797 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2798 | if (size != NULL) |
| 2799 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2800 | return buffer; |
| 2801 | } |
| 2802 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2803 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2805 | PyObject * |
| 2806 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2807 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2808 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2809 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2810 | PyErr_SetString(PyExc_ValueError, |
| 2811 | "chr() arg not in range(0x110000)"); |
| 2812 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2813 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2815 | if (ordinal < 256) |
| 2816 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2818 | v = PyUnicode_New(1, ordinal); |
| 2819 | if (v == NULL) |
| 2820 | return NULL; |
| 2821 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2822 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2823 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2826 | PyObject * |
| 2827 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2828 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2829 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2830 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2831 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2832 | if (PyUnicode_READY(obj)) |
| 2833 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2834 | Py_INCREF(obj); |
| 2835 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2836 | } |
| 2837 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | /* For a Unicode subtype that's not a Unicode object, |
| 2839 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2840 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2841 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2842 | PyErr_Format(PyExc_TypeError, |
| 2843 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2844 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2845 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2848 | PyObject * |
| 2849 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2850 | const char *encoding, |
| 2851 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2852 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2853 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2854 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2856 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2857 | PyErr_BadInternalCall(); |
| 2858 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2859 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2861 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2862 | if (PyBytes_Check(obj)) { |
| 2863 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2864 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2865 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2866 | } |
| 2867 | else { |
| 2868 | v = PyUnicode_Decode( |
| 2869 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2870 | encoding, errors); |
| 2871 | } |
| 2872 | return v; |
| 2873 | } |
| 2874 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2875 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2876 | PyErr_SetString(PyExc_TypeError, |
| 2877 | "decoding str is not supported"); |
| 2878 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2879 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2881 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2882 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2883 | PyErr_Format(PyExc_TypeError, |
| 2884 | "coercing to str: need bytes, bytearray " |
| 2885 | "or buffer-like object, %.80s found", |
| 2886 | Py_TYPE(obj)->tp_name); |
| 2887 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2888 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2889 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2890 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2891 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2892 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2893 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2894 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2895 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2896 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2897 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2898 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2901 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2902 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2903 | 1 on success. */ |
| 2904 | static int |
| 2905 | normalize_encoding(const char *encoding, |
| 2906 | char *lower, |
| 2907 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2909 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2910 | char *l; |
| 2911 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2912 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2913 | if (encoding == NULL) { |
| 2914 | strcpy(lower, "utf-8"); |
| 2915 | return 1; |
| 2916 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2917 | e = encoding; |
| 2918 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2919 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2920 | while (*e) { |
| 2921 | if (l == l_end) |
| 2922 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2923 | if (Py_ISUPPER(*e)) { |
| 2924 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2925 | } |
| 2926 | else if (*e == '_') { |
| 2927 | *l++ = '-'; |
| 2928 | e++; |
| 2929 | } |
| 2930 | else { |
| 2931 | *l++ = *e++; |
| 2932 | } |
| 2933 | } |
| 2934 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2935 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2938 | PyObject * |
| 2939 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2940 | Py_ssize_t size, |
| 2941 | const char *encoding, |
| 2942 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2943 | { |
| 2944 | PyObject *buffer = NULL, *unicode; |
| 2945 | Py_buffer info; |
| 2946 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2947 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2948 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2949 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2950 | if ((strcmp(lower, "utf-8") == 0) || |
| 2951 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2952 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2953 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2954 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2955 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2956 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2957 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2958 | else if (strcmp(lower, "mbcs") == 0) |
| 2959 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2960 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2961 | else if (strcmp(lower, "ascii") == 0) |
| 2962 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2963 | else if (strcmp(lower, "utf-16") == 0) |
| 2964 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2965 | else if (strcmp(lower, "utf-32") == 0) |
| 2966 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | |
| 2969 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2970 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2971 | 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] | 2972 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2973 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2974 | if (buffer == NULL) |
| 2975 | goto onError; |
| 2976 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2977 | if (unicode == NULL) |
| 2978 | goto onError; |
| 2979 | if (!PyUnicode_Check(unicode)) { |
| 2980 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2981 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2982 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2983 | Py_DECREF(unicode); |
| 2984 | goto onError; |
| 2985 | } |
| 2986 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2987 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2989 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | Py_XDECREF(buffer); |
| 2991 | return NULL; |
| 2992 | } |
| 2993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2994 | PyObject * |
| 2995 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2996 | const char *encoding, |
| 2997 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2998 | { |
| 2999 | PyObject *v; |
| 3000 | |
| 3001 | if (!PyUnicode_Check(unicode)) { |
| 3002 | PyErr_BadArgument(); |
| 3003 | goto onError; |
| 3004 | } |
| 3005 | |
| 3006 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* Decode via the codec registry */ |
| 3010 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3011 | if (v == NULL) |
| 3012 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3013 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3016 | return NULL; |
| 3017 | } |
| 3018 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3019 | PyObject * |
| 3020 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3021 | const char *encoding, |
| 3022 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3023 | { |
| 3024 | PyObject *v; |
| 3025 | |
| 3026 | if (!PyUnicode_Check(unicode)) { |
| 3027 | PyErr_BadArgument(); |
| 3028 | goto onError; |
| 3029 | } |
| 3030 | |
| 3031 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3032 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* Decode via the codec registry */ |
| 3035 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3036 | if (v == NULL) |
| 3037 | goto onError; |
| 3038 | if (!PyUnicode_Check(v)) { |
| 3039 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3040 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3041 | Py_TYPE(v)->tp_name); |
| 3042 | Py_DECREF(v); |
| 3043 | goto onError; |
| 3044 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3045 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3047 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3048 | return NULL; |
| 3049 | } |
| 3050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3051 | PyObject * |
| 3052 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3053 | Py_ssize_t size, |
| 3054 | const char *encoding, |
| 3055 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | { |
| 3057 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | unicode = PyUnicode_FromUnicode(s, size); |
| 3060 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3061 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3062 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3063 | Py_DECREF(unicode); |
| 3064 | return v; |
| 3065 | } |
| 3066 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3067 | PyObject * |
| 3068 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3069 | const char *encoding, |
| 3070 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3071 | { |
| 3072 | PyObject *v; |
| 3073 | |
| 3074 | if (!PyUnicode_Check(unicode)) { |
| 3075 | PyErr_BadArgument(); |
| 3076 | goto onError; |
| 3077 | } |
| 3078 | |
| 3079 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3080 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* Encode via the codec registry */ |
| 3083 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3084 | if (v == NULL) |
| 3085 | goto onError; |
| 3086 | return v; |
| 3087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3088 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3092 | PyObject * |
| 3093 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3094 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3095 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3096 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3097 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3098 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3099 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3100 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3101 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3102 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3103 | the Python codec requires to encode at least its own filename. Use the C |
| 3104 | version of the locale codec until the codec registry is initialized and |
| 3105 | the Python codec is loaded. |
| 3106 | |
| 3107 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3108 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3109 | subinterpreters. */ |
| 3110 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3111 | return PyUnicode_AsEncodedString(unicode, |
| 3112 | Py_FileSystemDefaultEncoding, |
| 3113 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3114 | } |
| 3115 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3116 | /* locale encoding with surrogateescape */ |
| 3117 | wchar_t *wchar; |
| 3118 | char *bytes; |
| 3119 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3120 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3121 | |
| 3122 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3123 | if (wchar == NULL) |
| 3124 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3125 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3126 | if (bytes == NULL) { |
| 3127 | if (error_pos != (size_t)-1) { |
| 3128 | char *errmsg = strerror(errno); |
| 3129 | PyObject *exc = NULL; |
| 3130 | if (errmsg == NULL) |
| 3131 | errmsg = "Py_wchar2char() failed"; |
| 3132 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3133 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3134 | error_pos, error_pos+1, |
| 3135 | errmsg); |
| 3136 | Py_XDECREF(exc); |
| 3137 | } |
| 3138 | else |
| 3139 | PyErr_NoMemory(); |
| 3140 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3141 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3142 | } |
| 3143 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3144 | |
| 3145 | bytes_obj = PyBytes_FromString(bytes); |
| 3146 | PyMem_Free(bytes); |
| 3147 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3148 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3149 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3152 | PyObject * |
| 3153 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3154 | const char *encoding, |
| 3155 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3156 | { |
| 3157 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3158 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3159 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | if (!PyUnicode_Check(unicode)) { |
| 3161 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3162 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3163 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3164 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3165 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3166 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3167 | if ((strcmp(lower, "utf-8") == 0) || |
| 3168 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3169 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3170 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3171 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3172 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3173 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3174 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3175 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3176 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3177 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3178 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3179 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3180 | else if (strcmp(lower, "mbcs") == 0) |
| 3181 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3182 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3183 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3184 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3185 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3186 | |
| 3187 | /* Encode via the codec registry */ |
| 3188 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3189 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3190 | return NULL; |
| 3191 | |
| 3192 | /* The normal path */ |
| 3193 | if (PyBytes_Check(v)) |
| 3194 | return v; |
| 3195 | |
| 3196 | /* 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] | 3197 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3198 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3199 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3200 | |
| 3201 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3202 | "encoder %s returned bytearray instead of bytes", |
| 3203 | encoding); |
| 3204 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3205 | Py_DECREF(v); |
| 3206 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3207 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3208 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3209 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3210 | Py_DECREF(v); |
| 3211 | return b; |
| 3212 | } |
| 3213 | |
| 3214 | PyErr_Format(PyExc_TypeError, |
| 3215 | "encoder did not return a bytes object (type=%.400s)", |
| 3216 | Py_TYPE(v)->tp_name); |
| 3217 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3218 | return NULL; |
| 3219 | } |
| 3220 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3221 | PyObject * |
| 3222 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3223 | const char *encoding, |
| 3224 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3225 | { |
| 3226 | PyObject *v; |
| 3227 | |
| 3228 | if (!PyUnicode_Check(unicode)) { |
| 3229 | PyErr_BadArgument(); |
| 3230 | goto onError; |
| 3231 | } |
| 3232 | |
| 3233 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3234 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3235 | |
| 3236 | /* Encode via the codec registry */ |
| 3237 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3238 | if (v == NULL) |
| 3239 | goto onError; |
| 3240 | if (!PyUnicode_Check(v)) { |
| 3241 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3242 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3243 | Py_TYPE(v)->tp_name); |
| 3244 | Py_DECREF(v); |
| 3245 | goto onError; |
| 3246 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3247 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3248 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3249 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3250 | return NULL; |
| 3251 | } |
| 3252 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3253 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3254 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3255 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3256 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3257 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3258 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3259 | PyObject* |
| 3260 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3261 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3262 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3263 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3264 | #elif defined(__APPLE__) |
| 3265 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3266 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3267 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3268 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3269 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3270 | the Python codec requires to encode at least its own filename. Use the C |
| 3271 | version of the locale codec until the codec registry is initialized and |
| 3272 | the Python codec is loaded. |
| 3273 | |
| 3274 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3275 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3276 | subinterpreters. */ |
| 3277 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3278 | return PyUnicode_Decode(s, size, |
| 3279 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3280 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3281 | } |
| 3282 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3283 | /* locale encoding with surrogateescape */ |
| 3284 | wchar_t *wchar; |
| 3285 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3286 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3287 | |
| 3288 | if (s[size] != '\0' || size != strlen(s)) { |
| 3289 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3290 | return NULL; |
| 3291 | } |
| 3292 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3293 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3294 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3295 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3296 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3297 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3298 | PyMem_Free(wchar); |
| 3299 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3300 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3301 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3304 | |
| 3305 | int |
| 3306 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3307 | { |
| 3308 | PyObject *output = NULL; |
| 3309 | Py_ssize_t size; |
| 3310 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3311 | if (arg == NULL) { |
| 3312 | Py_DECREF(*(PyObject**)addr); |
| 3313 | return 1; |
| 3314 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3315 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3316 | output = arg; |
| 3317 | Py_INCREF(output); |
| 3318 | } |
| 3319 | else { |
| 3320 | arg = PyUnicode_FromObject(arg); |
| 3321 | if (!arg) |
| 3322 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3323 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3324 | Py_DECREF(arg); |
| 3325 | if (!output) |
| 3326 | return 0; |
| 3327 | if (!PyBytes_Check(output)) { |
| 3328 | Py_DECREF(output); |
| 3329 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3330 | return 0; |
| 3331 | } |
| 3332 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3333 | size = PyBytes_GET_SIZE(output); |
| 3334 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3335 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3336 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3337 | Py_DECREF(output); |
| 3338 | return 0; |
| 3339 | } |
| 3340 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3341 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3345 | int |
| 3346 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3347 | { |
| 3348 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3349 | if (arg == NULL) { |
| 3350 | Py_DECREF(*(PyObject**)addr); |
| 3351 | return 1; |
| 3352 | } |
| 3353 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3354 | if (PyUnicode_READY(arg)) |
| 3355 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3356 | output = arg; |
| 3357 | Py_INCREF(output); |
| 3358 | } |
| 3359 | else { |
| 3360 | arg = PyBytes_FromObject(arg); |
| 3361 | if (!arg) |
| 3362 | return 0; |
| 3363 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3364 | PyBytes_GET_SIZE(arg)); |
| 3365 | Py_DECREF(arg); |
| 3366 | if (!output) |
| 3367 | return 0; |
| 3368 | if (!PyUnicode_Check(output)) { |
| 3369 | Py_DECREF(output); |
| 3370 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3371 | return 0; |
| 3372 | } |
| 3373 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3374 | if (PyUnicode_READY(output) < 0) { |
| 3375 | Py_DECREF(output); |
| 3376 | return 0; |
| 3377 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3378 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3379 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3380 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3381 | Py_DECREF(output); |
| 3382 | return 0; |
| 3383 | } |
| 3384 | *(PyObject**)addr = output; |
| 3385 | return Py_CLEANUP_SUPPORTED; |
| 3386 | } |
| 3387 | |
| 3388 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3389 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3390 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3391 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3392 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3393 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3394 | if (!PyUnicode_Check(unicode)) { |
| 3395 | PyErr_BadArgument(); |
| 3396 | return NULL; |
| 3397 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3398 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3399 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3400 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3401 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3402 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3403 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3404 | if (bytes == NULL) |
| 3405 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3406 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3407 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3408 | Py_DECREF(bytes); |
| 3409 | return NULL; |
| 3410 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3411 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3412 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3413 | PyBytes_AS_STRING(bytes), |
| 3414 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3415 | Py_DECREF(bytes); |
| 3416 | } |
| 3417 | |
| 3418 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3419 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3420 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3424 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3425 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3426 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3427 | } |
| 3428 | |
| 3429 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3430 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3431 | #endif |
| 3432 | |
| 3433 | |
| 3434 | Py_UNICODE * |
| 3435 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3436 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3437 | const unsigned char *one_byte; |
| 3438 | #if SIZEOF_WCHAR_T == 4 |
| 3439 | const Py_UCS2 *two_bytes; |
| 3440 | #else |
| 3441 | const Py_UCS4 *four_bytes; |
| 3442 | const Py_UCS4 *ucs4_end; |
| 3443 | Py_ssize_t num_surrogates; |
| 3444 | #endif |
| 3445 | wchar_t *w; |
| 3446 | wchar_t *wchar_end; |
| 3447 | |
| 3448 | if (!PyUnicode_Check(unicode)) { |
| 3449 | PyErr_BadArgument(); |
| 3450 | return NULL; |
| 3451 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3452 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3453 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3454 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3455 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3456 | |
| 3457 | #ifdef Py_DEBUG |
| 3458 | ++unicode_as_unicode_calls; |
| 3459 | #endif |
| 3460 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3461 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3462 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3463 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3464 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | num_surrogates = 0; |
| 3466 | |
| 3467 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3468 | if (*four_bytes > 0xFFFF) |
| 3469 | ++num_surrogates; |
| 3470 | } |
| 3471 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3472 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3473 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3474 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3475 | PyErr_NoMemory(); |
| 3476 | return NULL; |
| 3477 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3478 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3479 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3480 | w = _PyUnicode_WSTR(unicode); |
| 3481 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3482 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3483 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3484 | if (*four_bytes > 0xFFFF) { |
| 3485 | /* encode surrogate pair in this case */ |
| 3486 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3487 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3488 | } |
| 3489 | else |
| 3490 | *w = *four_bytes; |
| 3491 | |
| 3492 | if (w > wchar_end) { |
| 3493 | assert(0 && "Miscalculated string end"); |
| 3494 | } |
| 3495 | } |
| 3496 | *w = 0; |
| 3497 | #else |
| 3498 | /* sizeof(wchar_t) == 4 */ |
| 3499 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3500 | "should share memory already."); |
| 3501 | return NULL; |
| 3502 | #endif |
| 3503 | } |
| 3504 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3505 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3506 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3507 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3508 | PyErr_NoMemory(); |
| 3509 | return NULL; |
| 3510 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3511 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3512 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3513 | w = _PyUnicode_WSTR(unicode); |
| 3514 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3515 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3516 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3517 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3518 | for (; w < wchar_end; ++one_byte, ++w) |
| 3519 | *w = *one_byte; |
| 3520 | /* null-terminate the wstr */ |
| 3521 | *w = 0; |
| 3522 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3523 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3524 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3525 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3526 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3527 | *w = *two_bytes; |
| 3528 | /* null-terminate the wstr */ |
| 3529 | *w = 0; |
| 3530 | #else |
| 3531 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3532 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3533 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3534 | Py_FatalError("Impossible unicode object state, wstr " |
| 3535 | "and str should share memory already."); |
| 3536 | return NULL; |
| 3537 | #endif |
| 3538 | } |
| 3539 | else { |
| 3540 | assert(0 && "This should never happen."); |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3545 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3546 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3549 | Py_UNICODE * |
| 3550 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3551 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3552 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3556 | Py_ssize_t |
| 3557 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3558 | { |
| 3559 | if (!PyUnicode_Check(unicode)) { |
| 3560 | PyErr_BadArgument(); |
| 3561 | goto onError; |
| 3562 | } |
| 3563 | return PyUnicode_GET_SIZE(unicode); |
| 3564 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3565 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3566 | return -1; |
| 3567 | } |
| 3568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3569 | Py_ssize_t |
| 3570 | PyUnicode_GetLength(PyObject *unicode) |
| 3571 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3572 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3573 | PyErr_BadArgument(); |
| 3574 | return -1; |
| 3575 | } |
| 3576 | |
| 3577 | return PyUnicode_GET_LENGTH(unicode); |
| 3578 | } |
| 3579 | |
| 3580 | Py_UCS4 |
| 3581 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3582 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3583 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3584 | PyErr_BadArgument(); |
| 3585 | return (Py_UCS4)-1; |
| 3586 | } |
| 3587 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3588 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3589 | return (Py_UCS4)-1; |
| 3590 | } |
| 3591 | return PyUnicode_READ_CHAR(unicode, index); |
| 3592 | } |
| 3593 | |
| 3594 | int |
| 3595 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3596 | { |
| 3597 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3598 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3599 | return -1; |
| 3600 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3601 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3602 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3603 | return -1; |
| 3604 | } |
| 3605 | if (_PyUnicode_Dirty(unicode)) |
| 3606 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3607 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3608 | index, ch); |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3612 | const char * |
| 3613 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3614 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3615 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3618 | /* create or adjust a UnicodeDecodeError */ |
| 3619 | static void |
| 3620 | make_decode_exception(PyObject **exceptionObject, |
| 3621 | const char *encoding, |
| 3622 | const char *input, Py_ssize_t length, |
| 3623 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3624 | const char *reason) |
| 3625 | { |
| 3626 | if (*exceptionObject == NULL) { |
| 3627 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3628 | encoding, input, length, startpos, endpos, reason); |
| 3629 | } |
| 3630 | else { |
| 3631 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3632 | goto onError; |
| 3633 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3634 | goto onError; |
| 3635 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3636 | goto onError; |
| 3637 | } |
| 3638 | return; |
| 3639 | |
| 3640 | onError: |
| 3641 | Py_DECREF(*exceptionObject); |
| 3642 | *exceptionObject = NULL; |
| 3643 | } |
| 3644 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3645 | /* error handling callback helper: |
| 3646 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3647 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 | and adjust various state variables. |
| 3649 | return 0 on success, -1 on error |
| 3650 | */ |
| 3651 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3652 | static int |
| 3653 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3654 | const char *encoding, const char *reason, |
| 3655 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3656 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3657 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3658 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3659 | 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] | 3660 | |
| 3661 | PyObject *restuple = NULL; |
| 3662 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3663 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3664 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3665 | Py_ssize_t requiredsize; |
| 3666 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3667 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3668 | int res = -1; |
| 3669 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3670 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3671 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3672 | else |
| 3673 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3674 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3675 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3676 | *errorHandler = PyCodec_LookupError(errors); |
| 3677 | if (*errorHandler == NULL) |
| 3678 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3681 | make_decode_exception(exceptionObject, |
| 3682 | encoding, |
| 3683 | *input, *inend - *input, |
| 3684 | *startinpos, *endinpos, |
| 3685 | reason); |
| 3686 | if (*exceptionObject == NULL) |
| 3687 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3688 | |
| 3689 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3690 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3691 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3692 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3693 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3694 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3695 | } |
| 3696 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3697 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3698 | if (PyUnicode_READY(repunicode) < 0) |
| 3699 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3700 | |
| 3701 | /* Copy back the bytes variables, which might have been modified by the |
| 3702 | callback */ |
| 3703 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3704 | if (!inputobj) |
| 3705 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3706 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3707 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3708 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3709 | *input = PyBytes_AS_STRING(inputobj); |
| 3710 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3711 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3712 | /* we can DECREF safely, as the exception has another reference, |
| 3713 | so the object won't go away. */ |
| 3714 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3715 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3717 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3718 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3719 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3720 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3721 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3722 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3723 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3724 | /* need more space? (at least enough for what we |
| 3725 | have+the replacement+the rest of the string (starting |
| 3726 | at the new input position), so we won't have to check space |
| 3727 | when there are no errors in the rest of the string) */ |
| 3728 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3729 | requiredsize = *outpos + replen + insize-newpos; |
| 3730 | if (requiredsize > outsize) { |
| 3731 | if (requiredsize<2*outsize) |
| 3732 | requiredsize = 2*outsize; |
| 3733 | if (unicode_resize(output, requiredsize) < 0) |
| 3734 | goto onError; |
| 3735 | } |
| 3736 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3737 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3738 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 3739 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3740 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3741 | else { |
| 3742 | wchar_t *repwstr; |
| 3743 | Py_ssize_t repwlen; |
| 3744 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3745 | if (repwstr == NULL) |
| 3746 | goto onError; |
| 3747 | /* need more space? (at least enough for what we |
| 3748 | have+the replacement+the rest of the string (starting |
| 3749 | at the new input position), so we won't have to check space |
| 3750 | when there are no errors in the rest of the string) */ |
| 3751 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3752 | if (requiredsize > outsize) { |
| 3753 | if (requiredsize < 2*outsize) |
| 3754 | requiredsize = 2*outsize; |
| 3755 | if (unicode_resize(output, requiredsize) < 0) |
| 3756 | goto onError; |
| 3757 | } |
| 3758 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3759 | *outpos += repwlen; |
| 3760 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3761 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3762 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3763 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3764 | /* we made it! */ |
| 3765 | res = 0; |
| 3766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3767 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3768 | Py_XDECREF(restuple); |
| 3769 | return res; |
| 3770 | } |
| 3771 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3772 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3773 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3774 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3775 | |
| 3776 | /* Three simple macros defining base-64. */ |
| 3777 | |
| 3778 | /* Is c a base-64 character? */ |
| 3779 | |
| 3780 | #define IS_BASE64(c) \ |
| 3781 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3782 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3783 | ((c) >= '0' && (c) <= '9') || \ |
| 3784 | (c) == '+' || (c) == '/') |
| 3785 | |
| 3786 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3787 | |
| 3788 | #define FROM_BASE64(c) \ |
| 3789 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3790 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3791 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3792 | (c) == '+' ? 62 : 63) |
| 3793 | |
| 3794 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3795 | |
| 3796 | #define TO_BASE64(n) \ |
| 3797 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3798 | |
| 3799 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3800 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3801 | * byte not decoding to itself is the + which begins a base64 |
| 3802 | * string. */ |
| 3803 | |
| 3804 | #define DECODE_DIRECT(c) \ |
| 3805 | ((c) <= 127 && (c) != '+') |
| 3806 | |
| 3807 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3808 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3809 | * the above). See RFC2152. This array identifies these different |
| 3810 | * sets: |
| 3811 | * 0 : "Set D" |
| 3812 | * alphanumeric and '(),-./:? |
| 3813 | * 1 : "Set O" |
| 3814 | * !"#$%&*;<=>@[]^_`{|} |
| 3815 | * 2 : "whitespace" |
| 3816 | * ht nl cr sp |
| 3817 | * 3 : special (must be base64 encoded) |
| 3818 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3819 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3820 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3821 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3822 | char utf7_category[128] = { |
| 3823 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3824 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3825 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3826 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3827 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3828 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3829 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3830 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3831 | /* @ A B C D E F G H I J K L M N O */ |
| 3832 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3833 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3834 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3835 | /* ` a b c d e f g h i j k l m n o */ |
| 3836 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3837 | /* p q r s t u v w x y z { | } ~ del */ |
| 3838 | 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] | 3839 | }; |
| 3840 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3841 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3842 | * answer depends on whether we are encoding set O as itself, and also |
| 3843 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3844 | * clear that the answers to these questions vary between |
| 3845 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3846 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3847 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3848 | ((c) < 128 && (c) > 0 && \ |
| 3849 | ((utf7_category[(c)] == 0) || \ |
| 3850 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3851 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3853 | PyObject * |
| 3854 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3855 | Py_ssize_t size, |
| 3856 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3857 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3858 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3859 | } |
| 3860 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3861 | /* The decoder. The only state we preserve is our read position, |
| 3862 | * i.e. how many characters we have consumed. So if we end in the |
| 3863 | * middle of a shift sequence we have to back off the read position |
| 3864 | * and the output to the beginning of the sequence, otherwise we lose |
| 3865 | * all the shift state (seen bits, number of bits seen, high |
| 3866 | * surrogate). */ |
| 3867 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3868 | PyObject * |
| 3869 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3870 | Py_ssize_t size, |
| 3871 | const char *errors, |
| 3872 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3873 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3874 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3875 | Py_ssize_t startinpos; |
| 3876 | Py_ssize_t endinpos; |
| 3877 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3878 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3879 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3880 | const char *errmsg = ""; |
| 3881 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3882 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3883 | unsigned int base64bits = 0; |
| 3884 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3885 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3886 | PyObject *errorHandler = NULL; |
| 3887 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3888 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3889 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3890 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3891 | if (!unicode) |
| 3892 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3893 | if (size == 0) { |
| 3894 | if (consumed) |
| 3895 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3896 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3897 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3898 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3899 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3900 | e = s + size; |
| 3901 | |
| 3902 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3903 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3904 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3905 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3906 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3907 | if (inShift) { /* in a base-64 section */ |
| 3908 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3909 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3910 | base64bits += 6; |
| 3911 | s++; |
| 3912 | if (base64bits >= 16) { |
| 3913 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3914 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3915 | base64bits -= 16; |
| 3916 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3917 | if (surrogate) { |
| 3918 | /* expecting a second surrogate */ |
| 3919 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3920 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3921 | | (outCh & 0x3FF)) + 0x10000; |
| 3922 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3923 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3924 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3925 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3926 | } |
| 3927 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3928 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3929 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3930 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3931 | } |
| 3932 | } |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3933 | if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3934 | /* first surrogate */ |
| 3935 | surrogate = outCh; |
| 3936 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3937 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3938 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3939 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
| 3942 | } |
| 3943 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3944 | inShift = 0; |
| 3945 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3946 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3947 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3948 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3949 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3950 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3951 | if (base64bits > 0) { /* left-over bits */ |
| 3952 | if (base64bits >= 6) { |
| 3953 | /* We've seen at least one base-64 character */ |
| 3954 | errmsg = "partial character in shift sequence"; |
| 3955 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3956 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3957 | else { |
| 3958 | /* Some bits remain; they should be zero */ |
| 3959 | if (base64buffer != 0) { |
| 3960 | errmsg = "non-zero padding bits in shift sequence"; |
| 3961 | goto utf7Error; |
| 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | if (ch != '-') { |
| 3966 | /* '-' is absorbed; other terminating |
| 3967 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3968 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3969 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3970 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3971 | } |
| 3972 | } |
| 3973 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3974 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3975 | s++; /* consume '+' */ |
| 3976 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3977 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3978 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3979 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3980 | } |
| 3981 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3982 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3983 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3984 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3985 | } |
| 3986 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3987 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3988 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3989 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3990 | s++; |
| 3991 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3992 | else { |
| 3993 | startinpos = s-starts; |
| 3994 | s++; |
| 3995 | errmsg = "unexpected special character"; |
| 3996 | goto utf7Error; |
| 3997 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3998 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3999 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4000 | endinpos = s-starts; |
| 4001 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4002 | errors, &errorHandler, |
| 4003 | "utf7", errmsg, |
| 4004 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4005 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4006 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4009 | /* end of string */ |
| 4010 | |
| 4011 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4012 | /* if we're in an inconsistent state, that's an error */ |
| 4013 | if (surrogate || |
| 4014 | (base64bits >= 6) || |
| 4015 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4016 | endinpos = size; |
| 4017 | if (unicode_decode_call_errorhandler( |
| 4018 | errors, &errorHandler, |
| 4019 | "utf7", "unterminated shift sequence", |
| 4020 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4021 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4022 | goto onError; |
| 4023 | if (s < e) |
| 4024 | goto restart; |
| 4025 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4026 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4027 | |
| 4028 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4029 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4030 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4031 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4032 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4033 | } |
| 4034 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4035 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4036 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4037 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4038 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4039 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4040 | goto onError; |
| 4041 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | Py_XDECREF(errorHandler); |
| 4043 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4044 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4046 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | Py_XDECREF(errorHandler); |
| 4048 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4049 | Py_DECREF(unicode); |
| 4050 | return NULL; |
| 4051 | } |
| 4052 | |
| 4053 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4054 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4055 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4056 | int base64SetO, |
| 4057 | int base64WhiteSpace, |
| 4058 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4059 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4060 | int kind; |
| 4061 | void *data; |
| 4062 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4063 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4064 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4065 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4066 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4067 | unsigned int base64bits = 0; |
| 4068 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4069 | char * out; |
| 4070 | char * start; |
| 4071 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4072 | if (PyUnicode_READY(str) < 0) |
| 4073 | return NULL; |
| 4074 | kind = PyUnicode_KIND(str); |
| 4075 | data = PyUnicode_DATA(str); |
| 4076 | len = PyUnicode_GET_LENGTH(str); |
| 4077 | |
| 4078 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4079 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4080 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4081 | /* It might be possible to tighten this worst case */ |
| 4082 | allocated = 8 * len; |
| 4083 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4084 | return PyErr_NoMemory(); |
| 4085 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4086 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4087 | if (v == NULL) |
| 4088 | return NULL; |
| 4089 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4090 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4091 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4092 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4093 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4094 | if (inShift) { |
| 4095 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4096 | /* shifting out */ |
| 4097 | if (base64bits) { /* output remaining bits */ |
| 4098 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4099 | base64buffer = 0; |
| 4100 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4101 | } |
| 4102 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4103 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4104 | so no '-' is required, except if the character is itself a '-' */ |
| 4105 | if (IS_BASE64(ch) || ch == '-') { |
| 4106 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4107 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4108 | *out++ = (char) ch; |
| 4109 | } |
| 4110 | else { |
| 4111 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4112 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4113 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4114 | else { /* not in a shift sequence */ |
| 4115 | if (ch == '+') { |
| 4116 | *out++ = '+'; |
| 4117 | *out++ = '-'; |
| 4118 | } |
| 4119 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4120 | *out++ = (char) ch; |
| 4121 | } |
| 4122 | else { |
| 4123 | *out++ = '+'; |
| 4124 | inShift = 1; |
| 4125 | goto encode_char; |
| 4126 | } |
| 4127 | } |
| 4128 | continue; |
| 4129 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4130 | if (ch >= 0x10000) { |
| 4131 | /* code first surrogate */ |
| 4132 | base64bits += 16; |
| 4133 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4134 | while (base64bits >= 6) { |
| 4135 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4136 | base64bits -= 6; |
| 4137 | } |
| 4138 | /* prepare second surrogate */ |
| 4139 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4140 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4141 | base64bits += 16; |
| 4142 | base64buffer = (base64buffer << 16) | ch; |
| 4143 | while (base64bits >= 6) { |
| 4144 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4145 | base64bits -= 6; |
| 4146 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4147 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4148 | if (base64bits) |
| 4149 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4150 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4151 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4152 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4153 | return NULL; |
| 4154 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4155 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4156 | PyObject * |
| 4157 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4158 | Py_ssize_t size, |
| 4159 | int base64SetO, |
| 4160 | int base64WhiteSpace, |
| 4161 | const char *errors) |
| 4162 | { |
| 4163 | PyObject *result; |
| 4164 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4165 | if (tmp == NULL) |
| 4166 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4167 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4168 | base64WhiteSpace, errors); |
| 4169 | Py_DECREF(tmp); |
| 4170 | return result; |
| 4171 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4172 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4173 | #undef IS_BASE64 |
| 4174 | #undef FROM_BASE64 |
| 4175 | #undef TO_BASE64 |
| 4176 | #undef DECODE_DIRECT |
| 4177 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4178 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4179 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4180 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4181 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4182 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4183 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4184 | illegal prefix. See RFC 3629 for details */ |
| 4185 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4186 | 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] | 4187 | 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] | 4188 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4190 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4191 | 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] | 4192 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4193 | 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] | 4194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4195 | 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] | 4196 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4197 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4198 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4199 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4200 | 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] | 4201 | }; |
| 4202 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4203 | PyObject * |
| 4204 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4205 | Py_ssize_t size, |
| 4206 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4207 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4208 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4209 | } |
| 4210 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4211 | #include "stringlib/ucs1lib.h" |
| 4212 | #include "stringlib/codecs.h" |
| 4213 | #include "stringlib/undef.h" |
| 4214 | |
| 4215 | #include "stringlib/ucs2lib.h" |
| 4216 | #include "stringlib/codecs.h" |
| 4217 | #include "stringlib/undef.h" |
| 4218 | |
| 4219 | #include "stringlib/ucs4lib.h" |
| 4220 | #include "stringlib/codecs.h" |
| 4221 | #include "stringlib/undef.h" |
| 4222 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4223 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4224 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4225 | |
| 4226 | /* Mask to quickly check whether a C 'long' contains a |
| 4227 | non-ASCII, UTF8-encoded char. */ |
| 4228 | #if (SIZEOF_LONG == 8) |
| 4229 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4230 | #elif (SIZEOF_LONG == 4) |
| 4231 | # define ASCII_CHAR_MASK 0x80808080L |
| 4232 | #else |
| 4233 | # error C 'long' size should be either 4 or 8! |
| 4234 | #endif |
| 4235 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4236 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4237 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4238 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4239 | 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] | 4240 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4241 | */ |
| 4242 | static Py_UCS4 |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4243 | utf8_max_char_size_and_char_count(const char *s, Py_ssize_t string_size, |
| 4244 | Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4246 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4247 | const unsigned char *p = (const unsigned char *)s; |
| 4248 | const unsigned char *end = p + string_size; |
| 4249 | 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] | 4250 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4251 | assert(unicode_size != NULL); |
| 4252 | |
| 4253 | /* By having a cascade of independent loops which fallback onto each |
| 4254 | other, we minimize the amount of work done in the average loop |
| 4255 | iteration, and we also maximize the CPU's ability to predict |
| 4256 | branches correctly (because a given condition will have always the |
| 4257 | same boolean outcome except perhaps in the last iteration of the |
| 4258 | corresponding loop). |
| 4259 | In the general case this brings us rather close to decoding |
| 4260 | performance pre-PEP 393, despite the two-pass decoding. |
| 4261 | |
| 4262 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4263 | character has been encountered. It is actually a pessimization (by |
| 4264 | a significant factor) to use this loop on text with many non-ASCII |
| 4265 | characters, and it is important to avoid bad performance on valid |
| 4266 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4267 | */ |
| 4268 | |
| 4269 | /* ASCII */ |
| 4270 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4271 | /* Only check value if it's not a ASCII char... */ |
| 4272 | if (*p < 0x80) { |
| 4273 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4274 | an explanation. */ |
| 4275 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4276 | /* Help register allocation */ |
| 4277 | register const unsigned char *_p = p; |
| 4278 | while (_p < aligned_end) { |
| 4279 | unsigned long value = *(unsigned long *) _p; |
| 4280 | if (value & ASCII_CHAR_MASK) |
| 4281 | break; |
| 4282 | _p += SIZEOF_LONG; |
| 4283 | char_count += SIZEOF_LONG; |
| 4284 | } |
| 4285 | p = _p; |
| 4286 | if (p == end) |
| 4287 | break; |
| 4288 | } |
| 4289 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4290 | if (*p < 0x80) |
| 4291 | ++char_count; |
| 4292 | else |
| 4293 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4294 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4295 | *unicode_size = char_count; |
| 4296 | return 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4297 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4298 | _ucs1loop: |
| 4299 | for (; p < end; ++p) { |
| 4300 | if (*p < 0xc4) |
| 4301 | char_count += ((*p & 0xc0) != 0x80); |
| 4302 | else |
| 4303 | goto _ucs2loop; |
| 4304 | } |
| 4305 | *unicode_size = char_count; |
| 4306 | return 255; |
| 4307 | |
| 4308 | _ucs2loop: |
| 4309 | for (; p < end; ++p) { |
| 4310 | if (*p < 0xf0) |
| 4311 | char_count += ((*p & 0xc0) != 0x80); |
| 4312 | else |
| 4313 | goto _ucs4loop; |
| 4314 | } |
| 4315 | *unicode_size = char_count; |
| 4316 | return 65535; |
| 4317 | |
| 4318 | _ucs4loop: |
| 4319 | for (; p < end; ++p) { |
| 4320 | char_count += ((*p & 0xc0) != 0x80); |
| 4321 | } |
| 4322 | *unicode_size = char_count; |
| 4323 | return 65537; |
| 4324 | } |
| 4325 | |
| 4326 | /* Called when we encountered some error that wasn't detected in the original |
| 4327 | scan, e.g. an encoded surrogate character. The original maxchar computation |
| 4328 | may have been incorrect, so redo it. */ |
| 4329 | static int |
| 4330 | refit_partial_string(PyObject **unicode, int kind, void *data, Py_ssize_t n) |
| 4331 | { |
| 4332 | PyObject *tmp; |
Victor Stinner | f8facac | 2011-11-22 02:30:47 +0100 | [diff] [blame^] | 4333 | Py_ssize_t k; |
| 4334 | Py_UCS4 maxchar; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4335 | for (k = 0, maxchar = 0; k < n; k++) |
| 4336 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4337 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(*unicode), maxchar); |
| 4338 | if (tmp == NULL) |
| 4339 | return -1; |
| 4340 | PyUnicode_CopyCharacters(tmp, 0, *unicode, 0, n); |
| 4341 | Py_DECREF(*unicode); |
| 4342 | *unicode = tmp; |
| 4343 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4344 | } |
| 4345 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4346 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4347 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4348 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4349 | at the end. |
| 4350 | */ |
| 4351 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4352 | do { \ |
| 4353 | if (has_errors) { \ |
| 4354 | Py_ssize_t pos = index; \ |
| 4355 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4356 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4357 | goto onError; \ |
| 4358 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4359 | goto onError; \ |
| 4360 | } \ |
| 4361 | else \ |
| 4362 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4363 | } while (0) |
| 4364 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4365 | PyObject * |
| 4366 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4367 | Py_ssize_t size, |
| 4368 | const char *errors, |
| 4369 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4370 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4371 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4372 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4373 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4374 | Py_ssize_t startinpos; |
| 4375 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4376 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4377 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4378 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4379 | PyObject *errorHandler = NULL; |
| 4380 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4381 | Py_UCS4 maxchar = 0; |
| 4382 | Py_ssize_t unicode_size; |
| 4383 | Py_ssize_t i; |
| 4384 | int kind; |
| 4385 | void *data; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4386 | int has_errors = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4387 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4388 | if (size == 0) { |
| 4389 | if (consumed) |
| 4390 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4391 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4392 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4393 | maxchar = utf8_max_char_size_and_char_count(s, size, &unicode_size); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4394 | /* When the string is ASCII only, just use memcpy and return. |
| 4395 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4396 | sequence at the end of the ASCII block. */ |
| 4397 | if (maxchar < 128 && size == unicode_size) { |
Victor Stinner | 4288520 | 2011-11-22 01:23:02 +0100 | [diff] [blame] | 4398 | if (consumed) |
| 4399 | *consumed = size; |
| 4400 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4401 | if (size == 1) |
| 4402 | return get_latin1_char((unsigned char)s[0]); |
| 4403 | |
| 4404 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4405 | if (!unicode) |
| 4406 | return NULL; |
| 4407 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4408 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4409 | return unicode; |
| 4410 | } |
| 4411 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4412 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4413 | code below refits and resizes as necessary. */ |
| 4414 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4415 | if (!unicode) |
| 4416 | return NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4417 | kind = PyUnicode_KIND(unicode); |
| 4418 | data = PyUnicode_DATA(unicode); |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4420 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4421 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4422 | e = s + size; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4423 | switch (kind) { |
| 4424 | case PyUnicode_1BYTE_KIND: |
| 4425 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4426 | break; |
| 4427 | case PyUnicode_2BYTE_KIND: |
| 4428 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4429 | break; |
| 4430 | case PyUnicode_4BYTE_KIND: |
| 4431 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4432 | break; |
| 4433 | } |
| 4434 | if (!has_errors) { |
| 4435 | /* Ensure the unicode size calculation was correct */ |
| 4436 | assert(i == unicode_size); |
| 4437 | assert(s == e); |
| 4438 | if (consumed) |
| 4439 | *consumed = s-starts; |
| 4440 | return unicode; |
| 4441 | } |
| 4442 | /* Fall through to the generic decoding loop for the rest of |
| 4443 | the string */ |
| 4444 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
| 4445 | goto onError; |
| 4446 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4447 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4448 | |
| 4449 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4450 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4451 | |
| 4452 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4453 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4454 | input will consist of an overwhelming majority of ASCII |
| 4455 | characters, we try to optimize for this case by checking |
| 4456 | as many characters as a C 'long' can contain. |
| 4457 | First, check if we can do an aligned read, as most CPUs have |
| 4458 | a penalty for unaligned reads. |
| 4459 | */ |
| 4460 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4461 | /* Help register allocation */ |
| 4462 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4463 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4464 | while (_s < aligned_end) { |
| 4465 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4466 | and do a fast unrolled copy if it only contains ASCII |
| 4467 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4468 | unsigned long value = *(unsigned long *) _s; |
| 4469 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4470 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4471 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4472 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4473 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4474 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4475 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4476 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4477 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4478 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4479 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4480 | #endif |
| 4481 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4482 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4483 | } |
| 4484 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4485 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4486 | if (s == e) |
| 4487 | break; |
| 4488 | ch = (unsigned char)*s; |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4493 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | s++; |
| 4495 | continue; |
| 4496 | } |
| 4497 | |
| 4498 | n = utf8_code_length[ch]; |
| 4499 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4500 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | if (consumed) |
| 4502 | break; |
| 4503 | else { |
| 4504 | errmsg = "unexpected end of data"; |
| 4505 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4506 | endinpos = startinpos+1; |
| 4507 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4508 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4509 | goto utf8Error; |
| 4510 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4511 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4512 | |
| 4513 | switch (n) { |
| 4514 | |
| 4515 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4516 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4517 | startinpos = s-starts; |
| 4518 | endinpos = startinpos+1; |
| 4519 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | |
| 4521 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4522 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4523 | startinpos = s-starts; |
| 4524 | endinpos = startinpos+1; |
| 4525 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4526 | |
| 4527 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4528 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4529 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4530 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4531 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4532 | goto utf8Error; |
| 4533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4534 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4535 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4536 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | break; |
| 4538 | |
| 4539 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4540 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4541 | will result in surrogates in range d800-dfff. Surrogates are |
| 4542 | not valid UTF-8 so they are rejected. |
| 4543 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4544 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4545 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4546 | (s[2] & 0xc0) != 0x80 || |
| 4547 | ((unsigned char)s[0] == 0xE0 && |
| 4548 | (unsigned char)s[1] < 0xA0) || |
| 4549 | ((unsigned char)s[0] == 0xED && |
| 4550 | (unsigned char)s[1] > 0x9F)) { |
| 4551 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4553 | endinpos = startinpos + 1; |
| 4554 | |
| 4555 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4556 | continuation byte is s[2], so increment endinpos by 1, |
| 4557 | if not, s[1] is invalid and endinpos doesn't need to |
| 4558 | be incremented. */ |
| 4559 | if ((s[1] & 0xC0) == 0x80) |
| 4560 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4561 | goto utf8Error; |
| 4562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4564 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4565 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4566 | break; |
| 4567 | |
| 4568 | case 4: |
| 4569 | if ((s[1] & 0xc0) != 0x80 || |
| 4570 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4571 | (s[3] & 0xc0) != 0x80 || |
| 4572 | ((unsigned char)s[0] == 0xF0 && |
| 4573 | (unsigned char)s[1] < 0x90) || |
| 4574 | ((unsigned char)s[0] == 0xF4 && |
| 4575 | (unsigned char)s[1] > 0x8F)) { |
| 4576 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4577 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4578 | endinpos = startinpos + 1; |
| 4579 | if ((s[1] & 0xC0) == 0x80) { |
| 4580 | endinpos++; |
| 4581 | if ((s[2] & 0xC0) == 0x80) |
| 4582 | endinpos++; |
| 4583 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4584 | goto utf8Error; |
| 4585 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4586 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4587 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4588 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4589 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4590 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | } |
| 4593 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4594 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4596 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4597 | if (!has_errors) { |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4598 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4599 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4600 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4602 | if (unicode_decode_call_errorhandler( |
| 4603 | errors, &errorHandler, |
| 4604 | "utf8", errmsg, |
| 4605 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4606 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4607 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4608 | /* Update data because unicode_decode_call_errorhandler might have |
| 4609 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4610 | data = PyUnicode_DATA(unicode); |
| 4611 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4612 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4613 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4614 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4615 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4616 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4617 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4618 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4620 | /* Adjust length and ready string when it contained errors and |
| 4621 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4622 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4623 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4624 | goto onError; |
| 4625 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4626 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4627 | Py_XDECREF(errorHandler); |
| 4628 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4629 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4630 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4632 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4633 | Py_XDECREF(errorHandler); |
| 4634 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4635 | Py_DECREF(unicode); |
| 4636 | return NULL; |
| 4637 | } |
| 4638 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4639 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4640 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4641 | #ifdef __APPLE__ |
| 4642 | |
| 4643 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4644 | used to decode the command line arguments on Mac OS X. */ |
| 4645 | |
| 4646 | wchar_t* |
| 4647 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4648 | { |
| 4649 | int n; |
| 4650 | const char *e; |
| 4651 | wchar_t *unicode, *p; |
| 4652 | |
| 4653 | /* Note: size will always be longer than the resulting Unicode |
| 4654 | character count */ |
| 4655 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4656 | PyErr_NoMemory(); |
| 4657 | return NULL; |
| 4658 | } |
| 4659 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4660 | if (!unicode) |
| 4661 | return NULL; |
| 4662 | |
| 4663 | /* Unpack UTF-8 encoded data */ |
| 4664 | p = unicode; |
| 4665 | e = s + size; |
| 4666 | while (s < e) { |
| 4667 | Py_UCS4 ch = (unsigned char)*s; |
| 4668 | |
| 4669 | if (ch < 0x80) { |
| 4670 | *p++ = (wchar_t)ch; |
| 4671 | s++; |
| 4672 | continue; |
| 4673 | } |
| 4674 | |
| 4675 | n = utf8_code_length[ch]; |
| 4676 | if (s + n > e) { |
| 4677 | goto surrogateescape; |
| 4678 | } |
| 4679 | |
| 4680 | switch (n) { |
| 4681 | case 0: |
| 4682 | case 1: |
| 4683 | goto surrogateescape; |
| 4684 | |
| 4685 | case 2: |
| 4686 | if ((s[1] & 0xc0) != 0x80) |
| 4687 | goto surrogateescape; |
| 4688 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4689 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4690 | *p++ = (wchar_t)ch; |
| 4691 | break; |
| 4692 | |
| 4693 | case 3: |
| 4694 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4695 | will result in surrogates in range d800-dfff. Surrogates are |
| 4696 | not valid UTF-8 so they are rejected. |
| 4697 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4698 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4699 | if ((s[1] & 0xc0) != 0x80 || |
| 4700 | (s[2] & 0xc0) != 0x80 || |
| 4701 | ((unsigned char)s[0] == 0xE0 && |
| 4702 | (unsigned char)s[1] < 0xA0) || |
| 4703 | ((unsigned char)s[0] == 0xED && |
| 4704 | (unsigned char)s[1] > 0x9F)) { |
| 4705 | |
| 4706 | goto surrogateescape; |
| 4707 | } |
| 4708 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4709 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4710 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4711 | break; |
| 4712 | |
| 4713 | case 4: |
| 4714 | if ((s[1] & 0xc0) != 0x80 || |
| 4715 | (s[2] & 0xc0) != 0x80 || |
| 4716 | (s[3] & 0xc0) != 0x80 || |
| 4717 | ((unsigned char)s[0] == 0xF0 && |
| 4718 | (unsigned char)s[1] < 0x90) || |
| 4719 | ((unsigned char)s[0] == 0xF4 && |
| 4720 | (unsigned char)s[1] > 0x8F)) { |
| 4721 | goto surrogateescape; |
| 4722 | } |
| 4723 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4724 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4725 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4726 | |
| 4727 | #if SIZEOF_WCHAR_T == 4 |
| 4728 | *p++ = (wchar_t)ch; |
| 4729 | #else |
| 4730 | /* compute and append the two surrogates: */ |
| 4731 | |
| 4732 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4733 | ch -= 0x10000; |
| 4734 | |
| 4735 | /* high surrogate = top 10 bits added to D800 */ |
| 4736 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4737 | |
| 4738 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4739 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4740 | #endif |
| 4741 | break; |
| 4742 | } |
| 4743 | s += n; |
| 4744 | continue; |
| 4745 | |
| 4746 | surrogateescape: |
| 4747 | *p++ = 0xDC00 + ch; |
| 4748 | s++; |
| 4749 | } |
| 4750 | *p = L'\0'; |
| 4751 | return unicode; |
| 4752 | } |
| 4753 | |
| 4754 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4756 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4757 | |
| 4758 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4759 | and allocate exactly as much space needed at the end. Else allocate the |
| 4760 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4761 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4762 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4763 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4764 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4766 | #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4767 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4768 | Py_ssize_t i; /* index into s of next input byte */ |
| 4769 | PyObject *result; /* result string object */ |
| 4770 | char *p; /* next free byte in output buffer */ |
| 4771 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4772 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4773 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4774 | PyObject *errorHandler = NULL; |
| 4775 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4776 | int kind; |
| 4777 | void *data; |
| 4778 | Py_ssize_t size; |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4779 | PyObject *rep = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4781 | if (!PyUnicode_Check(unicode)) { |
| 4782 | PyErr_BadArgument(); |
| 4783 | return NULL; |
| 4784 | } |
| 4785 | |
| 4786 | if (PyUnicode_READY(unicode) == -1) |
| 4787 | return NULL; |
| 4788 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4789 | if (PyUnicode_UTF8(unicode)) |
| 4790 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4791 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4792 | |
| 4793 | kind = PyUnicode_KIND(unicode); |
| 4794 | data = PyUnicode_DATA(unicode); |
| 4795 | size = PyUnicode_GET_LENGTH(unicode); |
| 4796 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4797 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4798 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4799 | if (size <= MAX_SHORT_UNICHARS) { |
| 4800 | /* Write into the stack buffer; nallocated can't overflow. |
| 4801 | * At the end, we'll allocate exactly as much heap space as it |
| 4802 | * turns out we need. |
| 4803 | */ |
| 4804 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4805 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4806 | p = stackbuf; |
| 4807 | } |
| 4808 | else { |
| 4809 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4810 | nallocated = size * 4; |
| 4811 | if (nallocated / 4 != size) /* overflow! */ |
| 4812 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4813 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4814 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4815 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4816 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4817 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4818 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4819 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4820 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4821 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4822 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4823 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4824 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4826 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4827 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4828 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4829 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4830 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4831 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4832 | Py_ssize_t repsize, k, startpos; |
| 4833 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4834 | rep = unicode_encode_call_errorhandler( |
| 4835 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4836 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4837 | if (!rep) |
| 4838 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4840 | if (PyBytes_Check(rep)) |
| 4841 | repsize = PyBytes_GET_SIZE(rep); |
| 4842 | else |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 4843 | repsize = PyUnicode_GET_LENGTH(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4844 | |
| 4845 | if (repsize > 4) { |
| 4846 | Py_ssize_t offset; |
| 4847 | |
| 4848 | if (result == NULL) |
| 4849 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4850 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4851 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4852 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4853 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4854 | /* integer overflow */ |
| 4855 | PyErr_NoMemory(); |
| 4856 | goto error; |
| 4857 | } |
| 4858 | nallocated += repsize - 4; |
| 4859 | if (result != NULL) { |
| 4860 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4861 | goto error; |
| 4862 | } else { |
| 4863 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4864 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4865 | goto error; |
| 4866 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4867 | } |
| 4868 | p = PyBytes_AS_STRING(result) + offset; |
| 4869 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4870 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4871 | if (PyBytes_Check(rep)) { |
| 4872 | char *prep = PyBytes_AS_STRING(rep); |
| 4873 | for(k = repsize; k > 0; k--) |
| 4874 | *p++ = *prep++; |
| 4875 | } else /* rep is unicode */ { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4876 | enum PyUnicode_Kind repkind; |
| 4877 | void *repdata; |
| 4878 | |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4879 | if (PyUnicode_READY(rep) < 0) |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4880 | goto error; |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4881 | repkind = PyUnicode_KIND(rep); |
| 4882 | repdata = PyUnicode_DATA(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4883 | |
| 4884 | for(k=0; k<repsize; k++) { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4885 | Py_UCS4 c = PyUnicode_READ(repkind, repdata, k); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4886 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4887 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4888 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4889 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4890 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4891 | goto error; |
| 4892 | } |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4893 | *p++ = (char)c; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4894 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4895 | } |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4896 | Py_CLEAR(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4897 | } else if (ch < 0x10000) { |
| 4898 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4899 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4900 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4901 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4902 | /* Encode UCS4 Unicode ordinals */ |
| 4903 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4904 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4905 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4906 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4907 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4909 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4910 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4911 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4912 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4913 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4914 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4915 | } |
| 4916 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4917 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4918 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4919 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4920 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4921 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4922 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4923 | Py_XDECREF(errorHandler); |
| 4924 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4925 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4926 | error: |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4927 | Py_XDECREF(rep); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4928 | Py_XDECREF(errorHandler); |
| 4929 | Py_XDECREF(exc); |
| 4930 | Py_XDECREF(result); |
| 4931 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4932 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4933 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4936 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4937 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4938 | Py_ssize_t size, |
| 4939 | const char *errors) |
| 4940 | { |
| 4941 | PyObject *v, *unicode; |
| 4942 | |
| 4943 | unicode = PyUnicode_FromUnicode(s, size); |
| 4944 | if (unicode == NULL) |
| 4945 | return NULL; |
| 4946 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4947 | Py_DECREF(unicode); |
| 4948 | return v; |
| 4949 | } |
| 4950 | |
| 4951 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4952 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4953 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4954 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4957 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4958 | |
| 4959 | PyObject * |
| 4960 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4961 | Py_ssize_t size, |
| 4962 | const char *errors, |
| 4963 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4964 | { |
| 4965 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4966 | } |
| 4967 | |
| 4968 | PyObject * |
| 4969 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | Py_ssize_t size, |
| 4971 | const char *errors, |
| 4972 | int *byteorder, |
| 4973 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4974 | { |
| 4975 | const char *starts = s; |
| 4976 | Py_ssize_t startinpos; |
| 4977 | Py_ssize_t endinpos; |
| 4978 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4979 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4980 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4981 | int bo = 0; /* assume native ordering by default */ |
| 4982 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4984 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4985 | int iorder[] = {0, 1, 2, 3}; |
| 4986 | #else |
| 4987 | int iorder[] = {3, 2, 1, 0}; |
| 4988 | #endif |
| 4989 | PyObject *errorHandler = NULL; |
| 4990 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4991 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4992 | q = (unsigned char *)s; |
| 4993 | e = q + size; |
| 4994 | |
| 4995 | if (byteorder) |
| 4996 | bo = *byteorder; |
| 4997 | |
| 4998 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4999 | byte order setting accordingly. In native mode, the leading BOM |
| 5000 | mark is skipped, in all other modes, it is copied to the output |
| 5001 | stream as-is (giving a ZWNBSP character). */ |
| 5002 | if (bo == 0) { |
| 5003 | if (size >= 4) { |
| 5004 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5006 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | if (bom == 0x0000FEFF) { |
| 5008 | q += 4; |
| 5009 | bo = -1; |
| 5010 | } |
| 5011 | else if (bom == 0xFFFE0000) { |
| 5012 | q += 4; |
| 5013 | bo = 1; |
| 5014 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5015 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5016 | if (bom == 0x0000FEFF) { |
| 5017 | q += 4; |
| 5018 | bo = 1; |
| 5019 | } |
| 5020 | else if (bom == 0xFFFE0000) { |
| 5021 | q += 4; |
| 5022 | bo = -1; |
| 5023 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5024 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | if (bo == -1) { |
| 5029 | /* force LE */ |
| 5030 | iorder[0] = 0; |
| 5031 | iorder[1] = 1; |
| 5032 | iorder[2] = 2; |
| 5033 | iorder[3] = 3; |
| 5034 | } |
| 5035 | else if (bo == 1) { |
| 5036 | /* force BE */ |
| 5037 | iorder[0] = 3; |
| 5038 | iorder[1] = 2; |
| 5039 | iorder[2] = 1; |
| 5040 | iorder[3] = 0; |
| 5041 | } |
| 5042 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5043 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5044 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5045 | if (!unicode) |
| 5046 | return NULL; |
| 5047 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5048 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5049 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5050 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5052 | Py_UCS4 ch; |
| 5053 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5054 | if (e-q<4) { |
| 5055 | if (consumed) |
| 5056 | break; |
| 5057 | errmsg = "truncated data"; |
| 5058 | startinpos = ((const char *)q)-starts; |
| 5059 | endinpos = ((const char *)e)-starts; |
| 5060 | goto utf32Error; |
| 5061 | /* The remaining input chars are ignored if the callback |
| 5062 | chooses to skip the input */ |
| 5063 | } |
| 5064 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5065 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5067 | if (ch >= 0x110000) |
| 5068 | { |
| 5069 | errmsg = "codepoint not in range(0x110000)"; |
| 5070 | startinpos = ((const char *)q)-starts; |
| 5071 | endinpos = startinpos+4; |
| 5072 | goto utf32Error; |
| 5073 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5074 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5075 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | q += 4; |
| 5077 | continue; |
| 5078 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5079 | if (unicode_decode_call_errorhandler( |
| 5080 | errors, &errorHandler, |
| 5081 | "utf32", errmsg, |
| 5082 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5083 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5084 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | if (byteorder) |
| 5088 | *byteorder = bo; |
| 5089 | |
| 5090 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5091 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5092 | |
| 5093 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5094 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | goto onError; |
| 5096 | |
| 5097 | Py_XDECREF(errorHandler); |
| 5098 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5099 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5100 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5102 | Py_DECREF(unicode); |
| 5103 | Py_XDECREF(errorHandler); |
| 5104 | Py_XDECREF(exc); |
| 5105 | return NULL; |
| 5106 | } |
| 5107 | |
| 5108 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5109 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5110 | const char *errors, |
| 5111 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5112 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5113 | int kind; |
| 5114 | void *data; |
| 5115 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5116 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5117 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5118 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5119 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5120 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5121 | int iorder[] = {0, 1, 2, 3}; |
| 5122 | #else |
| 5123 | int iorder[] = {3, 2, 1, 0}; |
| 5124 | #endif |
| 5125 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | #define STORECHAR(CH) \ |
| 5127 | do { \ |
| 5128 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5129 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5130 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5131 | p[iorder[0]] = (CH) & 0xff; \ |
| 5132 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5133 | } while(0) |
| 5134 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5135 | if (!PyUnicode_Check(str)) { |
| 5136 | PyErr_BadArgument(); |
| 5137 | return NULL; |
| 5138 | } |
| 5139 | if (PyUnicode_READY(str) < 0) |
| 5140 | return NULL; |
| 5141 | kind = PyUnicode_KIND(str); |
| 5142 | data = PyUnicode_DATA(str); |
| 5143 | len = PyUnicode_GET_LENGTH(str); |
| 5144 | |
| 5145 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5146 | bytesize = nsize * 4; |
| 5147 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5148 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5149 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5150 | if (v == NULL) |
| 5151 | return NULL; |
| 5152 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5153 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5154 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5155 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5156 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5157 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5158 | |
| 5159 | if (byteorder == -1) { |
| 5160 | /* force LE */ |
| 5161 | iorder[0] = 0; |
| 5162 | iorder[1] = 1; |
| 5163 | iorder[2] = 2; |
| 5164 | iorder[3] = 3; |
| 5165 | } |
| 5166 | else if (byteorder == 1) { |
| 5167 | /* force BE */ |
| 5168 | iorder[0] = 3; |
| 5169 | iorder[1] = 2; |
| 5170 | iorder[2] = 1; |
| 5171 | iorder[3] = 0; |
| 5172 | } |
| 5173 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5174 | for (i = 0; i < len; i++) |
| 5175 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5176 | |
| 5177 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5178 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | #undef STORECHAR |
| 5180 | } |
| 5181 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5182 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5183 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5184 | Py_ssize_t size, |
| 5185 | const char *errors, |
| 5186 | int byteorder) |
| 5187 | { |
| 5188 | PyObject *result; |
| 5189 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5190 | if (tmp == NULL) |
| 5191 | return NULL; |
| 5192 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5193 | Py_DECREF(tmp); |
| 5194 | return result; |
| 5195 | } |
| 5196 | |
| 5197 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5198 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5199 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5200 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5204 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5205 | PyObject * |
| 5206 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5207 | Py_ssize_t size, |
| 5208 | const char *errors, |
| 5209 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5211 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5212 | } |
| 5213 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5214 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5215 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5216 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5217 | rare in most input. |
| 5218 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5219 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5220 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5221 | #if (SIZEOF_LONG == 8) |
| 5222 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5223 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5224 | #elif (SIZEOF_LONG == 4) |
| 5225 | # define FAST_CHAR_MASK 0x80008000L |
| 5226 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5227 | #else |
| 5228 | # error C 'long' size should be either 4 or 8! |
| 5229 | #endif |
| 5230 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5231 | PyObject * |
| 5232 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5233 | Py_ssize_t size, |
| 5234 | const char *errors, |
| 5235 | int *byteorder, |
| 5236 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5237 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5238 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5239 | Py_ssize_t startinpos; |
| 5240 | Py_ssize_t endinpos; |
| 5241 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5242 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5243 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5244 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5245 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5246 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5247 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5248 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5249 | int ihi = 1, ilo = 0; |
| 5250 | #else |
| 5251 | int ihi = 0, ilo = 1; |
| 5252 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5253 | PyObject *errorHandler = NULL; |
| 5254 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | |
| 5256 | /* Note: size will always be longer than the resulting Unicode |
| 5257 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5258 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | if (!unicode) |
| 5260 | return NULL; |
| 5261 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5262 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5263 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5265 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5266 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | |
| 5268 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5269 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5270 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5271 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5272 | byte order setting accordingly. In native mode, the leading BOM |
| 5273 | mark is skipped, in all other modes, it is copied to the output |
| 5274 | stream as-is (giving a ZWNBSP character). */ |
| 5275 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5276 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5277 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5278 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5279 | if (bom == 0xFEFF) { |
| 5280 | q += 2; |
| 5281 | bo = -1; |
| 5282 | } |
| 5283 | else if (bom == 0xFFFE) { |
| 5284 | q += 2; |
| 5285 | bo = 1; |
| 5286 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5287 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5288 | if (bom == 0xFEFF) { |
| 5289 | q += 2; |
| 5290 | bo = 1; |
| 5291 | } |
| 5292 | else if (bom == 0xFFFE) { |
| 5293 | q += 2; |
| 5294 | bo = -1; |
| 5295 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5296 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5298 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5300 | if (bo == -1) { |
| 5301 | /* force LE */ |
| 5302 | ihi = 1; |
| 5303 | ilo = 0; |
| 5304 | } |
| 5305 | else if (bo == 1) { |
| 5306 | /* force BE */ |
| 5307 | ihi = 0; |
| 5308 | ilo = 1; |
| 5309 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5310 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5311 | native_ordering = ilo < ihi; |
| 5312 | #else |
| 5313 | native_ordering = ilo > ihi; |
| 5314 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5315 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5316 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5317 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5318 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5319 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5320 | reads are more expensive, better to defer to another iteration. */ |
| 5321 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5322 | /* Fast path for runs of non-surrogate chars. */ |
| 5323 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5324 | int kind = PyUnicode_KIND(unicode); |
| 5325 | void *data = PyUnicode_DATA(unicode); |
| 5326 | while (_q < aligned_end) { |
| 5327 | unsigned long block = * (unsigned long *) _q; |
| 5328 | unsigned short *pblock = (unsigned short*)█ |
| 5329 | Py_UCS4 maxch; |
| 5330 | if (native_ordering) { |
| 5331 | /* Can use buffer directly */ |
| 5332 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5333 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5334 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5335 | else { |
| 5336 | /* Need to byte-swap */ |
| 5337 | unsigned char *_p = (unsigned char*)pblock; |
| 5338 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5339 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5340 | _p[0] = _q[1]; |
| 5341 | _p[1] = _q[0]; |
| 5342 | _p[2] = _q[3]; |
| 5343 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5344 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5345 | _p[4] = _q[5]; |
| 5346 | _p[5] = _q[4]; |
| 5347 | _p[6] = _q[7]; |
| 5348 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5349 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5350 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5351 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5352 | #if SIZEOF_LONG == 8 |
| 5353 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5354 | #endif |
| 5355 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5356 | if (unicode_widen(&unicode, maxch) < 0) |
| 5357 | goto onError; |
| 5358 | kind = PyUnicode_KIND(unicode); |
| 5359 | data = PyUnicode_DATA(unicode); |
| 5360 | } |
| 5361 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5362 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5363 | #if SIZEOF_LONG == 8 |
| 5364 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5365 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5366 | #endif |
| 5367 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5368 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5369 | q = _q; |
| 5370 | if (q >= e) |
| 5371 | break; |
| 5372 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5373 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5374 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5375 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | |
| 5377 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5378 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5379 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5380 | continue; |
| 5381 | } |
| 5382 | |
| 5383 | /* UTF-16 code pair: */ |
| 5384 | if (q > e) { |
| 5385 | errmsg = "unexpected end of data"; |
| 5386 | startinpos = (((const char *)q) - 2) - starts; |
| 5387 | endinpos = ((const char *)e) + 1 - starts; |
| 5388 | goto utf16Error; |
| 5389 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5390 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5391 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5393 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5394 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5395 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5396 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5397 | continue; |
| 5398 | } |
| 5399 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5400 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5401 | startinpos = (((const char *)q)-4)-starts; |
| 5402 | endinpos = startinpos+2; |
| 5403 | goto utf16Error; |
| 5404 | } |
| 5405 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5406 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5407 | errmsg = "illegal encoding"; |
| 5408 | startinpos = (((const char *)q)-2)-starts; |
| 5409 | endinpos = startinpos+2; |
| 5410 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5411 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5412 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5414 | errors, |
| 5415 | &errorHandler, |
| 5416 | "utf16", errmsg, |
| 5417 | &starts, |
| 5418 | (const char **)&e, |
| 5419 | &startinpos, |
| 5420 | &endinpos, |
| 5421 | &exc, |
| 5422 | (const char **)&q, |
| 5423 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5424 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5425 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5427 | /* remaining byte at the end? (size should be even) */ |
| 5428 | if (e == q) { |
| 5429 | if (!consumed) { |
| 5430 | errmsg = "truncated data"; |
| 5431 | startinpos = ((const char *)q) - starts; |
| 5432 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5433 | if (unicode_decode_call_errorhandler( |
| 5434 | errors, |
| 5435 | &errorHandler, |
| 5436 | "utf16", errmsg, |
| 5437 | &starts, |
| 5438 | (const char **)&e, |
| 5439 | &startinpos, |
| 5440 | &endinpos, |
| 5441 | &exc, |
| 5442 | (const char **)&q, |
| 5443 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5444 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5445 | goto onError; |
| 5446 | /* The remaining input chars are ignored if the callback |
| 5447 | chooses to skip the input */ |
| 5448 | } |
| 5449 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | |
| 5451 | if (byteorder) |
| 5452 | *byteorder = bo; |
| 5453 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5454 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5455 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5458 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | goto onError; |
| 5460 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5461 | Py_XDECREF(errorHandler); |
| 5462 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5463 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5465 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5466 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5467 | Py_XDECREF(errorHandler); |
| 5468 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | return NULL; |
| 5470 | } |
| 5471 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5472 | #undef FAST_CHAR_MASK |
| 5473 | #undef SWAPPED_FAST_CHAR_MASK |
| 5474 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5475 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5476 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5477 | const char *errors, |
| 5478 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5479 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5480 | int kind; |
| 5481 | void *data; |
| 5482 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5483 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5484 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5485 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5486 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5487 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5488 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5489 | int ihi = 1, ilo = 0; |
| 5490 | #else |
| 5491 | int ihi = 0, ilo = 1; |
| 5492 | #endif |
| 5493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | #define STORECHAR(CH) \ |
| 5495 | do { \ |
| 5496 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5497 | p[ilo] = (CH) & 0xff; \ |
| 5498 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5499 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5501 | if (!PyUnicode_Check(str)) { |
| 5502 | PyErr_BadArgument(); |
| 5503 | return NULL; |
| 5504 | } |
| 5505 | if (PyUnicode_READY(str) < 0) |
| 5506 | return NULL; |
| 5507 | kind = PyUnicode_KIND(str); |
| 5508 | data = PyUnicode_DATA(str); |
| 5509 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5510 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5511 | pairs = 0; |
| 5512 | if (kind == PyUnicode_4BYTE_KIND) |
| 5513 | for (i = 0; i < len; i++) |
| 5514 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5515 | pairs++; |
| 5516 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5517 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5518 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5519 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5520 | bytesize = nsize * 2; |
| 5521 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5522 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5523 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | if (v == NULL) |
| 5525 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5526 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5527 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5528 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5529 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5530 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5531 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5532 | |
| 5533 | if (byteorder == -1) { |
| 5534 | /* force LE */ |
| 5535 | ihi = 1; |
| 5536 | ilo = 0; |
| 5537 | } |
| 5538 | else if (byteorder == 1) { |
| 5539 | /* force BE */ |
| 5540 | ihi = 0; |
| 5541 | ilo = 1; |
| 5542 | } |
| 5543 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5544 | for (i = 0; i < len; i++) { |
| 5545 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5546 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | if (ch >= 0x10000) { |
| 5548 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5549 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5550 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5551 | STORECHAR(ch); |
| 5552 | if (ch2) |
| 5553 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5554 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5555 | |
| 5556 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5557 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5558 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5561 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5562 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5563 | Py_ssize_t size, |
| 5564 | const char *errors, |
| 5565 | int byteorder) |
| 5566 | { |
| 5567 | PyObject *result; |
| 5568 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5569 | if (tmp == NULL) |
| 5570 | return NULL; |
| 5571 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5572 | Py_DECREF(tmp); |
| 5573 | return result; |
| 5574 | } |
| 5575 | |
| 5576 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5577 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5579 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5583 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5584 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5585 | if all the escapes in the string make it still a valid ASCII string. |
| 5586 | Returns -1 if any escapes were found which cause the string to |
| 5587 | pop out of ASCII range. Otherwise returns the length of the |
| 5588 | required buffer to hold the string. |
| 5589 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5590 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5591 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5592 | { |
| 5593 | const unsigned char *p = (const unsigned char *)s; |
| 5594 | const unsigned char *end = p + size; |
| 5595 | Py_ssize_t length = 0; |
| 5596 | |
| 5597 | if (size < 0) |
| 5598 | return -1; |
| 5599 | |
| 5600 | for (; p < end; ++p) { |
| 5601 | if (*p > 127) { |
| 5602 | /* Non-ASCII */ |
| 5603 | return -1; |
| 5604 | } |
| 5605 | else if (*p != '\\') { |
| 5606 | /* Normal character */ |
| 5607 | ++length; |
| 5608 | } |
| 5609 | else { |
| 5610 | /* Backslash-escape, check next char */ |
| 5611 | ++p; |
| 5612 | /* Escape sequence reaches till end of string or |
| 5613 | non-ASCII follow-up. */ |
| 5614 | if (p >= end || *p > 127) |
| 5615 | return -1; |
| 5616 | switch (*p) { |
| 5617 | case '\n': |
| 5618 | /* backslash + \n result in zero characters */ |
| 5619 | break; |
| 5620 | case '\\': case '\'': case '\"': |
| 5621 | case 'b': case 'f': case 't': |
| 5622 | case 'n': case 'r': case 'v': case 'a': |
| 5623 | ++length; |
| 5624 | break; |
| 5625 | case '0': case '1': case '2': case '3': |
| 5626 | case '4': case '5': case '6': case '7': |
| 5627 | case 'x': case 'u': case 'U': case 'N': |
| 5628 | /* these do not guarantee ASCII characters */ |
| 5629 | return -1; |
| 5630 | default: |
| 5631 | /* count the backslash + the other character */ |
| 5632 | length += 2; |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | return length; |
| 5637 | } |
| 5638 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5639 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5640 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5641 | PyObject * |
| 5642 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5643 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5644 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5646 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5647 | Py_ssize_t startinpos; |
| 5648 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5649 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5650 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5652 | char* message; |
| 5653 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5654 | PyObject *errorHandler = NULL; |
| 5655 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5656 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5657 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5658 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5659 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5660 | |
| 5661 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5662 | either the string is pure ASCII with named escapes like \n, etc. |
| 5663 | and we determined it's exact size (common case) |
| 5664 | or it contains \x, \u, ... escape sequences. then we create a |
| 5665 | 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] | 5666 | if (len >= 0) { |
| 5667 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5668 | if (!v) |
| 5669 | goto onError; |
| 5670 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5671 | } |
| 5672 | else { |
| 5673 | /* Escaped strings will always be longer than the resulting |
| 5674 | Unicode string, so we start with size here and then reduce the |
| 5675 | length after conversion to the true value. |
| 5676 | (but if the error callback returns a long replacement string |
| 5677 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5678 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5679 | if (!v) |
| 5680 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5681 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5682 | } |
| 5683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5685 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5686 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | while (s < end) { |
| 5690 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5691 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5692 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5694 | /* The only case in which i == ascii_length is a backslash |
| 5695 | followed by a newline. */ |
| 5696 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5699 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5700 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5701 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | continue; |
| 5703 | } |
| 5704 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | /* \ - Escapes */ |
| 5707 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5708 | c = *s++; |
| 5709 | if (s > end) |
| 5710 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5711 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5712 | /* The only case in which i == ascii_length is a backslash |
| 5713 | followed by a newline. */ |
| 5714 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5715 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5716 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5718 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5719 | #define WRITECHAR(ch) \ |
| 5720 | do { \ |
| 5721 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5722 | goto onError; \ |
| 5723 | }while(0) |
| 5724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5726 | case '\\': WRITECHAR('\\'); break; |
| 5727 | case '\'': WRITECHAR('\''); break; |
| 5728 | case '\"': WRITECHAR('\"'); break; |
| 5729 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5730 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5731 | case 'f': WRITECHAR('\014'); break; |
| 5732 | case 't': WRITECHAR('\t'); break; |
| 5733 | case 'n': WRITECHAR('\n'); break; |
| 5734 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5735 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5736 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5737 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5738 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5740 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | case '0': case '1': case '2': case '3': |
| 5742 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5743 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5744 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5745 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5746 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5747 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5749 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | break; |
| 5751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | /* hex escapes */ |
| 5753 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5755 | digits = 2; |
| 5756 | message = "truncated \\xXX escape"; |
| 5757 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5761 | digits = 4; |
| 5762 | message = "truncated \\uXXXX escape"; |
| 5763 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5766 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5767 | digits = 8; |
| 5768 | message = "truncated \\UXXXXXXXX escape"; |
| 5769 | hexescape: |
| 5770 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5771 | if (s+digits>end) { |
| 5772 | endinpos = size; |
| 5773 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5774 | errors, &errorHandler, |
| 5775 | "unicodeescape", "end of string in escape sequence", |
| 5776 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5777 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5778 | goto onError; |
| 5779 | goto nextByte; |
| 5780 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5781 | for (j = 0; j < digits; ++j) { |
| 5782 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5783 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5784 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5785 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5786 | errors, &errorHandler, |
| 5787 | "unicodeescape", message, |
| 5788 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5789 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5790 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5791 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5792 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5793 | } |
| 5794 | chr = (chr<<4) & ~0xF; |
| 5795 | if (c >= '0' && c <= '9') |
| 5796 | chr += c - '0'; |
| 5797 | else if (c >= 'a' && c <= 'f') |
| 5798 | chr += 10 + c - 'a'; |
| 5799 | else |
| 5800 | chr += 10 + c - 'A'; |
| 5801 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5802 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5803 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5804 | /* _decoding_error will have already written into the |
| 5805 | target buffer. */ |
| 5806 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5807 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5808 | /* when we get here, chr is a 32-bit unicode character */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5809 | if (chr <= 0x10ffff) { |
| 5810 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5811 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5812 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5813 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5814 | errors, &errorHandler, |
| 5815 | "unicodeescape", "illegal Unicode character", |
| 5816 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5817 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5818 | goto onError; |
| 5819 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5820 | break; |
| 5821 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5822 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5823 | case 'N': |
| 5824 | message = "malformed \\N character escape"; |
| 5825 | if (ucnhash_CAPI == NULL) { |
| 5826 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5827 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5828 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5829 | if (ucnhash_CAPI == NULL) |
| 5830 | goto ucnhashError; |
| 5831 | } |
| 5832 | if (*s == '{') { |
| 5833 | const char *start = s+1; |
| 5834 | /* look for the closing brace */ |
| 5835 | while (*s != '}' && s < end) |
| 5836 | s++; |
| 5837 | if (s > start && s < end && *s == '}') { |
| 5838 | /* found a name. look it up in the unicode database */ |
| 5839 | message = "unknown Unicode character name"; |
| 5840 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5841 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5842 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5843 | goto store; |
| 5844 | } |
| 5845 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5846 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5847 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5848 | errors, &errorHandler, |
| 5849 | "unicodeescape", message, |
| 5850 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5851 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5852 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5853 | break; |
| 5854 | |
| 5855 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5856 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5857 | message = "\\ at end of string"; |
| 5858 | s--; |
| 5859 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5860 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | errors, &errorHandler, |
| 5862 | "unicodeescape", message, |
| 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 | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5865 | goto onError; |
| 5866 | } |
| 5867 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5868 | WRITECHAR('\\'); |
| 5869 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5870 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5871 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5876 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5877 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5878 | if (PyUnicode_Resize(&v, i) < 0) |
| 5879 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5880 | Py_XDECREF(errorHandler); |
| 5881 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5882 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5883 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5885 | PyErr_SetString( |
| 5886 | PyExc_UnicodeError, |
| 5887 | "\\N escapes not supported (can't load unicodedata module)" |
| 5888 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5889 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5890 | Py_XDECREF(errorHandler); |
| 5891 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5892 | return NULL; |
| 5893 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5894 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5896 | Py_XDECREF(errorHandler); |
| 5897 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5898 | return NULL; |
| 5899 | } |
| 5900 | |
| 5901 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5902 | |
| 5903 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5904 | appropriate. |
| 5905 | |
| 5906 | */ |
| 5907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5908 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5909 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5911 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5912 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5914 | int kind; |
| 5915 | void *data; |
| 5916 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5918 | /* Initial allocation is based on the longest-possible unichr |
| 5919 | escape. |
| 5920 | |
| 5921 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5922 | unichr, so in this case it's the longest unichr escape. In |
| 5923 | narrow (UTF-16) builds this is five chars per source unichr |
| 5924 | since there are two unichrs in the surrogate pair, so in narrow |
| 5925 | (UTF-16) builds it's not the longest unichr escape. |
| 5926 | |
| 5927 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5928 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5929 | escape. |
| 5930 | */ |
| 5931 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5932 | if (!PyUnicode_Check(unicode)) { |
| 5933 | PyErr_BadArgument(); |
| 5934 | return NULL; |
| 5935 | } |
| 5936 | if (PyUnicode_READY(unicode) < 0) |
| 5937 | return NULL; |
| 5938 | len = PyUnicode_GET_LENGTH(unicode); |
| 5939 | kind = PyUnicode_KIND(unicode); |
| 5940 | data = PyUnicode_DATA(unicode); |
| 5941 | switch(kind) { |
| 5942 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5943 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5944 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5945 | } |
| 5946 | |
| 5947 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5948 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5949 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5950 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5952 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5953 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5954 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5955 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5956 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | if (repr == NULL) |
| 5958 | return NULL; |
| 5959 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5960 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5962 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5963 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5964 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5965 | /* Escape backslashes */ |
| 5966 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | *p++ = '\\'; |
| 5968 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5969 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5970 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5971 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5972 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5973 | else if (ch >= 0x10000) { |
| 5974 | *p++ = '\\'; |
| 5975 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5976 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5977 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5978 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5979 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5980 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5981 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5982 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5983 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5984 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5985 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5988 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | *p++ = '\\'; |
| 5990 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5991 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5992 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5993 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5994 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5996 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5997 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5998 | else if (ch == '\t') { |
| 5999 | *p++ = '\\'; |
| 6000 | *p++ = 't'; |
| 6001 | } |
| 6002 | else if (ch == '\n') { |
| 6003 | *p++ = '\\'; |
| 6004 | *p++ = 'n'; |
| 6005 | } |
| 6006 | else if (ch == '\r') { |
| 6007 | *p++ = '\\'; |
| 6008 | *p++ = 'r'; |
| 6009 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6010 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6011 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6012 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6014 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6015 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6016 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6017 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | /* Copy everything else as-is */ |
| 6020 | else |
| 6021 | *p++ = (char) ch; |
| 6022 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6024 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6025 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6026 | return NULL; |
| 6027 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6030 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6031 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6032 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6034 | PyObject *result; |
| 6035 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6036 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6038 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6039 | Py_DECREF(tmp); |
| 6040 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6044 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6045 | PyObject * |
| 6046 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6047 | Py_ssize_t size, |
| 6048 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6050 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6051 | Py_ssize_t startinpos; |
| 6052 | Py_ssize_t endinpos; |
| 6053 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6054 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | const char *end; |
| 6056 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6057 | PyObject *errorHandler = NULL; |
| 6058 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | /* Escaped strings will always be longer than the resulting |
| 6061 | 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] | 6062 | length after conversion to the true value. (But decoding error |
| 6063 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6064 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6066 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6068 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6069 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | end = s + size; |
| 6071 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | unsigned char c; |
| 6073 | Py_UCS4 x; |
| 6074 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6075 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6078 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6079 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6080 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6081 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6082 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | startinpos = s-starts; |
| 6084 | |
| 6085 | /* \u-escapes are only interpreted iff the number of leading |
| 6086 | backslashes if odd */ |
| 6087 | bs = s; |
| 6088 | for (;s < end;) { |
| 6089 | if (*s != '\\') |
| 6090 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6091 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6092 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6093 | } |
| 6094 | if (((s - bs) & 1) == 0 || |
| 6095 | s >= end || |
| 6096 | (*s != 'u' && *s != 'U')) { |
| 6097 | continue; |
| 6098 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6099 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6100 | count = *s=='u' ? 4 : 8; |
| 6101 | s++; |
| 6102 | |
| 6103 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6104 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6105 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6106 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6107 | endinpos = s-starts; |
| 6108 | if (unicode_decode_call_errorhandler( |
| 6109 | errors, &errorHandler, |
| 6110 | "rawunicodeescape", "truncated \\uXXXX", |
| 6111 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6112 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6113 | goto onError; |
| 6114 | goto nextByte; |
| 6115 | } |
| 6116 | x = (x<<4) & ~0xF; |
| 6117 | if (c >= '0' && c <= '9') |
| 6118 | x += c - '0'; |
| 6119 | else if (c >= 'a' && c <= 'f') |
| 6120 | x += 10 + c - 'a'; |
| 6121 | else |
| 6122 | x += 10 + c - 'A'; |
| 6123 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6124 | if (x <= 0x10ffff) { |
| 6125 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6126 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6127 | } else { |
| 6128 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6129 | if (unicode_decode_call_errorhandler( |
| 6130 | errors, &errorHandler, |
| 6131 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6133 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6134 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6135 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | nextByte: |
| 6137 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6139 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6141 | Py_XDECREF(errorHandler); |
| 6142 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6143 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6144 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6147 | Py_XDECREF(errorHandler); |
| 6148 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | return NULL; |
| 6150 | } |
| 6151 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6152 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6153 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6154 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6156 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | char *p; |
| 6158 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6159 | Py_ssize_t expandsize, pos; |
| 6160 | int kind; |
| 6161 | void *data; |
| 6162 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6164 | if (!PyUnicode_Check(unicode)) { |
| 6165 | PyErr_BadArgument(); |
| 6166 | return NULL; |
| 6167 | } |
| 6168 | if (PyUnicode_READY(unicode) < 0) |
| 6169 | return NULL; |
| 6170 | kind = PyUnicode_KIND(unicode); |
| 6171 | data = PyUnicode_DATA(unicode); |
| 6172 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6173 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6174 | switch(kind) { |
| 6175 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6176 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6177 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6178 | } |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6179 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6180 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6181 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6182 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6183 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | if (repr == NULL) |
| 6185 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6186 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6187 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6189 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6190 | for (pos = 0; pos < len; pos++) { |
| 6191 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6192 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6193 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6194 | *p++ = '\\'; |
| 6195 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6196 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6197 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6198 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6199 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6200 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6201 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6202 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6203 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6205 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | *p++ = '\\'; |
| 6208 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6209 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6210 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6211 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6212 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | /* Copy everything else as-is */ |
| 6215 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | *p++ = (char) ch; |
| 6217 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6218 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6219 | assert(p > q); |
| 6220 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6221 | return NULL; |
| 6222 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6225 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6226 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6227 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6229 | PyObject *result; |
| 6230 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6231 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6232 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6233 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6234 | Py_DECREF(tmp); |
| 6235 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6238 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6240 | PyObject * |
| 6241 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6242 | Py_ssize_t size, |
| 6243 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6244 | { |
| 6245 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6246 | Py_ssize_t startinpos; |
| 6247 | Py_ssize_t endinpos; |
| 6248 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6249 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6250 | const char *end; |
| 6251 | const char *reason; |
| 6252 | PyObject *errorHandler = NULL; |
| 6253 | PyObject *exc = NULL; |
| 6254 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6255 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6256 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6257 | 1)) |
| 6258 | return NULL; |
| 6259 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6260 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6261 | 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] | 6262 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6263 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6264 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6265 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6266 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6267 | end = s + size; |
| 6268 | |
| 6269 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6270 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6271 | Py_UCS4 ch; |
| 6272 | /* We copy the raw representation one byte at a time because the |
| 6273 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6274 | ((char *) &uch)[0] = s[0]; |
| 6275 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6276 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6277 | ((char *) &uch)[2] = s[2]; |
| 6278 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6279 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6280 | ch = uch; |
| 6281 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6282 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6283 | some malformed UCS-4 data. */ |
| 6284 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6286 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6288 | end-s < Py_UNICODE_SIZE |
| 6289 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6290 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6291 | startinpos = s - starts; |
| 6292 | if (end-s < Py_UNICODE_SIZE) { |
| 6293 | endinpos = end-starts; |
| 6294 | reason = "truncated input"; |
| 6295 | } |
| 6296 | else { |
| 6297 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6298 | reason = "illegal code point (> 0x10FFFF)"; |
| 6299 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6300 | if (unicode_decode_call_errorhandler( |
| 6301 | errors, &errorHandler, |
| 6302 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6303 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6304 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6305 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6306 | continue; |
| 6307 | } |
| 6308 | |
| 6309 | s += Py_UNICODE_SIZE; |
| 6310 | #ifndef Py_UNICODE_WIDE |
| 6311 | if (ch >= 0xD800 && ch <= 0xDBFF && s < end) |
| 6312 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6313 | Py_UNICODE uch2; |
| 6314 | ((char *) &uch2)[0] = s[0]; |
| 6315 | ((char *) &uch2)[1] = s[1]; |
| 6316 | if (uch2 >= 0xDC00 && uch2 <= 0xDFFF) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6317 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6318 | ch = (((uch & 0x3FF)<<10) | (uch2 & 0x3FF)) + 0x10000; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6319 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6320 | } |
| 6321 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6322 | #endif |
| 6323 | |
| 6324 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6325 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6328 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6329 | goto onError; |
| 6330 | Py_XDECREF(errorHandler); |
| 6331 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6332 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6334 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6335 | Py_XDECREF(v); |
| 6336 | Py_XDECREF(errorHandler); |
| 6337 | Py_XDECREF(exc); |
| 6338 | return NULL; |
| 6339 | } |
| 6340 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6342 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6343 | PyObject * |
| 6344 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6345 | Py_ssize_t size, |
| 6346 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6349 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | } |
| 6351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6352 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6353 | static void |
| 6354 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6355 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6356 | PyObject *unicode, |
| 6357 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6358 | const char *reason) |
| 6359 | { |
| 6360 | if (*exceptionObject == NULL) { |
| 6361 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6362 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6363 | encoding, unicode, startpos, endpos, reason); |
| 6364 | } |
| 6365 | else { |
| 6366 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6367 | goto onError; |
| 6368 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6369 | goto onError; |
| 6370 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6371 | goto onError; |
| 6372 | return; |
| 6373 | onError: |
| 6374 | Py_DECREF(*exceptionObject); |
| 6375 | *exceptionObject = NULL; |
| 6376 | } |
| 6377 | } |
| 6378 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6380 | static void |
| 6381 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6382 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6383 | PyObject *unicode, |
| 6384 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6385 | const char *reason) |
| 6386 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6387 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6388 | encoding, unicode, startpos, endpos, reason); |
| 6389 | if (*exceptionObject != NULL) |
| 6390 | PyCodec_StrictErrors(*exceptionObject); |
| 6391 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6392 | |
| 6393 | /* error handling callback helper: |
| 6394 | build arguments, call the callback and check the arguments, |
| 6395 | put the result into newpos and return the replacement string, which |
| 6396 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6397 | static PyObject * |
| 6398 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6399 | PyObject **errorHandler, |
| 6400 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6401 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6402 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6403 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6404 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6405 | 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] | 6406 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6407 | PyObject *restuple; |
| 6408 | PyObject *resunicode; |
| 6409 | |
| 6410 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6413 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6416 | if (PyUnicode_READY(unicode) < 0) |
| 6417 | return NULL; |
| 6418 | len = PyUnicode_GET_LENGTH(unicode); |
| 6419 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6420 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6421 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | |
| 6425 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6427 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6429 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6430 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | Py_DECREF(restuple); |
| 6432 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6433 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6434 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | &resunicode, newpos)) { |
| 6436 | Py_DECREF(restuple); |
| 6437 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6439 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6440 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6441 | Py_DECREF(restuple); |
| 6442 | return NULL; |
| 6443 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6444 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6445 | *newpos = len + *newpos; |
| 6446 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6448 | Py_DECREF(restuple); |
| 6449 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6450 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6451 | Py_INCREF(resunicode); |
| 6452 | Py_DECREF(restuple); |
| 6453 | return resunicode; |
| 6454 | } |
| 6455 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6456 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6457 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6458 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6459 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6460 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6461 | /* input state */ |
| 6462 | Py_ssize_t pos=0, size; |
| 6463 | int kind; |
| 6464 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | /* output object */ |
| 6466 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6467 | /* pointer into the output */ |
| 6468 | char *str; |
| 6469 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6470 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6471 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6472 | 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] | 6473 | PyObject *errorHandler = NULL; |
| 6474 | PyObject *exc = NULL; |
| 6475 | /* the following variable is used for caching string comparisons |
| 6476 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6477 | int known_errorHandler = -1; |
| 6478 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6479 | if (PyUnicode_READY(unicode) < 0) |
| 6480 | return NULL; |
| 6481 | size = PyUnicode_GET_LENGTH(unicode); |
| 6482 | kind = PyUnicode_KIND(unicode); |
| 6483 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6484 | /* allocate enough for a simple encoding without |
| 6485 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6486 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6487 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6488 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6489 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6490 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6491 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6492 | ressize = size; |
| 6493 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6494 | while (pos < size) { |
| 6495 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | /* can we encode this? */ |
| 6498 | if (c<limit) { |
| 6499 | /* no overflow check, because we know that the space is enough */ |
| 6500 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6501 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6502 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6503 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6504 | Py_ssize_t requiredsize; |
| 6505 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6506 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6507 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6508 | Py_ssize_t collstart = pos; |
| 6509 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6511 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | ++collend; |
| 6513 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6514 | if (known_errorHandler==-1) { |
| 6515 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6516 | known_errorHandler = 1; |
| 6517 | else if (!strcmp(errors, "replace")) |
| 6518 | known_errorHandler = 2; |
| 6519 | else if (!strcmp(errors, "ignore")) |
| 6520 | known_errorHandler = 3; |
| 6521 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6522 | known_errorHandler = 4; |
| 6523 | else |
| 6524 | known_errorHandler = 0; |
| 6525 | } |
| 6526 | switch (known_errorHandler) { |
| 6527 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6528 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | goto onError; |
| 6530 | case 2: /* replace */ |
| 6531 | while (collstart++<collend) |
| 6532 | *str++ = '?'; /* fall through */ |
| 6533 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | break; |
| 6536 | case 4: /* xmlcharrefreplace */ |
| 6537 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6538 | /* determine replacement size */ |
| 6539 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6540 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6541 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6543 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6545 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6547 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6549 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | else |
| 6551 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6552 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6553 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6555 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | repsize += 2+6+1; |
| 6557 | else |
| 6558 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6559 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6561 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6562 | if (requiredsize > ressize) { |
| 6563 | if (requiredsize<2*ressize) |
| 6564 | requiredsize = 2*ressize; |
| 6565 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6566 | goto onError; |
| 6567 | str = PyBytes_AS_STRING(res) + respos; |
| 6568 | ressize = requiredsize; |
| 6569 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6570 | /* generate replacement */ |
| 6571 | for (i = collstart; i < collend; ++i) { |
| 6572 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6574 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6575 | break; |
| 6576 | default: |
| 6577 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6578 | encoding, reason, unicode, &exc, |
| 6579 | collstart, collend, &newpos); |
| 6580 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6581 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6582 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6583 | if (PyBytes_Check(repunicode)) { |
| 6584 | /* Directly copy bytes result to output. */ |
| 6585 | repsize = PyBytes_Size(repunicode); |
| 6586 | if (repsize > 1) { |
| 6587 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6588 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6589 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6590 | Py_DECREF(repunicode); |
| 6591 | goto onError; |
| 6592 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6593 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6594 | ressize += repsize-1; |
| 6595 | } |
| 6596 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6597 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6598 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6599 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6600 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | /* need more space? (at least enough for what we |
| 6603 | have+the replacement+the rest of the string, so |
| 6604 | we won't have to check space for encodable characters) */ |
| 6605 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6606 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6607 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | if (requiredsize > ressize) { |
| 6609 | if (requiredsize<2*ressize) |
| 6610 | requiredsize = 2*ressize; |
| 6611 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6612 | Py_DECREF(repunicode); |
| 6613 | goto onError; |
| 6614 | } |
| 6615 | str = PyBytes_AS_STRING(res) + respos; |
| 6616 | ressize = requiredsize; |
| 6617 | } |
| 6618 | /* check if there is anything unencodable in the replacement |
| 6619 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6621 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6622 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6623 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6624 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6625 | Py_DECREF(repunicode); |
| 6626 | goto onError; |
| 6627 | } |
| 6628 | *str = (char)c; |
| 6629 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6630 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6631 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6632 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6633 | } |
| 6634 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6635 | /* Resize if we allocated to much */ |
| 6636 | size = str - PyBytes_AS_STRING(res); |
| 6637 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6638 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6639 | if (_PyBytes_Resize(&res, size) < 0) |
| 6640 | goto onError; |
| 6641 | } |
| 6642 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6643 | Py_XDECREF(errorHandler); |
| 6644 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6645 | return res; |
| 6646 | |
| 6647 | onError: |
| 6648 | Py_XDECREF(res); |
| 6649 | Py_XDECREF(errorHandler); |
| 6650 | Py_XDECREF(exc); |
| 6651 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6654 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6655 | PyObject * |
| 6656 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6657 | Py_ssize_t size, |
| 6658 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6660 | PyObject *result; |
| 6661 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6662 | if (unicode == NULL) |
| 6663 | return NULL; |
| 6664 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6665 | Py_DECREF(unicode); |
| 6666 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6669 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6670 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | { |
| 6672 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6673 | PyErr_BadArgument(); |
| 6674 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6676 | if (PyUnicode_READY(unicode) == -1) |
| 6677 | return NULL; |
| 6678 | /* Fast path: if it is a one-byte string, construct |
| 6679 | bytes object directly. */ |
| 6680 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6681 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6682 | PyUnicode_GET_LENGTH(unicode)); |
| 6683 | /* Non-Latin-1 characters present. Defer to above function to |
| 6684 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6685 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | PyObject* |
| 6689 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6690 | { |
| 6691 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6695 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6696 | PyObject * |
| 6697 | PyUnicode_DecodeASCII(const char *s, |
| 6698 | Py_ssize_t size, |
| 6699 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6700 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6701 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6702 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6703 | int kind; |
| 6704 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6705 | Py_ssize_t startinpos; |
| 6706 | Py_ssize_t endinpos; |
| 6707 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6708 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6709 | int has_error; |
| 6710 | const unsigned char *p = (const unsigned char *)s; |
| 6711 | const unsigned char *end = p + size; |
| 6712 | 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] | 6713 | PyObject *errorHandler = NULL; |
| 6714 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6715 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6716 | if (size == 0) { |
| 6717 | Py_INCREF(unicode_empty); |
| 6718 | return unicode_empty; |
| 6719 | } |
| 6720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6722 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6723 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6724 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6725 | has_error = 0; |
| 6726 | while (p < end && !has_error) { |
| 6727 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6728 | an explanation. */ |
| 6729 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6730 | /* Help register allocation */ |
| 6731 | register const unsigned char *_p = p; |
| 6732 | while (_p < aligned_end) { |
| 6733 | unsigned long value = *(unsigned long *) _p; |
| 6734 | if (value & ASCII_CHAR_MASK) { |
| 6735 | has_error = 1; |
| 6736 | break; |
| 6737 | } |
| 6738 | _p += SIZEOF_LONG; |
| 6739 | } |
| 6740 | if (_p == end) |
| 6741 | break; |
| 6742 | if (has_error) |
| 6743 | break; |
| 6744 | p = _p; |
| 6745 | } |
| 6746 | if (*p & 0x80) { |
| 6747 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6748 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6749 | } |
| 6750 | else { |
| 6751 | ++p; |
| 6752 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6753 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6754 | if (!has_error) |
| 6755 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6756 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6757 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6759 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6761 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6762 | kind = PyUnicode_KIND(v); |
| 6763 | data = PyUnicode_DATA(v); |
| 6764 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6765 | e = s + size; |
| 6766 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | register unsigned char c = (unsigned char)*s; |
| 6768 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6769 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 | ++s; |
| 6771 | } |
| 6772 | else { |
| 6773 | startinpos = s-starts; |
| 6774 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6775 | if (unicode_decode_call_errorhandler( |
| 6776 | errors, &errorHandler, |
| 6777 | "ascii", "ordinal not in range(128)", |
| 6778 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6779 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6781 | kind = PyUnicode_KIND(v); |
| 6782 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6783 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6785 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6786 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6787 | Py_XDECREF(errorHandler); |
| 6788 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6789 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6790 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6791 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6792 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6794 | Py_XDECREF(errorHandler); |
| 6795 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | return NULL; |
| 6797 | } |
| 6798 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6799 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6800 | PyObject * |
| 6801 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6802 | Py_ssize_t size, |
| 6803 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6805 | PyObject *result; |
| 6806 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6807 | if (unicode == NULL) |
| 6808 | return NULL; |
| 6809 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6810 | Py_DECREF(unicode); |
| 6811 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | } |
| 6813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6814 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6815 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | { |
| 6817 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6818 | PyErr_BadArgument(); |
| 6819 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6821 | if (PyUnicode_READY(unicode) == -1) |
| 6822 | return NULL; |
| 6823 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6824 | directly. Else defer to above function to raise the exception. */ |
| 6825 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6826 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6827 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6828 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | PyObject * |
| 6832 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6833 | { |
| 6834 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6837 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6838 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6839 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6840 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6841 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6842 | #define NEED_RETRY |
| 6843 | #endif |
| 6844 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6845 | #ifndef WC_ERR_INVALID_CHARS |
| 6846 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6847 | #endif |
| 6848 | |
| 6849 | static char* |
| 6850 | code_page_name(UINT code_page, PyObject **obj) |
| 6851 | { |
| 6852 | *obj = NULL; |
| 6853 | if (code_page == CP_ACP) |
| 6854 | return "mbcs"; |
| 6855 | if (code_page == CP_UTF7) |
| 6856 | return "CP_UTF7"; |
| 6857 | if (code_page == CP_UTF8) |
| 6858 | return "CP_UTF8"; |
| 6859 | |
| 6860 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6861 | if (*obj == NULL) |
| 6862 | return NULL; |
| 6863 | return PyBytes_AS_STRING(*obj); |
| 6864 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6865 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6866 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6867 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6868 | { |
| 6869 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6870 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6871 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6872 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6873 | return 0; |
| 6874 | |
| 6875 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6876 | if (prev == curr) |
| 6877 | return 1; |
| 6878 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6879 | as it assumes an incomplete character consists of a single |
| 6880 | byte. */ |
| 6881 | if (curr - prev == 2) |
| 6882 | return 1; |
| 6883 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6884 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6885 | return 0; |
| 6886 | } |
| 6887 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | static DWORD |
| 6889 | decode_code_page_flags(UINT code_page) |
| 6890 | { |
| 6891 | if (code_page == CP_UTF7) { |
| 6892 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6893 | return 0; |
| 6894 | } |
| 6895 | else |
| 6896 | return MB_ERR_INVALID_CHARS; |
| 6897 | } |
| 6898 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6899 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6901 | * mode. |
| 6902 | * |
| 6903 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6904 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6905 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6906 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6907 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6908 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6909 | const char *in, |
| 6910 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6912 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6913 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6914 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6915 | |
| 6916 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6917 | assert(insize > 0); |
| 6918 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6919 | if (outsize <= 0) |
| 6920 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6921 | |
| 6922 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6924 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6925 | if (*v == NULL) |
| 6926 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6927 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6928 | } |
| 6929 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6930 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6931 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6932 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6933 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6934 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6935 | } |
| 6936 | |
| 6937 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6938 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6939 | if (outsize <= 0) |
| 6940 | goto error; |
| 6941 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6942 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6943 | error: |
| 6944 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6945 | return -2; |
| 6946 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6947 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6948 | } |
| 6949 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6950 | /* |
| 6951 | * Decode a byte string from a code page into unicode object with an error |
| 6952 | * handler. |
| 6953 | * |
| 6954 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6955 | * UnicodeDecodeError exception and returns -1 on error. |
| 6956 | */ |
| 6957 | static int |
| 6958 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6959 | PyObject **v, |
| 6960 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6961 | const char *errors) |
| 6962 | { |
| 6963 | const char *startin = in; |
| 6964 | const char *endin = in + size; |
| 6965 | const DWORD flags = decode_code_page_flags(code_page); |
| 6966 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6967 | 2000 English version of the message. */ |
| 6968 | const char *reason = "No mapping for the Unicode character exists " |
| 6969 | "in the target code page."; |
| 6970 | /* each step cannot decode more than 1 character, but a character can be |
| 6971 | represented as a surrogate pair */ |
| 6972 | wchar_t buffer[2], *startout, *out; |
| 6973 | int insize, outsize; |
| 6974 | PyObject *errorHandler = NULL; |
| 6975 | PyObject *exc = NULL; |
| 6976 | PyObject *encoding_obj = NULL; |
| 6977 | char *encoding; |
| 6978 | DWORD err; |
| 6979 | int ret = -1; |
| 6980 | |
| 6981 | assert(size > 0); |
| 6982 | |
| 6983 | encoding = code_page_name(code_page, &encoding_obj); |
| 6984 | if (encoding == NULL) |
| 6985 | return -1; |
| 6986 | |
| 6987 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6988 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6989 | UnicodeDecodeError. */ |
| 6990 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6991 | if (exc != NULL) { |
| 6992 | PyCodec_StrictErrors(exc); |
| 6993 | Py_CLEAR(exc); |
| 6994 | } |
| 6995 | goto error; |
| 6996 | } |
| 6997 | |
| 6998 | if (*v == NULL) { |
| 6999 | /* Create unicode object */ |
| 7000 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7001 | PyErr_NoMemory(); |
| 7002 | goto error; |
| 7003 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7004 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7005 | if (*v == NULL) |
| 7006 | goto error; |
| 7007 | startout = PyUnicode_AS_UNICODE(*v); |
| 7008 | } |
| 7009 | else { |
| 7010 | /* Extend unicode object */ |
| 7011 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7012 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7013 | PyErr_NoMemory(); |
| 7014 | goto error; |
| 7015 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7016 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7017 | goto error; |
| 7018 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7019 | } |
| 7020 | |
| 7021 | /* Decode the byte string character per character */ |
| 7022 | out = startout; |
| 7023 | while (in < endin) |
| 7024 | { |
| 7025 | /* Decode a character */ |
| 7026 | insize = 1; |
| 7027 | do |
| 7028 | { |
| 7029 | outsize = MultiByteToWideChar(code_page, flags, |
| 7030 | in, insize, |
| 7031 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7032 | if (outsize > 0) |
| 7033 | break; |
| 7034 | err = GetLastError(); |
| 7035 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7036 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7037 | { |
| 7038 | PyErr_SetFromWindowsErr(0); |
| 7039 | goto error; |
| 7040 | } |
| 7041 | insize++; |
| 7042 | } |
| 7043 | /* 4=maximum length of a UTF-8 sequence */ |
| 7044 | while (insize <= 4 && (in + insize) <= endin); |
| 7045 | |
| 7046 | if (outsize <= 0) { |
| 7047 | Py_ssize_t startinpos, endinpos, outpos; |
| 7048 | |
| 7049 | startinpos = in - startin; |
| 7050 | endinpos = startinpos + 1; |
| 7051 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7052 | if (unicode_decode_call_errorhandler( |
| 7053 | errors, &errorHandler, |
| 7054 | encoding, reason, |
| 7055 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7056 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7057 | { |
| 7058 | goto error; |
| 7059 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7060 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7061 | } |
| 7062 | else { |
| 7063 | in += insize; |
| 7064 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7065 | out += outsize; |
| 7066 | } |
| 7067 | } |
| 7068 | |
| 7069 | /* write a NUL character at the end */ |
| 7070 | *out = 0; |
| 7071 | |
| 7072 | /* Extend unicode object */ |
| 7073 | outsize = out - startout; |
| 7074 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7075 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7076 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7077 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7078 | |
| 7079 | error: |
| 7080 | Py_XDECREF(encoding_obj); |
| 7081 | Py_XDECREF(errorHandler); |
| 7082 | Py_XDECREF(exc); |
| 7083 | return ret; |
| 7084 | } |
| 7085 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7086 | static PyObject * |
| 7087 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7088 | const char *s, Py_ssize_t size, |
| 7089 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7090 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7091 | PyObject *v = NULL; |
| 7092 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7093 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | if (code_page < 0) { |
| 7095 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7096 | return NULL; |
| 7097 | } |
| 7098 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7099 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7100 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7101 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7102 | do |
| 7103 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7104 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7105 | if (size > INT_MAX) { |
| 7106 | chunk_size = INT_MAX; |
| 7107 | final = 0; |
| 7108 | done = 0; |
| 7109 | } |
| 7110 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7111 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7112 | { |
| 7113 | chunk_size = (int)size; |
| 7114 | final = (consumed == NULL); |
| 7115 | done = 1; |
| 7116 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7117 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7118 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7119 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7120 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7121 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7122 | if (chunk_size == 0 && done) { |
| 7123 | if (v != NULL) |
| 7124 | break; |
| 7125 | Py_INCREF(unicode_empty); |
| 7126 | return unicode_empty; |
| 7127 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7128 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7129 | |
| 7130 | converted = decode_code_page_strict(code_page, &v, |
| 7131 | s, chunk_size); |
| 7132 | if (converted == -2) |
| 7133 | converted = decode_code_page_errors(code_page, &v, |
| 7134 | s, chunk_size, |
| 7135 | errors); |
| 7136 | assert(converted != 0); |
| 7137 | |
| 7138 | if (converted < 0) { |
| 7139 | Py_XDECREF(v); |
| 7140 | return NULL; |
| 7141 | } |
| 7142 | |
| 7143 | if (consumed) |
| 7144 | *consumed += converted; |
| 7145 | |
| 7146 | s += converted; |
| 7147 | size -= converted; |
| 7148 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7149 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7150 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7153 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7154 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7155 | const char *s, |
| 7156 | Py_ssize_t size, |
| 7157 | const char *errors, |
| 7158 | Py_ssize_t *consumed) |
| 7159 | { |
| 7160 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7161 | } |
| 7162 | |
| 7163 | PyObject * |
| 7164 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7165 | Py_ssize_t size, |
| 7166 | const char *errors, |
| 7167 | Py_ssize_t *consumed) |
| 7168 | { |
| 7169 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7170 | } |
| 7171 | |
| 7172 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7173 | PyUnicode_DecodeMBCS(const char *s, |
| 7174 | Py_ssize_t size, |
| 7175 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7176 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7177 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7178 | } |
| 7179 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | static DWORD |
| 7181 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7182 | { |
| 7183 | if (code_page == CP_UTF8) { |
| 7184 | if (winver.dwMajorVersion >= 6) |
| 7185 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7186 | and later */ |
| 7187 | return WC_ERR_INVALID_CHARS; |
| 7188 | else |
| 7189 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7190 | return 0; |
| 7191 | } |
| 7192 | else if (code_page == CP_UTF7) { |
| 7193 | /* CP_UTF7 only supports flags=0 */ |
| 7194 | return 0; |
| 7195 | } |
| 7196 | else { |
| 7197 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7198 | return 0; |
| 7199 | else |
| 7200 | return WC_NO_BEST_FIT_CHARS; |
| 7201 | } |
| 7202 | } |
| 7203 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7204 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7206 | * mode. |
| 7207 | * |
| 7208 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7209 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7210 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7211 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7212 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7213 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7214 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7215 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7216 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7218 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7219 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7220 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7221 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7223 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7224 | /* Create a substring so that we can get the UTF-16 representation |
| 7225 | of just the slice under consideration. */ |
| 7226 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7227 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7228 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7229 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7230 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7231 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7232 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7233 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7234 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7235 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7236 | if (substring == NULL) |
| 7237 | return -1; |
| 7238 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7239 | if (p == NULL) { |
| 7240 | Py_DECREF(substring); |
| 7241 | return -1; |
| 7242 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7243 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7244 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 | outsize = WideCharToMultiByte(code_page, flags, |
| 7246 | p, size, |
| 7247 | NULL, 0, |
| 7248 | NULL, pusedDefaultChar); |
| 7249 | if (outsize <= 0) |
| 7250 | goto error; |
| 7251 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7252 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7253 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7254 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7255 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7256 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7257 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7258 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7259 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7260 | if (*outbytes == NULL) { |
| 7261 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7263 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7264 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7265 | } |
| 7266 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7269 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7270 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7271 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7274 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7275 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7277 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7282 | outsize = WideCharToMultiByte(code_page, flags, |
| 7283 | p, size, |
| 7284 | out, outsize, |
| 7285 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7286 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7287 | if (outsize <= 0) |
| 7288 | goto error; |
| 7289 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7290 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7291 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7292 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7293 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7294 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7295 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7296 | return -2; |
| 7297 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7298 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7299 | } |
| 7300 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7301 | /* |
| 7302 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7303 | * error handler. |
| 7304 | * |
| 7305 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7306 | * -1 on other error. |
| 7307 | */ |
| 7308 | static int |
| 7309 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7310 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7311 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7312 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7313 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7314 | Py_ssize_t pos = unicode_offset; |
| 7315 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7317 | 2000 English version of the message. */ |
| 7318 | const char *reason = "invalid character"; |
| 7319 | /* 4=maximum length of a UTF-8 sequence */ |
| 7320 | char buffer[4]; |
| 7321 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7322 | Py_ssize_t outsize; |
| 7323 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7324 | PyObject *errorHandler = NULL; |
| 7325 | PyObject *exc = NULL; |
| 7326 | PyObject *encoding_obj = NULL; |
| 7327 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7328 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7329 | PyObject *rep; |
| 7330 | int ret = -1; |
| 7331 | |
| 7332 | assert(insize > 0); |
| 7333 | |
| 7334 | encoding = code_page_name(code_page, &encoding_obj); |
| 7335 | if (encoding == NULL) |
| 7336 | return -1; |
| 7337 | |
| 7338 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7339 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7340 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7341 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7342 | if (exc != NULL) { |
| 7343 | PyCodec_StrictErrors(exc); |
| 7344 | Py_DECREF(exc); |
| 7345 | } |
| 7346 | Py_XDECREF(encoding_obj); |
| 7347 | return -1; |
| 7348 | } |
| 7349 | |
| 7350 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7351 | pusedDefaultChar = &usedDefaultChar; |
| 7352 | else |
| 7353 | pusedDefaultChar = NULL; |
| 7354 | |
| 7355 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7356 | PyErr_NoMemory(); |
| 7357 | goto error; |
| 7358 | } |
| 7359 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7360 | |
| 7361 | if (*outbytes == NULL) { |
| 7362 | /* Create string object */ |
| 7363 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7364 | if (*outbytes == NULL) |
| 7365 | goto error; |
| 7366 | out = PyBytes_AS_STRING(*outbytes); |
| 7367 | } |
| 7368 | else { |
| 7369 | /* Extend string object */ |
| 7370 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7371 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7372 | PyErr_NoMemory(); |
| 7373 | goto error; |
| 7374 | } |
| 7375 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7376 | goto error; |
| 7377 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7378 | } |
| 7379 | |
| 7380 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7381 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7382 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7383 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7384 | wchar_t chars[2]; |
| 7385 | int charsize; |
| 7386 | if (ch < 0x10000) { |
| 7387 | chars[0] = (wchar_t)ch; |
| 7388 | charsize = 1; |
| 7389 | } |
| 7390 | else { |
| 7391 | ch -= 0x10000; |
| 7392 | chars[0] = 0xd800 + (ch >> 10); |
| 7393 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7394 | charsize = 2; |
| 7395 | } |
| 7396 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7397 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7398 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7400 | NULL, pusedDefaultChar); |
| 7401 | if (outsize > 0) { |
| 7402 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7403 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7404 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7405 | memcpy(out, buffer, outsize); |
| 7406 | out += outsize; |
| 7407 | continue; |
| 7408 | } |
| 7409 | } |
| 7410 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7411 | PyErr_SetFromWindowsErr(0); |
| 7412 | goto error; |
| 7413 | } |
| 7414 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7415 | rep = unicode_encode_call_errorhandler( |
| 7416 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7417 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7418 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7419 | if (rep == NULL) |
| 7420 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7421 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7422 | |
| 7423 | if (PyBytes_Check(rep)) { |
| 7424 | outsize = PyBytes_GET_SIZE(rep); |
| 7425 | if (outsize != 1) { |
| 7426 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7427 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7428 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7429 | Py_DECREF(rep); |
| 7430 | goto error; |
| 7431 | } |
| 7432 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7433 | } |
| 7434 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7435 | out += outsize; |
| 7436 | } |
| 7437 | else { |
| 7438 | Py_ssize_t i; |
| 7439 | enum PyUnicode_Kind kind; |
| 7440 | void *data; |
| 7441 | |
| 7442 | if (PyUnicode_READY(rep) < 0) { |
| 7443 | Py_DECREF(rep); |
| 7444 | goto error; |
| 7445 | } |
| 7446 | |
| 7447 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7448 | if (outsize != 1) { |
| 7449 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7450 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7451 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7452 | Py_DECREF(rep); |
| 7453 | goto error; |
| 7454 | } |
| 7455 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7456 | } |
| 7457 | kind = PyUnicode_KIND(rep); |
| 7458 | data = PyUnicode_DATA(rep); |
| 7459 | for (i=0; i < outsize; i++) { |
| 7460 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7461 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7462 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7463 | encoding, unicode, |
| 7464 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7465 | "unable to encode error handler result to ASCII"); |
| 7466 | Py_DECREF(rep); |
| 7467 | goto error; |
| 7468 | } |
| 7469 | *out = (unsigned char)ch; |
| 7470 | out++; |
| 7471 | } |
| 7472 | } |
| 7473 | Py_DECREF(rep); |
| 7474 | } |
| 7475 | /* write a NUL byte */ |
| 7476 | *out = 0; |
| 7477 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7478 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7479 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7480 | goto error; |
| 7481 | ret = 0; |
| 7482 | |
| 7483 | error: |
| 7484 | Py_XDECREF(encoding_obj); |
| 7485 | Py_XDECREF(errorHandler); |
| 7486 | Py_XDECREF(exc); |
| 7487 | return ret; |
| 7488 | } |
| 7489 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7490 | static PyObject * |
| 7491 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7492 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7493 | const char *errors) |
| 7494 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7495 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7496 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7497 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7498 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7499 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7500 | if (PyUnicode_READY(unicode) < 0) |
| 7501 | return NULL; |
| 7502 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7503 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7504 | if (code_page < 0) { |
| 7505 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7506 | return NULL; |
| 7507 | } |
| 7508 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7509 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7510 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7511 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7512 | offset = 0; |
| 7513 | do |
| 7514 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7515 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7516 | /* 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] | 7517 | chunks. */ |
| 7518 | if (len > INT_MAX/2) { |
| 7519 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7520 | done = 0; |
| 7521 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7522 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7523 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7524 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7525 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7526 | done = 1; |
| 7527 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7528 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7529 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7530 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7531 | errors); |
| 7532 | if (ret == -2) |
| 7533 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7534 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7535 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7536 | if (ret < 0) { |
| 7537 | Py_XDECREF(outbytes); |
| 7538 | return NULL; |
| 7539 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7540 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7541 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7542 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7543 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7544 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7545 | return outbytes; |
| 7546 | } |
| 7547 | |
| 7548 | PyObject * |
| 7549 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7550 | Py_ssize_t size, |
| 7551 | const char *errors) |
| 7552 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7553 | PyObject *unicode, *res; |
| 7554 | unicode = PyUnicode_FromUnicode(p, size); |
| 7555 | if (unicode == NULL) |
| 7556 | return NULL; |
| 7557 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7558 | Py_DECREF(unicode); |
| 7559 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7560 | } |
| 7561 | |
| 7562 | PyObject * |
| 7563 | PyUnicode_EncodeCodePage(int code_page, |
| 7564 | PyObject *unicode, |
| 7565 | const char *errors) |
| 7566 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7567 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7568 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7570 | PyObject * |
| 7571 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7572 | { |
| 7573 | if (!PyUnicode_Check(unicode)) { |
| 7574 | PyErr_BadArgument(); |
| 7575 | return NULL; |
| 7576 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7577 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7578 | } |
| 7579 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7580 | #undef NEED_RETRY |
| 7581 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7582 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7585 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7586 | PyObject * |
| 7587 | PyUnicode_DecodeCharmap(const char *s, |
| 7588 | Py_ssize_t size, |
| 7589 | PyObject *mapping, |
| 7590 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7592 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7593 | Py_ssize_t startinpos; |
| 7594 | Py_ssize_t endinpos; |
| 7595 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7596 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7597 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7598 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7599 | PyObject *errorHandler = NULL; |
| 7600 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | /* Default to Latin-1 */ |
| 7603 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7604 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7605 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7606 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7609 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7610 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7611 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7612 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7613 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7614 | Py_ssize_t maplen; |
| 7615 | enum PyUnicode_Kind kind; |
| 7616 | void *data; |
| 7617 | Py_UCS4 x; |
| 7618 | |
| 7619 | if (PyUnicode_READY(mapping) < 0) |
| 7620 | return NULL; |
| 7621 | |
| 7622 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7623 | data = PyUnicode_DATA(mapping); |
| 7624 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7625 | while (s < e) { |
| 7626 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7628 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7629 | x = PyUnicode_READ(kind, data, ch); |
| 7630 | else |
| 7631 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7633 | if (x == 0xfffe) |
| 7634 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7635 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7636 | startinpos = s-starts; |
| 7637 | endinpos = startinpos+1; |
| 7638 | if (unicode_decode_call_errorhandler( |
| 7639 | errors, &errorHandler, |
| 7640 | "charmap", "character maps to <undefined>", |
| 7641 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7642 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | goto onError; |
| 7644 | } |
| 7645 | continue; |
| 7646 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7647 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7648 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7649 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7651 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7652 | } |
| 7653 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7654 | while (s < e) { |
| 7655 | unsigned char ch = *s; |
| 7656 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7658 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7659 | w = PyLong_FromLong((long)ch); |
| 7660 | if (w == NULL) |
| 7661 | goto onError; |
| 7662 | x = PyObject_GetItem(mapping, w); |
| 7663 | Py_DECREF(w); |
| 7664 | if (x == NULL) { |
| 7665 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7666 | /* No mapping found means: mapping is undefined. */ |
| 7667 | PyErr_Clear(); |
| 7668 | x = Py_None; |
| 7669 | Py_INCREF(x); |
| 7670 | } else |
| 7671 | goto onError; |
| 7672 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7673 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7674 | /* Apply mapping */ |
| 7675 | if (PyLong_Check(x)) { |
| 7676 | long value = PyLong_AS_LONG(x); |
| 7677 | if (value < 0 || value > 65535) { |
| 7678 | PyErr_SetString(PyExc_TypeError, |
| 7679 | "character mapping must be in range(65536)"); |
| 7680 | Py_DECREF(x); |
| 7681 | goto onError; |
| 7682 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7683 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7684 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | } |
| 7686 | else if (x == Py_None) { |
| 7687 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7688 | startinpos = s-starts; |
| 7689 | endinpos = startinpos+1; |
| 7690 | if (unicode_decode_call_errorhandler( |
| 7691 | errors, &errorHandler, |
| 7692 | "charmap", "character maps to <undefined>", |
| 7693 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7694 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7695 | Py_DECREF(x); |
| 7696 | goto onError; |
| 7697 | } |
| 7698 | Py_DECREF(x); |
| 7699 | continue; |
| 7700 | } |
| 7701 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7702 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7703 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7704 | if (PyUnicode_READY(x) < 0) |
| 7705 | goto onError; |
| 7706 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7707 | |
| 7708 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7709 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7710 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7711 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7712 | goto onError; |
| 7713 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | else if (targetsize > 1) { |
| 7715 | /* 1-n mapping */ |
| 7716 | if (targetsize > extrachars) { |
| 7717 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7719 | (targetsize << 2); |
| 7720 | extrachars += needed; |
| 7721 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7722 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7723 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7724 | Py_DECREF(x); |
| 7725 | goto onError; |
| 7726 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7728 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7729 | goto onError; |
| 7730 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7731 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7732 | extrachars -= targetsize; |
| 7733 | } |
| 7734 | /* 1-0 mapping: skip the character */ |
| 7735 | } |
| 7736 | else { |
| 7737 | /* wrong return value */ |
| 7738 | PyErr_SetString(PyExc_TypeError, |
| 7739 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7740 | Py_DECREF(x); |
| 7741 | goto onError; |
| 7742 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7743 | Py_DECREF(x); |
| 7744 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7745 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7747 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7748 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7749 | Py_XDECREF(errorHandler); |
| 7750 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7751 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7753 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7754 | Py_XDECREF(errorHandler); |
| 7755 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | Py_XDECREF(v); |
| 7757 | return NULL; |
| 7758 | } |
| 7759 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | /* Charmap encoding: the lookup table */ |
| 7761 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7762 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | PyObject_HEAD |
| 7764 | unsigned char level1[32]; |
| 7765 | int count2, count3; |
| 7766 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7767 | }; |
| 7768 | |
| 7769 | static PyObject* |
| 7770 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7771 | { |
| 7772 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7773 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7774 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7778 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7780 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7781 | }; |
| 7782 | |
| 7783 | static void |
| 7784 | encoding_map_dealloc(PyObject* o) |
| 7785 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7786 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7790 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | "EncodingMap", /*tp_name*/ |
| 7792 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7793 | 0, /*tp_itemsize*/ |
| 7794 | /* methods */ |
| 7795 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7796 | 0, /*tp_print*/ |
| 7797 | 0, /*tp_getattr*/ |
| 7798 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7799 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | 0, /*tp_repr*/ |
| 7801 | 0, /*tp_as_number*/ |
| 7802 | 0, /*tp_as_sequence*/ |
| 7803 | 0, /*tp_as_mapping*/ |
| 7804 | 0, /*tp_hash*/ |
| 7805 | 0, /*tp_call*/ |
| 7806 | 0, /*tp_str*/ |
| 7807 | 0, /*tp_getattro*/ |
| 7808 | 0, /*tp_setattro*/ |
| 7809 | 0, /*tp_as_buffer*/ |
| 7810 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7811 | 0, /*tp_doc*/ |
| 7812 | 0, /*tp_traverse*/ |
| 7813 | 0, /*tp_clear*/ |
| 7814 | 0, /*tp_richcompare*/ |
| 7815 | 0, /*tp_weaklistoffset*/ |
| 7816 | 0, /*tp_iter*/ |
| 7817 | 0, /*tp_iternext*/ |
| 7818 | encoding_map_methods, /*tp_methods*/ |
| 7819 | 0, /*tp_members*/ |
| 7820 | 0, /*tp_getset*/ |
| 7821 | 0, /*tp_base*/ |
| 7822 | 0, /*tp_dict*/ |
| 7823 | 0, /*tp_descr_get*/ |
| 7824 | 0, /*tp_descr_set*/ |
| 7825 | 0, /*tp_dictoffset*/ |
| 7826 | 0, /*tp_init*/ |
| 7827 | 0, /*tp_alloc*/ |
| 7828 | 0, /*tp_new*/ |
| 7829 | 0, /*tp_free*/ |
| 7830 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7831 | }; |
| 7832 | |
| 7833 | PyObject* |
| 7834 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7835 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7836 | PyObject *result; |
| 7837 | struct encoding_map *mresult; |
| 7838 | int i; |
| 7839 | int need_dict = 0; |
| 7840 | unsigned char level1[32]; |
| 7841 | unsigned char level2[512]; |
| 7842 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7843 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 | int kind; |
| 7845 | void *data; |
| 7846 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7847 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7849 | PyErr_BadArgument(); |
| 7850 | return NULL; |
| 7851 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7852 | kind = PyUnicode_KIND(string); |
| 7853 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | memset(level1, 0xFF, sizeof level1); |
| 7855 | memset(level2, 0xFF, sizeof level2); |
| 7856 | |
| 7857 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7858 | or if there are non-BMP characters, we need to use |
| 7859 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7861 | need_dict = 1; |
| 7862 | for (i = 1; i < 256; i++) { |
| 7863 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7864 | ch = PyUnicode_READ(kind, data, i); |
| 7865 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7866 | need_dict = 1; |
| 7867 | break; |
| 7868 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7869 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7870 | /* unmapped character */ |
| 7871 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7872 | l1 = ch >> 11; |
| 7873 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | if (level1[l1] == 0xFF) |
| 7875 | level1[l1] = count2++; |
| 7876 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7877 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7881 | need_dict = 1; |
| 7882 | |
| 7883 | if (need_dict) { |
| 7884 | PyObject *result = PyDict_New(); |
| 7885 | PyObject *key, *value; |
| 7886 | if (!result) |
| 7887 | return NULL; |
| 7888 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7889 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7890 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7891 | if (!key || !value) |
| 7892 | goto failed1; |
| 7893 | if (PyDict_SetItem(result, key, value) == -1) |
| 7894 | goto failed1; |
| 7895 | Py_DECREF(key); |
| 7896 | Py_DECREF(value); |
| 7897 | } |
| 7898 | return result; |
| 7899 | failed1: |
| 7900 | Py_XDECREF(key); |
| 7901 | Py_XDECREF(value); |
| 7902 | Py_DECREF(result); |
| 7903 | return NULL; |
| 7904 | } |
| 7905 | |
| 7906 | /* Create a three-level trie */ |
| 7907 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7908 | 16*count2 + 128*count3 - 1); |
| 7909 | if (!result) |
| 7910 | return PyErr_NoMemory(); |
| 7911 | PyObject_Init(result, &EncodingMapType); |
| 7912 | mresult = (struct encoding_map*)result; |
| 7913 | mresult->count2 = count2; |
| 7914 | mresult->count3 = count3; |
| 7915 | mlevel1 = mresult->level1; |
| 7916 | mlevel2 = mresult->level23; |
| 7917 | mlevel3 = mresult->level23 + 16*count2; |
| 7918 | memcpy(mlevel1, level1, 32); |
| 7919 | memset(mlevel2, 0xFF, 16*count2); |
| 7920 | memset(mlevel3, 0, 128*count3); |
| 7921 | count3 = 0; |
| 7922 | for (i = 1; i < 256; i++) { |
| 7923 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | /* unmapped character */ |
| 7926 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7927 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7928 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7929 | i2 = 16*mlevel1[o1] + o2; |
| 7930 | if (mlevel2[i2] == 0xFF) |
| 7931 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7932 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7933 | i3 = 128*mlevel2[i2] + o3; |
| 7934 | mlevel3[i3] = i; |
| 7935 | } |
| 7936 | return result; |
| 7937 | } |
| 7938 | |
| 7939 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7940 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7941 | { |
| 7942 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7943 | int l1 = c>>11; |
| 7944 | int l2 = (c>>7) & 0xF; |
| 7945 | int l3 = c & 0x7F; |
| 7946 | int i; |
| 7947 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7948 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7949 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7950 | if (c == 0) |
| 7951 | return 0; |
| 7952 | /* level 1*/ |
| 7953 | i = map->level1[l1]; |
| 7954 | if (i == 0xFF) { |
| 7955 | return -1; |
| 7956 | } |
| 7957 | /* level 2*/ |
| 7958 | i = map->level23[16*i+l2]; |
| 7959 | if (i == 0xFF) { |
| 7960 | return -1; |
| 7961 | } |
| 7962 | /* level 3 */ |
| 7963 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7964 | if (i == 0) { |
| 7965 | return -1; |
| 7966 | } |
| 7967 | return i; |
| 7968 | } |
| 7969 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7970 | /* Lookup the character ch in the mapping. If the character |
| 7971 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7972 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7973 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7974 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7976 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7977 | PyObject *x; |
| 7978 | |
| 7979 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7981 | x = PyObject_GetItem(mapping, w); |
| 7982 | Py_DECREF(w); |
| 7983 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7985 | /* No mapping found means: mapping is undefined. */ |
| 7986 | PyErr_Clear(); |
| 7987 | x = Py_None; |
| 7988 | Py_INCREF(x); |
| 7989 | return x; |
| 7990 | } else |
| 7991 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7993 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7994 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7995 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7996 | long value = PyLong_AS_LONG(x); |
| 7997 | if (value < 0 || value > 255) { |
| 7998 | PyErr_SetString(PyExc_TypeError, |
| 7999 | "character mapping must be in range(256)"); |
| 8000 | Py_DECREF(x); |
| 8001 | return NULL; |
| 8002 | } |
| 8003 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8004 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8005 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8006 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8008 | /* wrong return value */ |
| 8009 | PyErr_Format(PyExc_TypeError, |
| 8010 | "character mapping must return integer, bytes or None, not %.400s", |
| 8011 | x->ob_type->tp_name); |
| 8012 | Py_DECREF(x); |
| 8013 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | } |
| 8015 | } |
| 8016 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8017 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8018 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8019 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8020 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8021 | /* exponentially overallocate to minimize reallocations */ |
| 8022 | if (requiredsize < 2*outsize) |
| 8023 | requiredsize = 2*outsize; |
| 8024 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8025 | return -1; |
| 8026 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8027 | } |
| 8028 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8029 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8031 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8032 | /* 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] | 8033 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8034 | space is available. Return a new reference to the object that |
| 8035 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8036 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8037 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8038 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8039 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8040 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8041 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8042 | PyObject *rep; |
| 8043 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8044 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8045 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8046 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8047 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8048 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8049 | if (res == -1) |
| 8050 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | if (outsize<requiredsize) |
| 8052 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8053 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8054 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | outstart[(*outpos)++] = (char)res; |
| 8056 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8057 | } |
| 8058 | |
| 8059 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8060 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8061 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8062 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | Py_DECREF(rep); |
| 8064 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8065 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | if (PyLong_Check(rep)) { |
| 8067 | Py_ssize_t requiredsize = *outpos+1; |
| 8068 | if (outsize<requiredsize) |
| 8069 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8070 | Py_DECREF(rep); |
| 8071 | return enc_EXCEPTION; |
| 8072 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8073 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8075 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 | else { |
| 8077 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8078 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8079 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8080 | if (outsize<requiredsize) |
| 8081 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8082 | Py_DECREF(rep); |
| 8083 | return enc_EXCEPTION; |
| 8084 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8085 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8086 | memcpy(outstart + *outpos, repchars, repsize); |
| 8087 | *outpos += repsize; |
| 8088 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8089 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8090 | Py_DECREF(rep); |
| 8091 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | /* handle an error in PyUnicode_EncodeCharmap |
| 8095 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8096 | static int |
| 8097 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8098 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8099 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8100 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8101 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8102 | { |
| 8103 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8104 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8105 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8106 | enum PyUnicode_Kind kind; |
| 8107 | void *data; |
| 8108 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8109 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8110 | Py_ssize_t collstartpos = *inpos; |
| 8111 | Py_ssize_t collendpos = *inpos+1; |
| 8112 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8113 | char *encoding = "charmap"; |
| 8114 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8115 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8116 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8117 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8119 | if (PyUnicode_READY(unicode) < 0) |
| 8120 | return -1; |
| 8121 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8122 | /* find all unencodable characters */ |
| 8123 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8124 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8125 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8126 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8127 | val = encoding_map_lookup(ch, mapping); |
| 8128 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8129 | break; |
| 8130 | ++collendpos; |
| 8131 | continue; |
| 8132 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8133 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8134 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8135 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | if (rep==NULL) |
| 8137 | return -1; |
| 8138 | else if (rep!=Py_None) { |
| 8139 | Py_DECREF(rep); |
| 8140 | break; |
| 8141 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8142 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8143 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8144 | } |
| 8145 | /* cache callback name lookup |
| 8146 | * (if not done yet, i.e. it's the first error) */ |
| 8147 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8149 | *known_errorHandler = 1; |
| 8150 | else if (!strcmp(errors, "replace")) |
| 8151 | *known_errorHandler = 2; |
| 8152 | else if (!strcmp(errors, "ignore")) |
| 8153 | *known_errorHandler = 3; |
| 8154 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8155 | *known_errorHandler = 4; |
| 8156 | else |
| 8157 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8158 | } |
| 8159 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8160 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8161 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8162 | return -1; |
| 8163 | case 2: /* replace */ |
| 8164 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | x = charmapencode_output('?', mapping, res, respos); |
| 8166 | if (x==enc_EXCEPTION) { |
| 8167 | return -1; |
| 8168 | } |
| 8169 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8170 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 | return -1; |
| 8172 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8173 | } |
| 8174 | /* fall through */ |
| 8175 | case 3: /* ignore */ |
| 8176 | *inpos = collendpos; |
| 8177 | break; |
| 8178 | case 4: /* xmlcharrefreplace */ |
| 8179 | /* generate replacement (temporarily (mis)uses p) */ |
| 8180 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | char buffer[2+29+1+1]; |
| 8182 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8183 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | for (cp = buffer; *cp; ++cp) { |
| 8185 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8186 | if (x==enc_EXCEPTION) |
| 8187 | return -1; |
| 8188 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8189 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | return -1; |
| 8191 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8192 | } |
| 8193 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 | *inpos = collendpos; |
| 8195 | break; |
| 8196 | default: |
| 8197 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8198 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8199 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8200 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8201 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8202 | if (PyBytes_Check(repunicode)) { |
| 8203 | /* Directly copy bytes result to output. */ |
| 8204 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8205 | Py_ssize_t requiredsize; |
| 8206 | repsize = PyBytes_Size(repunicode); |
| 8207 | requiredsize = *respos + repsize; |
| 8208 | if (requiredsize > outsize) |
| 8209 | /* Make room for all additional bytes. */ |
| 8210 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8211 | Py_DECREF(repunicode); |
| 8212 | return -1; |
| 8213 | } |
| 8214 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8215 | PyBytes_AsString(repunicode), repsize); |
| 8216 | *respos += repsize; |
| 8217 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8218 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8219 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8220 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8221 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8222 | if (PyUnicode_READY(repunicode) < 0) { |
| 8223 | Py_DECREF(repunicode); |
| 8224 | return -1; |
| 8225 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8226 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8227 | data = PyUnicode_DATA(repunicode); |
| 8228 | kind = PyUnicode_KIND(repunicode); |
| 8229 | for (index = 0; index < repsize; index++) { |
| 8230 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8231 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8233 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | return -1; |
| 8235 | } |
| 8236 | else if (x==enc_FAILED) { |
| 8237 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8238 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8239 | return -1; |
| 8240 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8241 | } |
| 8242 | *inpos = newpos; |
| 8243 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | } |
| 8245 | return 0; |
| 8246 | } |
| 8247 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8248 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8249 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8250 | PyObject *mapping, |
| 8251 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8252 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8253 | /* output object */ |
| 8254 | PyObject *res = NULL; |
| 8255 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8256 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8257 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8258 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8259 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | PyObject *errorHandler = NULL; |
| 8261 | PyObject *exc = NULL; |
| 8262 | /* the following variable is used for caching string comparisons |
| 8263 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8264 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8265 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8267 | if (PyUnicode_READY(unicode) < 0) |
| 8268 | return NULL; |
| 8269 | size = PyUnicode_GET_LENGTH(unicode); |
| 8270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8271 | /* Default to Latin-1 */ |
| 8272 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8273 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8274 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | /* allocate enough for a simple encoding without |
| 8276 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8277 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8278 | if (res == NULL) |
| 8279 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8280 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8281 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8283 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8284 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8286 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | if (x==enc_EXCEPTION) /* error */ |
| 8288 | goto onError; |
| 8289 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8290 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 | &exc, |
| 8292 | &known_errorHandler, &errorHandler, errors, |
| 8293 | &res, &respos)) { |
| 8294 | goto onError; |
| 8295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8296 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | else |
| 8298 | /* done with this character => adjust input position */ |
| 8299 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8301 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8303 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8304 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8305 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8306 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8307 | Py_XDECREF(exc); |
| 8308 | Py_XDECREF(errorHandler); |
| 8309 | return res; |
| 8310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8312 | Py_XDECREF(res); |
| 8313 | Py_XDECREF(exc); |
| 8314 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | return NULL; |
| 8316 | } |
| 8317 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8318 | /* Deprecated */ |
| 8319 | PyObject * |
| 8320 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8321 | Py_ssize_t size, |
| 8322 | PyObject *mapping, |
| 8323 | const char *errors) |
| 8324 | { |
| 8325 | PyObject *result; |
| 8326 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8327 | if (unicode == NULL) |
| 8328 | return NULL; |
| 8329 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8330 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8331 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8334 | PyObject * |
| 8335 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8336 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | { |
| 8338 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8339 | PyErr_BadArgument(); |
| 8340 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8342 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | } |
| 8344 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8345 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8346 | static void |
| 8347 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8348 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8349 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8350 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8353 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8354 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8355 | } |
| 8356 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8358 | goto onError; |
| 8359 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8360 | goto onError; |
| 8361 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8362 | goto onError; |
| 8363 | return; |
| 8364 | onError: |
| 8365 | Py_DECREF(*exceptionObject); |
| 8366 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8367 | } |
| 8368 | } |
| 8369 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8371 | static void |
| 8372 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8373 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8374 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8375 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8376 | { |
| 8377 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8378 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8379 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | } |
| 8382 | |
| 8383 | /* error handling callback helper: |
| 8384 | build arguments, call the callback and check the arguments, |
| 8385 | put the result into newpos and return the replacement string, which |
| 8386 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8387 | static PyObject * |
| 8388 | unicode_translate_call_errorhandler(const char *errors, |
| 8389 | PyObject **errorHandler, |
| 8390 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8391 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8392 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8393 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8395 | 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] | 8396 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8397 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8398 | PyObject *restuple; |
| 8399 | PyObject *resunicode; |
| 8400 | |
| 8401 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8403 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | } |
| 8406 | |
| 8407 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8408 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8409 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8411 | |
| 8412 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8413 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8414 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8417 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8418 | Py_DECREF(restuple); |
| 8419 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | } |
| 8421 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | &resunicode, &i_newpos)) { |
| 8423 | Py_DECREF(restuple); |
| 8424 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8425 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8426 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8428 | else |
| 8429 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8430 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8432 | Py_DECREF(restuple); |
| 8433 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8434 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8435 | Py_INCREF(resunicode); |
| 8436 | Py_DECREF(restuple); |
| 8437 | return resunicode; |
| 8438 | } |
| 8439 | |
| 8440 | /* Lookup the character ch in the mapping and put the result in result, |
| 8441 | which must be decrefed by the caller. |
| 8442 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8443 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8444 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8445 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8446 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8447 | PyObject *x; |
| 8448 | |
| 8449 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8451 | x = PyObject_GetItem(mapping, w); |
| 8452 | Py_DECREF(w); |
| 8453 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8455 | /* No mapping found means: use 1:1 mapping. */ |
| 8456 | PyErr_Clear(); |
| 8457 | *result = NULL; |
| 8458 | return 0; |
| 8459 | } else |
| 8460 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8461 | } |
| 8462 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | *result = x; |
| 8464 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8466 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8467 | long value = PyLong_AS_LONG(x); |
| 8468 | long max = PyUnicode_GetMax(); |
| 8469 | if (value < 0 || value > max) { |
| 8470 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8471 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | Py_DECREF(x); |
| 8473 | return -1; |
| 8474 | } |
| 8475 | *result = x; |
| 8476 | return 0; |
| 8477 | } |
| 8478 | else if (PyUnicode_Check(x)) { |
| 8479 | *result = x; |
| 8480 | return 0; |
| 8481 | } |
| 8482 | else { |
| 8483 | /* wrong return value */ |
| 8484 | PyErr_SetString(PyExc_TypeError, |
| 8485 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8486 | Py_DECREF(x); |
| 8487 | return -1; |
| 8488 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8489 | } |
| 8490 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | if not reallocate and adjust various state variables. |
| 8492 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8493 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8495 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8496 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8497 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8498 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | /* exponentially overallocate to minimize reallocations */ |
| 8500 | if (requiredsize < 2 * oldsize) |
| 8501 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8503 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8505 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8506 | } |
| 8507 | return 0; |
| 8508 | } |
| 8509 | /* lookup the character, put the result in the output string and adjust |
| 8510 | various state variables. Return a new reference to the object that |
| 8511 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8512 | undefined (in which case no character was written). |
| 8513 | The called must decref result. |
| 8514 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8515 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8517 | PyObject *mapping, Py_UCS4 **output, |
| 8518 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8519 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8520 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8522 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8524 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8527 | } |
| 8528 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8530 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8531 | /* 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] | 8532 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | } |
| 8534 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | Py_ssize_t repsize; |
| 8536 | if (PyUnicode_READY(*res) == -1) |
| 8537 | return -1; |
| 8538 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8539 | if (repsize==1) { |
| 8540 | /* 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] | 8541 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8542 | } |
| 8543 | else if (repsize!=0) { |
| 8544 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8545 | Py_ssize_t requiredsize = *opos + |
| 8546 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8547 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8548 | Py_ssize_t i; |
| 8549 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8551 | for(i = 0; i < repsize; i++) |
| 8552 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8554 | } |
| 8555 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8556 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8557 | return 0; |
| 8558 | } |
| 8559 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8560 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8561 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8562 | PyObject *mapping, |
| 8563 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8564 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8565 | /* input object */ |
| 8566 | char *idata; |
| 8567 | Py_ssize_t size, i; |
| 8568 | int kind; |
| 8569 | /* output buffer */ |
| 8570 | Py_UCS4 *output = NULL; |
| 8571 | Py_ssize_t osize; |
| 8572 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8573 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8574 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | char *reason = "character maps to <undefined>"; |
| 8576 | PyObject *errorHandler = NULL; |
| 8577 | PyObject *exc = NULL; |
| 8578 | /* the following variable is used for caching string comparisons |
| 8579 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8580 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8581 | int known_errorHandler = -1; |
| 8582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | PyErr_BadArgument(); |
| 8585 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8586 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8587 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | if (PyUnicode_READY(input) == -1) |
| 8589 | return NULL; |
| 8590 | idata = (char*)PyUnicode_DATA(input); |
| 8591 | kind = PyUnicode_KIND(input); |
| 8592 | size = PyUnicode_GET_LENGTH(input); |
| 8593 | i = 0; |
| 8594 | |
| 8595 | if (size == 0) { |
| 8596 | Py_INCREF(input); |
| 8597 | return input; |
| 8598 | } |
| 8599 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8600 | /* allocate enough for a simple 1:1 translation without |
| 8601 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | osize = size; |
| 8603 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8604 | opos = 0; |
| 8605 | if (output == NULL) { |
| 8606 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8610 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | /* try to encode it */ |
| 8612 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | if (charmaptranslate_output(input, i, mapping, |
| 8614 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8615 | Py_XDECREF(x); |
| 8616 | goto onError; |
| 8617 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8618 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8619 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8620 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8621 | else { /* untranslatable character */ |
| 8622 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8623 | Py_ssize_t repsize; |
| 8624 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8625 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8626 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | Py_ssize_t collstart = i; |
| 8628 | Py_ssize_t collend = i+1; |
| 8629 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8630 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8631 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | while (collend < size) { |
| 8633 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8634 | goto onError; |
| 8635 | Py_XDECREF(x); |
| 8636 | if (x!=Py_None) |
| 8637 | break; |
| 8638 | ++collend; |
| 8639 | } |
| 8640 | /* cache callback name lookup |
| 8641 | * (if not done yet, i.e. it's the first error) */ |
| 8642 | if (known_errorHandler==-1) { |
| 8643 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8644 | known_errorHandler = 1; |
| 8645 | else if (!strcmp(errors, "replace")) |
| 8646 | known_errorHandler = 2; |
| 8647 | else if (!strcmp(errors, "ignore")) |
| 8648 | known_errorHandler = 3; |
| 8649 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8650 | known_errorHandler = 4; |
| 8651 | else |
| 8652 | known_errorHandler = 0; |
| 8653 | } |
| 8654 | switch (known_errorHandler) { |
| 8655 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | raise_translate_exception(&exc, input, collstart, |
| 8657 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8658 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | case 2: /* replace */ |
| 8660 | /* 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] | 8661 | for (coll = collstart; coll<collend; coll++) |
| 8662 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8663 | /* fall through */ |
| 8664 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8665 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8666 | break; |
| 8667 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8668 | /* generate replacement (temporarily (mis)uses i) */ |
| 8669 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8670 | char buffer[2+29+1+1]; |
| 8671 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8672 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8673 | if (charmaptranslate_makespace(&output, &osize, |
| 8674 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8675 | goto onError; |
| 8676 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | break; |
| 8681 | default: |
| 8682 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8683 | reason, input, &exc, |
| 8684 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8685 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8687 | if (PyUnicode_READY(repunicode) < 0) { |
| 8688 | Py_DECREF(repunicode); |
| 8689 | goto onError; |
| 8690 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8691 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8692 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8693 | if (charmaptranslate_makespace(&output, &osize, |
| 8694 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8695 | Py_DECREF(repunicode); |
| 8696 | goto onError; |
| 8697 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8699 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8700 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8702 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8703 | } |
| 8704 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8705 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8706 | if (!res) |
| 8707 | goto onError; |
| 8708 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8709 | Py_XDECREF(exc); |
| 8710 | Py_XDECREF(errorHandler); |
| 8711 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8712 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8713 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8715 | Py_XDECREF(exc); |
| 8716 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | return NULL; |
| 8718 | } |
| 8719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8721 | PyObject * |
| 8722 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8723 | Py_ssize_t size, |
| 8724 | PyObject *mapping, |
| 8725 | const char *errors) |
| 8726 | { |
| 8727 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8728 | if (!unicode) |
| 8729 | return NULL; |
| 8730 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8731 | } |
| 8732 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8733 | PyObject * |
| 8734 | PyUnicode_Translate(PyObject *str, |
| 8735 | PyObject *mapping, |
| 8736 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8737 | { |
| 8738 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | str = PyUnicode_FromObject(str); |
| 8741 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8742 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8743 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8744 | Py_DECREF(str); |
| 8745 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8747 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | Py_XDECREF(str); |
| 8749 | return NULL; |
| 8750 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8753 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | { |
| 8755 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8756 | called as a callback from fixup() which does it already. */ |
| 8757 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8758 | const int kind = PyUnicode_KIND(self); |
| 8759 | void *data = PyUnicode_DATA(self); |
| 8760 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8761 | Py_ssize_t i; |
| 8762 | |
| 8763 | for (i = 0; i < len; ++i) { |
| 8764 | ch = PyUnicode_READ(kind, data, i); |
| 8765 | fixed = 0; |
| 8766 | if (ch > 127) { |
| 8767 | if (Py_UNICODE_ISSPACE(ch)) |
| 8768 | fixed = ' '; |
| 8769 | else { |
| 8770 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8771 | if (decimal >= 0) |
| 8772 | fixed = '0' + decimal; |
| 8773 | } |
| 8774 | if (fixed != 0) { |
| 8775 | if (fixed > maxchar) |
| 8776 | maxchar = fixed; |
| 8777 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8778 | } |
| 8779 | else if (ch > maxchar) |
| 8780 | maxchar = ch; |
| 8781 | } |
| 8782 | else if (ch > maxchar) |
| 8783 | maxchar = ch; |
| 8784 | } |
| 8785 | |
| 8786 | return maxchar; |
| 8787 | } |
| 8788 | |
| 8789 | PyObject * |
| 8790 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8791 | { |
| 8792 | if (!PyUnicode_Check(unicode)) { |
| 8793 | PyErr_BadInternalCall(); |
| 8794 | return NULL; |
| 8795 | } |
| 8796 | if (PyUnicode_READY(unicode) == -1) |
| 8797 | return NULL; |
| 8798 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8799 | /* If the string is already ASCII, just return the same string */ |
| 8800 | Py_INCREF(unicode); |
| 8801 | return unicode; |
| 8802 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8803 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8804 | } |
| 8805 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8806 | PyObject * |
| 8807 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8808 | Py_ssize_t length) |
| 8809 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8810 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8811 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8812 | Py_UCS4 maxchar; |
| 8813 | enum PyUnicode_Kind kind; |
| 8814 | void *data; |
| 8815 | |
| 8816 | maxchar = 0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8817 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8818 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8819 | if (ch > 127) { |
| 8820 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8821 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8822 | ch = '0' + decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8823 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8824 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8825 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8826 | |
| 8827 | /* Copy to a new string */ |
| 8828 | decimal = PyUnicode_New(length, maxchar); |
| 8829 | if (decimal == NULL) |
| 8830 | return decimal; |
| 8831 | kind = PyUnicode_KIND(decimal); |
| 8832 | data = PyUnicode_DATA(decimal); |
| 8833 | /* Iterate over code points */ |
| 8834 | for (i = 0; i < length; i++) { |
| 8835 | Py_UNICODE ch = s[i]; |
| 8836 | if (ch > 127) { |
| 8837 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8838 | if (decimal >= 0) |
| 8839 | ch = '0' + decimal; |
| 8840 | } |
| 8841 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8842 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8843 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8844 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8845 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8846 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8847 | int |
| 8848 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8849 | Py_ssize_t length, |
| 8850 | char *output, |
| 8851 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8852 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8853 | PyObject *errorHandler = NULL; |
| 8854 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8855 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8856 | const char *encoding = "decimal"; |
| 8857 | const char *reason = "invalid decimal Unicode string"; |
| 8858 | /* the following variable is used for caching string comparisons |
| 8859 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8860 | int known_errorHandler = -1; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8861 | Py_ssize_t i, j; |
| 8862 | enum PyUnicode_Kind kind; |
| 8863 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8864 | |
| 8865 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8866 | PyErr_BadArgument(); |
| 8867 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8868 | } |
| 8869 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8870 | unicode = PyUnicode_FromUnicode(s, length); |
| 8871 | if (unicode == NULL) |
| 8872 | return -1; |
| 8873 | |
| 8874 | if (PyUnicode_READY(unicode) < 0) |
| 8875 | goto onError; |
| 8876 | kind = PyUnicode_KIND(unicode); |
| 8877 | data = PyUnicode_DATA(unicode); |
| 8878 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8879 | for (i=0; i < length; ) { |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8880 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8881 | int decimal; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8882 | Py_ssize_t startpos, endpos; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8883 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8884 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8885 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8886 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8888 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8890 | if (decimal >= 0) { |
| 8891 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8892 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8893 | continue; |
| 8894 | } |
| 8895 | if (0 < ch && ch < 256) { |
| 8896 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8897 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | continue; |
| 8899 | } |
| 8900 | /* All other characters are considered unencodable */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8901 | startpos = i; |
| 8902 | endpos = i+1; |
| 8903 | for (; endpos < length; endpos++) { |
| 8904 | ch = PyUnicode_READ(kind, data, endpos); |
| 8905 | if ((0 < ch && ch < 256) || |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8906 | Py_UNICODE_ISSPACE(ch) || |
| 8907 | 0 <= Py_UNICODE_TODECIMAL(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8908 | break; |
| 8909 | } |
| 8910 | /* cache callback name lookup |
| 8911 | * (if not done yet, i.e. it's the first error) */ |
| 8912 | if (known_errorHandler==-1) { |
| 8913 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8914 | known_errorHandler = 1; |
| 8915 | else if (!strcmp(errors, "replace")) |
| 8916 | known_errorHandler = 2; |
| 8917 | else if (!strcmp(errors, "ignore")) |
| 8918 | known_errorHandler = 3; |
| 8919 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8920 | known_errorHandler = 4; |
| 8921 | else |
| 8922 | known_errorHandler = 0; |
| 8923 | } |
| 8924 | switch (known_errorHandler) { |
| 8925 | case 1: /* strict */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8926 | raise_encode_exception(&exc, encoding, unicode, startpos, endpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8927 | goto onError; |
| 8928 | case 2: /* replace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8929 | for (j=startpos; j < endpos; j++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8930 | *output++ = '?'; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8931 | i = endpos; |
| 8932 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8933 | case 3: /* ignore */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8934 | i = endpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | break; |
| 8936 | case 4: /* xmlcharrefreplace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8937 | /* generate replacement */ |
| 8938 | for (j=startpos; j < endpos; j++) { |
| 8939 | ch = PyUnicode_READ(kind, data, i); |
| 8940 | output += sprintf(output, "&#%d;", (int)ch); |
| 8941 | i++; |
| 8942 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8943 | break; |
| 8944 | default: |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8945 | { |
| 8946 | PyObject *repunicode; |
| 8947 | Py_ssize_t repsize, newpos, k; |
| 8948 | enum PyUnicode_Kind repkind; |
| 8949 | void *repdata; |
| 8950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8951 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8952 | encoding, reason, unicode, &exc, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8953 | startpos, endpos, &newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8954 | if (repunicode == NULL) |
| 8955 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8956 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8957 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8958 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8959 | Py_DECREF(repunicode); |
| 8960 | goto onError; |
| 8961 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8962 | if (PyUnicode_READY(repunicode) < 0) { |
| 8963 | Py_DECREF(repunicode); |
| 8964 | goto onError; |
| 8965 | } |
| 8966 | repkind = PyUnicode_KIND(repunicode); |
| 8967 | repdata = PyUnicode_DATA(repunicode); |
| 8968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | /* generate replacement */ |
| 8970 | repsize = PyUnicode_GET_SIZE(repunicode); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8971 | for (k=0; k<repsize; k++) { |
| 8972 | ch = PyUnicode_READ(repkind, repdata, k); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8973 | if (Py_UNICODE_ISSPACE(ch)) |
| 8974 | *output++ = ' '; |
| 8975 | else { |
| 8976 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8977 | if (decimal >= 0) |
| 8978 | *output++ = '0' + decimal; |
| 8979 | else if (0 < ch && ch < 256) |
| 8980 | *output++ = (char)ch; |
| 8981 | else { |
| 8982 | Py_DECREF(repunicode); |
| 8983 | raise_encode_exception(&exc, encoding, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8984 | unicode, startpos, endpos, |
| 8985 | reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8986 | goto onError; |
| 8987 | } |
| 8988 | } |
| 8989 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8990 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | Py_DECREF(repunicode); |
| 8992 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8993 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8994 | } |
| 8995 | /* 0-terminate the output string */ |
| 8996 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8997 | Py_XDECREF(exc); |
| 8998 | Py_XDECREF(errorHandler); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8999 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9000 | return 0; |
| 9001 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9002 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9003 | Py_XDECREF(exc); |
| 9004 | Py_XDECREF(errorHandler); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9005 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9006 | return -1; |
| 9007 | } |
| 9008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | /* --- Helpers ------------------------------------------------------------ */ |
| 9010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9012 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9013 | Py_ssize_t start, |
| 9014 | Py_ssize_t end) |
| 9015 | { |
| 9016 | int kind1, kind2, kind; |
| 9017 | void *buf1, *buf2; |
| 9018 | Py_ssize_t len1, len2, result; |
| 9019 | |
| 9020 | kind1 = PyUnicode_KIND(s1); |
| 9021 | kind2 = PyUnicode_KIND(s2); |
| 9022 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9023 | buf1 = PyUnicode_DATA(s1); |
| 9024 | buf2 = PyUnicode_DATA(s2); |
| 9025 | if (kind1 != kind) |
| 9026 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9027 | if (!buf1) |
| 9028 | return -2; |
| 9029 | if (kind2 != kind) |
| 9030 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9031 | if (!buf2) { |
| 9032 | if (kind1 != kind) PyMem_Free(buf1); |
| 9033 | return -2; |
| 9034 | } |
| 9035 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9036 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9037 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9038 | if (direction > 0) { |
| 9039 | switch(kind) { |
| 9040 | case PyUnicode_1BYTE_KIND: |
| 9041 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9042 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9043 | else |
| 9044 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9045 | break; |
| 9046 | case PyUnicode_2BYTE_KIND: |
| 9047 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9048 | break; |
| 9049 | case PyUnicode_4BYTE_KIND: |
| 9050 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9051 | break; |
| 9052 | default: |
| 9053 | assert(0); result = -2; |
| 9054 | } |
| 9055 | } |
| 9056 | else { |
| 9057 | switch(kind) { |
| 9058 | case PyUnicode_1BYTE_KIND: |
| 9059 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9060 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9061 | else |
| 9062 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9063 | break; |
| 9064 | case PyUnicode_2BYTE_KIND: |
| 9065 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9066 | break; |
| 9067 | case PyUnicode_4BYTE_KIND: |
| 9068 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9069 | break; |
| 9070 | default: |
| 9071 | assert(0); result = -2; |
| 9072 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9073 | } |
| 9074 | |
| 9075 | if (kind1 != kind) |
| 9076 | PyMem_Free(buf1); |
| 9077 | if (kind2 != kind) |
| 9078 | PyMem_Free(buf2); |
| 9079 | |
| 9080 | return result; |
| 9081 | } |
| 9082 | |
| 9083 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9084 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9085 | Py_ssize_t n_buffer, |
| 9086 | void *digits, Py_ssize_t n_digits, |
| 9087 | Py_ssize_t min_width, |
| 9088 | const char *grouping, |
| 9089 | const char *thousands_sep) |
| 9090 | { |
| 9091 | switch(kind) { |
| 9092 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9093 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9094 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9095 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9096 | min_width, grouping, thousands_sep); |
| 9097 | else |
| 9098 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9099 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9100 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9101 | case PyUnicode_2BYTE_KIND: |
| 9102 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9103 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9104 | min_width, grouping, thousands_sep); |
| 9105 | case PyUnicode_4BYTE_KIND: |
| 9106 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9107 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9108 | min_width, grouping, thousands_sep); |
| 9109 | } |
| 9110 | assert(0); |
| 9111 | return -1; |
| 9112 | } |
| 9113 | |
| 9114 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9115 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9116 | #define ADJUST_INDICES(start, end, len) \ |
| 9117 | if (end > len) \ |
| 9118 | end = len; \ |
| 9119 | else if (end < 0) { \ |
| 9120 | end += len; \ |
| 9121 | if (end < 0) \ |
| 9122 | end = 0; \ |
| 9123 | } \ |
| 9124 | if (start < 0) { \ |
| 9125 | start += len; \ |
| 9126 | if (start < 0) \ |
| 9127 | start = 0; \ |
| 9128 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9129 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9130 | Py_ssize_t |
| 9131 | PyUnicode_Count(PyObject *str, |
| 9132 | PyObject *substr, |
| 9133 | Py_ssize_t start, |
| 9134 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9135 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9136 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9137 | PyObject* str_obj; |
| 9138 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9139 | int kind1, kind2, kind; |
| 9140 | void *buf1 = NULL, *buf2 = NULL; |
| 9141 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9142 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9143 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9144 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9145 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9146 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9147 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9148 | Py_DECREF(str_obj); |
| 9149 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9151 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9152 | kind1 = PyUnicode_KIND(str_obj); |
| 9153 | kind2 = PyUnicode_KIND(sub_obj); |
| 9154 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9155 | buf1 = PyUnicode_DATA(str_obj); |
| 9156 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9157 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9158 | if (!buf1) |
| 9159 | goto onError; |
| 9160 | buf2 = PyUnicode_DATA(sub_obj); |
| 9161 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9162 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 | if (!buf2) |
| 9164 | goto onError; |
| 9165 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9166 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9167 | |
| 9168 | ADJUST_INDICES(start, end, len1); |
| 9169 | switch(kind) { |
| 9170 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9171 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9172 | result = asciilib_count( |
| 9173 | ((Py_UCS1*)buf1) + start, end - start, |
| 9174 | buf2, len2, PY_SSIZE_T_MAX |
| 9175 | ); |
| 9176 | else |
| 9177 | result = ucs1lib_count( |
| 9178 | ((Py_UCS1*)buf1) + start, end - start, |
| 9179 | buf2, len2, PY_SSIZE_T_MAX |
| 9180 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9181 | break; |
| 9182 | case PyUnicode_2BYTE_KIND: |
| 9183 | result = ucs2lib_count( |
| 9184 | ((Py_UCS2*)buf1) + start, end - start, |
| 9185 | buf2, len2, PY_SSIZE_T_MAX |
| 9186 | ); |
| 9187 | break; |
| 9188 | case PyUnicode_4BYTE_KIND: |
| 9189 | result = ucs4lib_count( |
| 9190 | ((Py_UCS4*)buf1) + start, end - start, |
| 9191 | buf2, len2, PY_SSIZE_T_MAX |
| 9192 | ); |
| 9193 | break; |
| 9194 | default: |
| 9195 | assert(0); result = 0; |
| 9196 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9197 | |
| 9198 | Py_DECREF(sub_obj); |
| 9199 | Py_DECREF(str_obj); |
| 9200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | if (kind1 != kind) |
| 9202 | PyMem_Free(buf1); |
| 9203 | if (kind2 != kind) |
| 9204 | PyMem_Free(buf2); |
| 9205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9206 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9207 | onError: |
| 9208 | Py_DECREF(sub_obj); |
| 9209 | Py_DECREF(str_obj); |
| 9210 | if (kind1 != kind && buf1) |
| 9211 | PyMem_Free(buf1); |
| 9212 | if (kind2 != kind && buf2) |
| 9213 | PyMem_Free(buf2); |
| 9214 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9217 | Py_ssize_t |
| 9218 | PyUnicode_Find(PyObject *str, |
| 9219 | PyObject *sub, |
| 9220 | Py_ssize_t start, |
| 9221 | Py_ssize_t end, |
| 9222 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9223 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9224 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9226 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9227 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9228 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9229 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9230 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9231 | Py_DECREF(str); |
| 9232 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9233 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9234 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9235 | result = any_find_slice(direction, |
| 9236 | str, sub, start, end |
| 9237 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9240 | Py_DECREF(sub); |
| 9241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9242 | return result; |
| 9243 | } |
| 9244 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9245 | Py_ssize_t |
| 9246 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9247 | Py_ssize_t start, Py_ssize_t end, |
| 9248 | int direction) |
| 9249 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9250 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9251 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9252 | if (PyUnicode_READY(str) == -1) |
| 9253 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9254 | if (start < 0 || end < 0) { |
| 9255 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9256 | return -2; |
| 9257 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9259 | end = PyUnicode_GET_LENGTH(str); |
| 9260 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9261 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9262 | kind, end-start, ch, direction); |
| 9263 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9264 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9265 | else |
| 9266 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9267 | } |
| 9268 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9269 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9270 | tailmatch(PyObject *self, |
| 9271 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9272 | Py_ssize_t start, |
| 9273 | Py_ssize_t end, |
| 9274 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9275 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9276 | int kind_self; |
| 9277 | int kind_sub; |
| 9278 | void *data_self; |
| 9279 | void *data_sub; |
| 9280 | Py_ssize_t offset; |
| 9281 | Py_ssize_t i; |
| 9282 | Py_ssize_t end_sub; |
| 9283 | |
| 9284 | if (PyUnicode_READY(self) == -1 || |
| 9285 | PyUnicode_READY(substring) == -1) |
| 9286 | return 0; |
| 9287 | |
| 9288 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | return 1; |
| 9290 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9291 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9292 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9294 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9295 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9296 | kind_self = PyUnicode_KIND(self); |
| 9297 | data_self = PyUnicode_DATA(self); |
| 9298 | kind_sub = PyUnicode_KIND(substring); |
| 9299 | data_sub = PyUnicode_DATA(substring); |
| 9300 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9301 | |
| 9302 | if (direction > 0) |
| 9303 | offset = end; |
| 9304 | else |
| 9305 | offset = start; |
| 9306 | |
| 9307 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9308 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9309 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9310 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9311 | /* If both are of the same kind, memcmp is sufficient */ |
| 9312 | if (kind_self == kind_sub) { |
| 9313 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9314 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | data_sub, |
| 9316 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9317 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9318 | } |
| 9319 | /* otherwise we have to compare each character by first accesing it */ |
| 9320 | else { |
| 9321 | /* We do not need to compare 0 and len(substring)-1 because |
| 9322 | the if statement above ensured already that they are equal |
| 9323 | when we end up here. */ |
| 9324 | // TODO: honor direction and do a forward or backwards search |
| 9325 | for (i = 1; i < end_sub; ++i) { |
| 9326 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9327 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9328 | return 0; |
| 9329 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9330 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9332 | } |
| 9333 | |
| 9334 | return 0; |
| 9335 | } |
| 9336 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9337 | Py_ssize_t |
| 9338 | PyUnicode_Tailmatch(PyObject *str, |
| 9339 | PyObject *substr, |
| 9340 | Py_ssize_t start, |
| 9341 | Py_ssize_t end, |
| 9342 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9343 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9344 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9346 | str = PyUnicode_FromObject(str); |
| 9347 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9348 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9349 | substr = PyUnicode_FromObject(substr); |
| 9350 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9351 | Py_DECREF(str); |
| 9352 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9354 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9355 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9356 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9357 | Py_DECREF(str); |
| 9358 | Py_DECREF(substr); |
| 9359 | return result; |
| 9360 | } |
| 9361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9362 | /* Apply fixfct filter to the Unicode object self and return a |
| 9363 | reference to the modified object */ |
| 9364 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9365 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9366 | fixup(PyObject *self, |
| 9367 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9369 | PyObject *u; |
| 9370 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9371 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9372 | u = PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9374 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9375 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9377 | /* fix functions return the new maximum character in a string, |
| 9378 | if the kind of the resulting unicode object does not change, |
| 9379 | everything is fine. Otherwise we need to change the string kind |
| 9380 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9381 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | if (maxchar_new == 0) |
| 9383 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9384 | else if (maxchar_new <= 127) |
| 9385 | maxchar_new = 127; |
| 9386 | else if (maxchar_new <= 255) |
| 9387 | maxchar_new = 255; |
| 9388 | else if (maxchar_new <= 65535) |
| 9389 | maxchar_new = 65535; |
| 9390 | else |
| 9391 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9392 | |
| 9393 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9394 | /* fixfct should return TRUE if it modified the buffer. If |
| 9395 | FALSE, return a reference to the original buffer instead |
| 9396 | (to save space, not time) */ |
| 9397 | Py_INCREF(self); |
| 9398 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9399 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | else if (maxchar_new == maxchar_old) { |
| 9402 | return u; |
| 9403 | } |
| 9404 | else { |
| 9405 | /* In case the maximum character changed, we need to |
| 9406 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9407 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | if (v == NULL) { |
| 9409 | Py_DECREF(u); |
| 9410 | return NULL; |
| 9411 | } |
| 9412 | if (maxchar_new > maxchar_old) { |
| 9413 | /* If the maxchar increased so that the kind changed, not all |
| 9414 | characters are representable anymore and we need to fix the |
| 9415 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9416 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9417 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9419 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9420 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9421 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9422 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | |
| 9424 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9425 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9426 | return v; |
| 9427 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9428 | } |
| 9429 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9430 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9431 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9432 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9433 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9434 | called as a callback from fixup() which does it already. */ |
| 9435 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9436 | const int kind = PyUnicode_KIND(self); |
| 9437 | void *data = PyUnicode_DATA(self); |
| 9438 | int touched = 0; |
| 9439 | Py_UCS4 maxchar = 0; |
| 9440 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9442 | for (i = 0; i < len; ++i) { |
| 9443 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9444 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9445 | if (up != ch) { |
| 9446 | if (up > maxchar) |
| 9447 | maxchar = up; |
| 9448 | PyUnicode_WRITE(kind, data, i, up); |
| 9449 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9450 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9451 | else if (ch > maxchar) |
| 9452 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9453 | } |
| 9454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | if (touched) |
| 9456 | return maxchar; |
| 9457 | else |
| 9458 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9459 | } |
| 9460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9461 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9462 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9463 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9465 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9466 | const int kind = PyUnicode_KIND(self); |
| 9467 | void *data = PyUnicode_DATA(self); |
| 9468 | int touched = 0; |
| 9469 | Py_UCS4 maxchar = 0; |
| 9470 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | for(i = 0; i < len; ++i) { |
| 9473 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9474 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9475 | if (lo != ch) { |
| 9476 | if (lo > maxchar) |
| 9477 | maxchar = lo; |
| 9478 | PyUnicode_WRITE(kind, data, i, lo); |
| 9479 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9480 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9481 | else if (ch > maxchar) |
| 9482 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | } |
| 9484 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9485 | if (touched) |
| 9486 | return maxchar; |
| 9487 | else |
| 9488 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | } |
| 9490 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9492 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9495 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9496 | const int kind = PyUnicode_KIND(self); |
| 9497 | void *data = PyUnicode_DATA(self); |
| 9498 | int touched = 0; |
| 9499 | Py_UCS4 maxchar = 0; |
| 9500 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9501 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | for(i = 0; i < len; ++i) { |
| 9503 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9504 | Py_UCS4 nu = 0; |
| 9505 | |
| 9506 | if (Py_UNICODE_ISUPPER(ch)) |
| 9507 | nu = Py_UNICODE_TOLOWER(ch); |
| 9508 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9509 | nu = Py_UNICODE_TOUPPER(ch); |
| 9510 | |
| 9511 | if (nu != 0) { |
| 9512 | if (nu > maxchar) |
| 9513 | maxchar = nu; |
| 9514 | PyUnicode_WRITE(kind, data, i, nu); |
| 9515 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9516 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9517 | else if (ch > maxchar) |
| 9518 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9519 | } |
| 9520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9521 | if (touched) |
| 9522 | return maxchar; |
| 9523 | else |
| 9524 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | } |
| 9526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9527 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9528 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9529 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9531 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9532 | const int kind = PyUnicode_KIND(self); |
| 9533 | void *data = PyUnicode_DATA(self); |
| 9534 | int touched = 0; |
| 9535 | Py_UCS4 maxchar = 0; |
| 9536 | Py_ssize_t i = 0; |
| 9537 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9538 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9539 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9540 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9541 | |
| 9542 | ch = PyUnicode_READ(kind, data, i); |
| 9543 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9544 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9545 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9546 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9547 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9548 | ++i; |
| 9549 | for(; i < len; ++i) { |
| 9550 | ch = PyUnicode_READ(kind, data, i); |
| 9551 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9552 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9553 | if (lo > maxchar) |
| 9554 | maxchar = lo; |
| 9555 | PyUnicode_WRITE(kind, data, i, lo); |
| 9556 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9557 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9558 | else if (ch > maxchar) |
| 9559 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9560 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | |
| 9562 | if (touched) |
| 9563 | return maxchar; |
| 9564 | else |
| 9565 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9569 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9570 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9571 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9572 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9573 | const int kind = PyUnicode_KIND(self); |
| 9574 | void *data = PyUnicode_DATA(self); |
| 9575 | Py_UCS4 maxchar = 0; |
| 9576 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9577 | int previous_is_cased; |
| 9578 | |
| 9579 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9580 | if (len == 1) { |
| 9581 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9582 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9583 | if (ti != ch) { |
| 9584 | PyUnicode_WRITE(kind, data, i, ti); |
| 9585 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9586 | } |
| 9587 | else |
| 9588 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9589 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9591 | for(; i < len; ++i) { |
| 9592 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9593 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9595 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9596 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9597 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | nu = Py_UNICODE_TOTITLE(ch); |
| 9599 | |
| 9600 | if (nu > maxchar) |
| 9601 | maxchar = nu; |
| 9602 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9603 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | if (Py_UNICODE_ISLOWER(ch) || |
| 9605 | Py_UNICODE_ISUPPER(ch) || |
| 9606 | Py_UNICODE_ISTITLE(ch)) |
| 9607 | previous_is_cased = 1; |
| 9608 | else |
| 9609 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9610 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9611 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9612 | } |
| 9613 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9614 | PyObject * |
| 9615 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9616 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9617 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9618 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9619 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9620 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9621 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9622 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9623 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9624 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9625 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9627 | int use_memcpy; |
| 9628 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9629 | PyObject *last_obj; |
| 9630 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9632 | fseq = PySequence_Fast(seq, ""); |
| 9633 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9634 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9637 | /* NOTE: the following code can't call back into Python code, |
| 9638 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9639 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9640 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9641 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9642 | /* If empty sequence, return u"". */ |
| 9643 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9644 | Py_DECREF(fseq); |
| 9645 | Py_INCREF(unicode_empty); |
| 9646 | res = unicode_empty; |
| 9647 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9648 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9649 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9650 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9651 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9652 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9653 | if (seqlen == 1) { |
| 9654 | if (PyUnicode_CheckExact(items[0])) { |
| 9655 | res = items[0]; |
| 9656 | Py_INCREF(res); |
| 9657 | Py_DECREF(fseq); |
| 9658 | return res; |
| 9659 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9660 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9661 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9662 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9663 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9664 | /* Set up sep and seplen */ |
| 9665 | if (separator == NULL) { |
| 9666 | /* fall back to a blank space separator */ |
| 9667 | sep = PyUnicode_FromOrdinal(' '); |
| 9668 | if (!sep) |
| 9669 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9670 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9671 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9672 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9673 | else { |
| 9674 | if (!PyUnicode_Check(separator)) { |
| 9675 | PyErr_Format(PyExc_TypeError, |
| 9676 | "separator: expected str instance," |
| 9677 | " %.80s found", |
| 9678 | Py_TYPE(separator)->tp_name); |
| 9679 | goto onError; |
| 9680 | } |
| 9681 | if (PyUnicode_READY(separator)) |
| 9682 | goto onError; |
| 9683 | sep = separator; |
| 9684 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9685 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9686 | /* inc refcount to keep this code path symmetric with the |
| 9687 | above case of a blank separator */ |
| 9688 | Py_INCREF(sep); |
| 9689 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9690 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9693 | /* There are at least two things to join, or else we have a subclass |
| 9694 | * of str in the sequence. |
| 9695 | * Do a pre-pass to figure out the total amount of space we'll |
| 9696 | * need (sz), and see whether all argument are strings. |
| 9697 | */ |
| 9698 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9699 | #ifdef Py_DEBUG |
| 9700 | use_memcpy = 0; |
| 9701 | #else |
| 9702 | use_memcpy = 1; |
| 9703 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9704 | for (i = 0; i < seqlen; i++) { |
| 9705 | const Py_ssize_t old_sz = sz; |
| 9706 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9707 | if (!PyUnicode_Check(item)) { |
| 9708 | PyErr_Format(PyExc_TypeError, |
| 9709 | "sequence item %zd: expected str instance," |
| 9710 | " %.80s found", |
| 9711 | i, Py_TYPE(item)->tp_name); |
| 9712 | goto onError; |
| 9713 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | if (PyUnicode_READY(item) == -1) |
| 9715 | goto onError; |
| 9716 | sz += PyUnicode_GET_LENGTH(item); |
| 9717 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9718 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9719 | if (i != 0) |
| 9720 | sz += seplen; |
| 9721 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9722 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9723 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9724 | goto onError; |
| 9725 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9726 | if (use_memcpy && last_obj != NULL) { |
| 9727 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9728 | use_memcpy = 0; |
| 9729 | } |
| 9730 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9731 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9733 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9734 | if (res == NULL) |
| 9735 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9736 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9737 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9738 | #ifdef Py_DEBUG |
| 9739 | use_memcpy = 0; |
| 9740 | #else |
| 9741 | if (use_memcpy) { |
| 9742 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9743 | kind = PyUnicode_KIND(res); |
| 9744 | if (seplen != 0) |
| 9745 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9746 | } |
| 9747 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9748 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9749 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9750 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9751 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9752 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9753 | if (use_memcpy) { |
| 9754 | Py_MEMCPY(res_data, |
| 9755 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9756 | kind * seplen); |
| 9757 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9758 | } |
| 9759 | else { |
| 9760 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9761 | res_offset += seplen; |
| 9762 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9763 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9764 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9765 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9766 | if (use_memcpy) { |
| 9767 | Py_MEMCPY(res_data, |
| 9768 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9769 | kind * itemlen); |
| 9770 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9771 | } |
| 9772 | else { |
| 9773 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9774 | res_offset += itemlen; |
| 9775 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9776 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9777 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9778 | if (use_memcpy) |
| 9779 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9780 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9781 | else |
| 9782 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9783 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9784 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9785 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9786 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9788 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9789 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9790 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9791 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9792 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9793 | return NULL; |
| 9794 | } |
| 9795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9796 | #define FILL(kind, data, value, start, length) \ |
| 9797 | do { \ |
| 9798 | Py_ssize_t i_ = 0; \ |
| 9799 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9800 | switch ((kind)) { \ |
| 9801 | case PyUnicode_1BYTE_KIND: { \ |
| 9802 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9803 | memset(to_, (unsigned char)value, length); \ |
| 9804 | break; \ |
| 9805 | } \ |
| 9806 | case PyUnicode_2BYTE_KIND: { \ |
| 9807 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9808 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9809 | break; \ |
| 9810 | } \ |
| 9811 | default: { \ |
| 9812 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9813 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9814 | break; \ |
| 9815 | } \ |
| 9816 | } \ |
| 9817 | } while (0) |
| 9818 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9819 | static PyObject * |
| 9820 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9821 | Py_ssize_t left, |
| 9822 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9823 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9824 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 | PyObject *u; |
| 9826 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9827 | int kind; |
| 9828 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9829 | |
| 9830 | if (left < 0) |
| 9831 | left = 0; |
| 9832 | if (right < 0) |
| 9833 | right = 0; |
| 9834 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9835 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9836 | Py_INCREF(self); |
| 9837 | return self; |
| 9838 | } |
| 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); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9871 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | switch(PyUnicode_KIND(string)) { |
| 9874 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9875 | if (PyUnicode_IS_ASCII(string)) |
| 9876 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9877 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9878 | PyUnicode_GET_LENGTH(string), keepends); |
| 9879 | else |
| 9880 | list = ucs1lib_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); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9883 | break; |
| 9884 | case PyUnicode_2BYTE_KIND: |
| 9885 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9886 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | PyUnicode_GET_LENGTH(string), keepends); |
| 9888 | break; |
| 9889 | case PyUnicode_4BYTE_KIND: |
| 9890 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9891 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | PyUnicode_GET_LENGTH(string), keepends); |
| 9893 | break; |
| 9894 | default: |
| 9895 | assert(0); |
| 9896 | list = 0; |
| 9897 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9898 | Py_DECREF(string); |
| 9899 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9900 | } |
| 9901 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9902 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9903 | split(PyObject *self, |
| 9904 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9905 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9906 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9907 | int kind1, kind2, kind; |
| 9908 | void *buf1, *buf2; |
| 9909 | Py_ssize_t len1, len2; |
| 9910 | PyObject* out; |
| 9911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9912 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9913 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | if (PyUnicode_READY(self) == -1) |
| 9916 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9918 | if (substring == NULL) |
| 9919 | switch(PyUnicode_KIND(self)) { |
| 9920 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9921 | if (PyUnicode_IS_ASCII(self)) |
| 9922 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9923 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9924 | PyUnicode_GET_LENGTH(self), maxcount |
| 9925 | ); |
| 9926 | else |
| 9927 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9928 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9929 | PyUnicode_GET_LENGTH(self), maxcount |
| 9930 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | case PyUnicode_2BYTE_KIND: |
| 9932 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9933 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | PyUnicode_GET_LENGTH(self), maxcount |
| 9935 | ); |
| 9936 | case PyUnicode_4BYTE_KIND: |
| 9937 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9938 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | PyUnicode_GET_LENGTH(self), maxcount |
| 9940 | ); |
| 9941 | default: |
| 9942 | assert(0); |
| 9943 | return NULL; |
| 9944 | } |
| 9945 | |
| 9946 | if (PyUnicode_READY(substring) == -1) |
| 9947 | return NULL; |
| 9948 | |
| 9949 | kind1 = PyUnicode_KIND(self); |
| 9950 | kind2 = PyUnicode_KIND(substring); |
| 9951 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9952 | buf1 = PyUnicode_DATA(self); |
| 9953 | buf2 = PyUnicode_DATA(substring); |
| 9954 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9955 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9956 | if (!buf1) |
| 9957 | return NULL; |
| 9958 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9959 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9960 | if (!buf2) { |
| 9961 | if (kind1 != kind) PyMem_Free(buf1); |
| 9962 | return NULL; |
| 9963 | } |
| 9964 | len1 = PyUnicode_GET_LENGTH(self); |
| 9965 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9966 | |
| 9967 | switch(kind) { |
| 9968 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9969 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9970 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9971 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9972 | else |
| 9973 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9974 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9975 | break; |
| 9976 | case PyUnicode_2BYTE_KIND: |
| 9977 | out = ucs2lib_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_4BYTE_KIND: |
| 9981 | out = ucs4lib_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 | default: |
| 9985 | out = NULL; |
| 9986 | } |
| 9987 | if (kind1 != kind) |
| 9988 | PyMem_Free(buf1); |
| 9989 | if (kind2 != kind) |
| 9990 | PyMem_Free(buf2); |
| 9991 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9992 | } |
| 9993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9994 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9995 | rsplit(PyObject *self, |
| 9996 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9997 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | int kind1, kind2, kind; |
| 10000 | void *buf1, *buf2; |
| 10001 | Py_ssize_t len1, len2; |
| 10002 | PyObject* out; |
| 10003 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10004 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10005 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10006 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | if (PyUnicode_READY(self) == -1) |
| 10008 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10009 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10010 | if (substring == NULL) |
| 10011 | switch(PyUnicode_KIND(self)) { |
| 10012 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10013 | if (PyUnicode_IS_ASCII(self)) |
| 10014 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10015 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10016 | PyUnicode_GET_LENGTH(self), maxcount |
| 10017 | ); |
| 10018 | else |
| 10019 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10020 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10021 | PyUnicode_GET_LENGTH(self), maxcount |
| 10022 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | case PyUnicode_2BYTE_KIND: |
| 10024 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10025 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10026 | PyUnicode_GET_LENGTH(self), maxcount |
| 10027 | ); |
| 10028 | case PyUnicode_4BYTE_KIND: |
| 10029 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10030 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10031 | PyUnicode_GET_LENGTH(self), maxcount |
| 10032 | ); |
| 10033 | default: |
| 10034 | assert(0); |
| 10035 | return NULL; |
| 10036 | } |
| 10037 | |
| 10038 | if (PyUnicode_READY(substring) == -1) |
| 10039 | return NULL; |
| 10040 | |
| 10041 | kind1 = PyUnicode_KIND(self); |
| 10042 | kind2 = PyUnicode_KIND(substring); |
| 10043 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10044 | buf1 = PyUnicode_DATA(self); |
| 10045 | buf2 = PyUnicode_DATA(substring); |
| 10046 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10047 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10048 | if (!buf1) |
| 10049 | return NULL; |
| 10050 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10051 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | if (!buf2) { |
| 10053 | if (kind1 != kind) PyMem_Free(buf1); |
| 10054 | return NULL; |
| 10055 | } |
| 10056 | len1 = PyUnicode_GET_LENGTH(self); |
| 10057 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10058 | |
| 10059 | switch(kind) { |
| 10060 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10061 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10062 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10063 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10064 | else |
| 10065 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10066 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | break; |
| 10068 | case PyUnicode_2BYTE_KIND: |
| 10069 | out = ucs2lib_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_4BYTE_KIND: |
| 10073 | out = ucs4lib_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 | default: |
| 10077 | out = NULL; |
| 10078 | } |
| 10079 | if (kind1 != kind) |
| 10080 | PyMem_Free(buf1); |
| 10081 | if (kind2 != kind) |
| 10082 | PyMem_Free(buf2); |
| 10083 | return out; |
| 10084 | } |
| 10085 | |
| 10086 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10087 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10088 | 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] | 10089 | { |
| 10090 | switch(kind) { |
| 10091 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10092 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10093 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10094 | else |
| 10095 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10096 | case PyUnicode_2BYTE_KIND: |
| 10097 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10098 | case PyUnicode_4BYTE_KIND: |
| 10099 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10100 | } |
| 10101 | assert(0); |
| 10102 | return -1; |
| 10103 | } |
| 10104 | |
| 10105 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10106 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10107 | 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] | 10108 | { |
| 10109 | switch(kind) { |
| 10110 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10111 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10112 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10113 | else |
| 10114 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | case PyUnicode_2BYTE_KIND: |
| 10116 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10117 | case PyUnicode_4BYTE_KIND: |
| 10118 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10119 | } |
| 10120 | assert(0); |
| 10121 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10122 | } |
| 10123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10124 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | replace(PyObject *self, PyObject *str1, |
| 10126 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10127 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | PyObject *u; |
| 10129 | char *sbuf = PyUnicode_DATA(self); |
| 10130 | char *buf1 = PyUnicode_DATA(str1); |
| 10131 | char *buf2 = PyUnicode_DATA(str2); |
| 10132 | int srelease = 0, release1 = 0, release2 = 0; |
| 10133 | int skind = PyUnicode_KIND(self); |
| 10134 | int kind1 = PyUnicode_KIND(str1); |
| 10135 | int kind2 = PyUnicode_KIND(str2); |
| 10136 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10137 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10138 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10139 | int mayshrink; |
| 10140 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | |
| 10142 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10143 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10144 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10145 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10146 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10147 | if (str1 == str2) |
| 10148 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 | if (skind < kind1) |
| 10150 | /* substring too wide to be present */ |
| 10151 | goto nothing; |
| 10152 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10153 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10154 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10155 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10156 | result string. */ |
| 10157 | mayshrink = (maxchar_str2 < maxchar); |
| 10158 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10159 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10161 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10162 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10164 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10166 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10167 | Py_UCS4 u1, u2; |
| 10168 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10169 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10170 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10171 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10172 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10173 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10175 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10176 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10177 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | rkind = PyUnicode_KIND(u); |
| 10179 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10180 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10181 | if (--maxcount < 0) |
| 10182 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10183 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10184 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10185 | } |
| 10186 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10187 | int rkind = skind; |
| 10188 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10189 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | if (kind1 < rkind) { |
| 10191 | /* widen substring */ |
| 10192 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10193 | if (!buf1) goto error; |
| 10194 | release1 = 1; |
| 10195 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10196 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10197 | if (i < 0) |
| 10198 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | if (rkind > kind2) { |
| 10200 | /* widen replacement */ |
| 10201 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10202 | if (!buf2) goto error; |
| 10203 | release2 = 1; |
| 10204 | } |
| 10205 | else if (rkind < kind2) { |
| 10206 | /* widen self and buf1 */ |
| 10207 | rkind = kind2; |
| 10208 | if (release1) PyMem_Free(buf1); |
| 10209 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10210 | if (!sbuf) goto error; |
| 10211 | srelease = 1; |
| 10212 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10213 | if (!buf1) goto error; |
| 10214 | release1 = 1; |
| 10215 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10216 | u = PyUnicode_New(slen, maxchar); |
| 10217 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10219 | assert(PyUnicode_KIND(u) == rkind); |
| 10220 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10221 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10222 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10223 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10224 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10226 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10228 | |
| 10229 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10230 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10231 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10232 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10233 | if (i == -1) |
| 10234 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10235 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10237 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10239 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10240 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10241 | } |
| 10242 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | Py_ssize_t n, i, j, ires; |
| 10244 | Py_ssize_t product, new_size; |
| 10245 | int rkind = skind; |
| 10246 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10247 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10249 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10251 | if (!buf1) goto error; |
| 10252 | release1 = 1; |
| 10253 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10254 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10255 | if (n == 0) |
| 10256 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10257 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10258 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10260 | if (!buf2) goto error; |
| 10261 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10262 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10264 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10265 | rkind = kind2; |
| 10266 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10267 | if (!sbuf) goto error; |
| 10268 | srelease = 1; |
| 10269 | if (release1) PyMem_Free(buf1); |
| 10270 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10271 | if (!buf1) goto error; |
| 10272 | release1 = 1; |
| 10273 | } |
| 10274 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10275 | PyUnicode_GET_LENGTH(str1))); */ |
| 10276 | product = n * (len2-len1); |
| 10277 | if ((product / (len2-len1)) != n) { |
| 10278 | PyErr_SetString(PyExc_OverflowError, |
| 10279 | "replace string is too long"); |
| 10280 | goto error; |
| 10281 | } |
| 10282 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10283 | if (new_size == 0) { |
| 10284 | Py_INCREF(unicode_empty); |
| 10285 | u = unicode_empty; |
| 10286 | goto done; |
| 10287 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10289 | PyErr_SetString(PyExc_OverflowError, |
| 10290 | "replace string is too long"); |
| 10291 | goto error; |
| 10292 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10293 | u = PyUnicode_New(new_size, maxchar); |
| 10294 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10296 | assert(PyUnicode_KIND(u) == rkind); |
| 10297 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | ires = i = 0; |
| 10299 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10300 | while (n-- > 0) { |
| 10301 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10302 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10303 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10304 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10305 | if (j == -1) |
| 10306 | break; |
| 10307 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10308 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10309 | memcpy(res + rkind * ires, |
| 10310 | sbuf + rkind * i, |
| 10311 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10312 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10313 | } |
| 10314 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10316 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10318 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10320 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10321 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10322 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10323 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10324 | /* copy tail [i:] */ |
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 * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10328 | } |
| 10329 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10330 | /* interleave */ |
| 10331 | while (n > 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 | if (--n <= 0) |
| 10337 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10338 | memcpy(res + rkind * ires, |
| 10339 | sbuf + rkind * i, |
| 10340 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10341 | ires++; |
| 10342 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10343 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10344 | memcpy(res + rkind * ires, |
| 10345 | sbuf + rkind * i, |
| 10346 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10347 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10351 | unicode_adjust_maxchar(&u); |
| 10352 | if (u == NULL) |
| 10353 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10354 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10355 | |
| 10356 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | if (srelease) |
| 10358 | PyMem_FREE(sbuf); |
| 10359 | if (release1) |
| 10360 | PyMem_FREE(buf1); |
| 10361 | if (release2) |
| 10362 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10363 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10365 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10366 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10367 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | if (srelease) |
| 10369 | PyMem_FREE(sbuf); |
| 10370 | if (release1) |
| 10371 | PyMem_FREE(buf1); |
| 10372 | if (release2) |
| 10373 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10374 | if (PyUnicode_CheckExact(self)) { |
| 10375 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10376 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10377 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10378 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | error: |
| 10380 | if (srelease && sbuf) |
| 10381 | PyMem_FREE(sbuf); |
| 10382 | if (release1 && buf1) |
| 10383 | PyMem_FREE(buf1); |
| 10384 | if (release2 && buf2) |
| 10385 | PyMem_FREE(buf2); |
| 10386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | } |
| 10388 | |
| 10389 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10390 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10391 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10392 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | \n\ |
| 10394 | 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] | 10395 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10396 | |
| 10397 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10398 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10399 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | return fixup(self, fixtitle); |
| 10401 | } |
| 10402 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10403 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10404 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10405 | \n\ |
| 10406 | 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] | 10407 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10408 | |
| 10409 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10410 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10411 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10412 | return fixup(self, fixcapitalize); |
| 10413 | } |
| 10414 | |
| 10415 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10416 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10417 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10418 | \n\ |
| 10419 | 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] | 10420 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | |
| 10422 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10423 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10424 | { |
| 10425 | PyObject *list; |
| 10426 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10427 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | /* Split into words */ |
| 10430 | list = split(self, NULL, -1); |
| 10431 | if (!list) |
| 10432 | return NULL; |
| 10433 | |
| 10434 | /* Capitalize each word */ |
| 10435 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10436 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10437 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10438 | if (item == NULL) |
| 10439 | goto onError; |
| 10440 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10441 | PyList_SET_ITEM(list, i, item); |
| 10442 | } |
| 10443 | |
| 10444 | /* Join the words to form a new string */ |
| 10445 | item = PyUnicode_Join(NULL, list); |
| 10446 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10447 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10448 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10449 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | } |
| 10451 | #endif |
| 10452 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10453 | /* Argument converter. Coerces to a single unicode character */ |
| 10454 | |
| 10455 | static int |
| 10456 | convert_uc(PyObject *obj, void *addr) |
| 10457 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10458 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10459 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10460 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10461 | uniobj = PyUnicode_FromObject(obj); |
| 10462 | if (uniobj == NULL) { |
| 10463 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10464 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10465 | return 0; |
| 10466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10468 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10469 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10470 | Py_DECREF(uniobj); |
| 10471 | return 0; |
| 10472 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10474 | Py_DECREF(uniobj); |
| 10475 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10476 | } |
| 10477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10478 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10479 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10480 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10481 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10482 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10483 | |
| 10484 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10485 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10486 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10487 | Py_ssize_t marg, left; |
| 10488 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | Py_UCS4 fillchar = ' '; |
| 10490 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10491 | 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] | 10492 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10494 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10495 | return NULL; |
| 10496 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10499 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10500 | } |
| 10501 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10502 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10503 | left = marg / 2 + (marg & width & 1); |
| 10504 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10505 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10508 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10509 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10510 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10511 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10512 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10513 | int kind1, kind2; |
| 10514 | void *data1, *data2; |
| 10515 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10517 | kind1 = PyUnicode_KIND(str1); |
| 10518 | kind2 = PyUnicode_KIND(str2); |
| 10519 | data1 = PyUnicode_DATA(str1); |
| 10520 | data2 = PyUnicode_DATA(str2); |
| 10521 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10522 | len2 = PyUnicode_GET_LENGTH(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 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10525 | Py_UCS4 c1, c2; |
| 10526 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10527 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10528 | |
| 10529 | if (c1 != c2) |
| 10530 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10531 | } |
| 10532 | |
| 10533 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10534 | } |
| 10535 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10536 | int |
| 10537 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10538 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10539 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10540 | if (PyUnicode_READY(left) == -1 || |
| 10541 | PyUnicode_READY(right) == -1) |
| 10542 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10543 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10545 | PyErr_Format(PyExc_TypeError, |
| 10546 | "Can't compare %.100s and %.100s", |
| 10547 | left->ob_type->tp_name, |
| 10548 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | return -1; |
| 10550 | } |
| 10551 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10552 | int |
| 10553 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10554 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | Py_ssize_t i; |
| 10556 | int kind; |
| 10557 | void *data; |
| 10558 | Py_UCS4 chr; |
| 10559 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10560 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10561 | if (PyUnicode_READY(uni) == -1) |
| 10562 | return -1; |
| 10563 | kind = PyUnicode_KIND(uni); |
| 10564 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10565 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10567 | if (chr != str[i]) |
| 10568 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10569 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10570 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10572 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10573 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10574 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10575 | return 0; |
| 10576 | } |
| 10577 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10579 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10580 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10581 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10582 | PyObject * |
| 10583 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10584 | { |
| 10585 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10586 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10587 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10588 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | if (PyUnicode_READY(left) == -1 || |
| 10590 | PyUnicode_READY(right) == -1) |
| 10591 | return NULL; |
| 10592 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10593 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10594 | if (op == Py_EQ) { |
| 10595 | Py_INCREF(Py_False); |
| 10596 | return Py_False; |
| 10597 | } |
| 10598 | if (op == Py_NE) { |
| 10599 | Py_INCREF(Py_True); |
| 10600 | return Py_True; |
| 10601 | } |
| 10602 | } |
| 10603 | if (left == right) |
| 10604 | result = 0; |
| 10605 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10606 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10607 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10608 | /* Convert the return value to a Boolean */ |
| 10609 | switch (op) { |
| 10610 | case Py_EQ: |
| 10611 | v = TEST_COND(result == 0); |
| 10612 | break; |
| 10613 | case Py_NE: |
| 10614 | v = TEST_COND(result != 0); |
| 10615 | break; |
| 10616 | case Py_LE: |
| 10617 | v = TEST_COND(result <= 0); |
| 10618 | break; |
| 10619 | case Py_GE: |
| 10620 | v = TEST_COND(result >= 0); |
| 10621 | break; |
| 10622 | case Py_LT: |
| 10623 | v = TEST_COND(result == -1); |
| 10624 | break; |
| 10625 | case Py_GT: |
| 10626 | v = TEST_COND(result == 1); |
| 10627 | break; |
| 10628 | default: |
| 10629 | PyErr_BadArgument(); |
| 10630 | return NULL; |
| 10631 | } |
| 10632 | Py_INCREF(v); |
| 10633 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10634 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10635 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10636 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10637 | } |
| 10638 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10639 | int |
| 10640 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10641 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10642 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | int kind1, kind2, kind; |
| 10644 | void *buf1, *buf2; |
| 10645 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10646 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10647 | |
| 10648 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10649 | sub = PyUnicode_FromObject(element); |
| 10650 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10651 | PyErr_Format(PyExc_TypeError, |
| 10652 | "'in <string>' requires string as left operand, not %s", |
| 10653 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10655 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10656 | if (PyUnicode_READY(sub) == -1) |
| 10657 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10658 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10659 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10660 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10661 | Py_DECREF(sub); |
| 10662 | return -1; |
| 10663 | } |
| 10664 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | kind1 = PyUnicode_KIND(str); |
| 10666 | kind2 = PyUnicode_KIND(sub); |
| 10667 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10668 | buf1 = PyUnicode_DATA(str); |
| 10669 | buf2 = PyUnicode_DATA(sub); |
| 10670 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10671 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10672 | if (!buf1) { |
| 10673 | Py_DECREF(sub); |
| 10674 | return -1; |
| 10675 | } |
| 10676 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10677 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10678 | if (!buf2) { |
| 10679 | Py_DECREF(sub); |
| 10680 | if (kind1 != kind) PyMem_Free(buf1); |
| 10681 | return -1; |
| 10682 | } |
| 10683 | len1 = PyUnicode_GET_LENGTH(str); |
| 10684 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10685 | |
| 10686 | switch(kind) { |
| 10687 | case PyUnicode_1BYTE_KIND: |
| 10688 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10689 | break; |
| 10690 | case PyUnicode_2BYTE_KIND: |
| 10691 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10692 | break; |
| 10693 | case PyUnicode_4BYTE_KIND: |
| 10694 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10695 | break; |
| 10696 | default: |
| 10697 | result = -1; |
| 10698 | assert(0); |
| 10699 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10700 | |
| 10701 | Py_DECREF(str); |
| 10702 | Py_DECREF(sub); |
| 10703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10704 | if (kind1 != kind) |
| 10705 | PyMem_Free(buf1); |
| 10706 | if (kind2 != kind) |
| 10707 | PyMem_Free(buf2); |
| 10708 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10709 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10713 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10714 | PyObject * |
| 10715 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10718 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | |
| 10720 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10722 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10723 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10724 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10726 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 | |
| 10728 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10729 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10730 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10731 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10733 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10734 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10735 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 | } |
| 10737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10739 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10740 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10743 | w = PyUnicode_New( |
| 10744 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10745 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10747 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10748 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10749 | copy_characters(w, PyUnicode_GET_LENGTH(u), v, 0, PyUnicode_GET_LENGTH(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 | Py_DECREF(u); |
| 10751 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10752 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10753 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10754 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10755 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 | Py_XDECREF(u); |
| 10757 | Py_XDECREF(v); |
| 10758 | return NULL; |
| 10759 | } |
| 10760 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10761 | static void |
| 10762 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10763 | { |
| 10764 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10765 | |
| 10766 | assert(PyUnicode_IS_READY(*p_left)); |
| 10767 | assert(PyUnicode_IS_READY(right)); |
| 10768 | |
| 10769 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10770 | right_len = PyUnicode_GET_LENGTH(right); |
| 10771 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10772 | PyErr_SetString(PyExc_OverflowError, |
| 10773 | "strings are too large to concat"); |
| 10774 | goto error; |
| 10775 | } |
| 10776 | new_len = left_len + right_len; |
| 10777 | |
| 10778 | /* Now we own the last reference to 'left', so we can resize it |
| 10779 | * in-place. |
| 10780 | */ |
| 10781 | if (unicode_resize(p_left, new_len) != 0) { |
| 10782 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10783 | * deallocated so it cannot be put back into |
| 10784 | * 'variable'. The MemoryError is raised when there |
| 10785 | * is no value in 'variable', which might (very |
| 10786 | * remotely) be a cause of incompatibilities. |
| 10787 | */ |
| 10788 | goto error; |
| 10789 | } |
| 10790 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10791 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10792 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10793 | return; |
| 10794 | |
| 10795 | error: |
| 10796 | Py_DECREF(*p_left); |
| 10797 | *p_left = NULL; |
| 10798 | } |
| 10799 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10800 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10801 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10802 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10803 | PyObject *left, *res; |
| 10804 | |
| 10805 | if (p_left == NULL) { |
| 10806 | if (!PyErr_Occurred()) |
| 10807 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10808 | return; |
| 10809 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10810 | left = *p_left; |
| 10811 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10812 | if (!PyErr_Occurred()) |
| 10813 | PyErr_BadInternalCall(); |
| 10814 | goto error; |
| 10815 | } |
| 10816 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10817 | if (PyUnicode_READY(left)) |
| 10818 | goto error; |
| 10819 | if (PyUnicode_READY(right)) |
| 10820 | goto error; |
| 10821 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10822 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10823 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10824 | && unicode_resizable(left) |
| 10825 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10826 | || _PyUnicode_WSTR(left) != NULL)) |
| 10827 | { |
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. */ |
| 10832 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10833 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10834 | unicode_append_inplace(p_left, right); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10835 | assert(p_left == NULL || _PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10836 | return; |
| 10837 | } |
| 10838 | } |
| 10839 | |
| 10840 | res = PyUnicode_Concat(left, right); |
| 10841 | if (res == NULL) |
| 10842 | goto error; |
| 10843 | Py_DECREF(left); |
| 10844 | *p_left = res; |
| 10845 | return; |
| 10846 | |
| 10847 | error: |
| 10848 | Py_DECREF(*p_left); |
| 10849 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10850 | } |
| 10851 | |
| 10852 | void |
| 10853 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10854 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10855 | PyUnicode_Append(pleft, right); |
| 10856 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10857 | } |
| 10858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10859 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10860 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10862 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10863 | 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] | 10864 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | |
| 10866 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10867 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10869 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10870 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10871 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10872 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | int kind1, kind2, kind; |
| 10874 | void *buf1, *buf2; |
| 10875 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10876 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10877 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10878 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10879 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10881 | kind1 = PyUnicode_KIND(self); |
| 10882 | kind2 = PyUnicode_KIND(substring); |
| 10883 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10884 | buf1 = PyUnicode_DATA(self); |
| 10885 | buf2 = PyUnicode_DATA(substring); |
| 10886 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10887 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10888 | if (!buf1) { |
| 10889 | Py_DECREF(substring); |
| 10890 | return NULL; |
| 10891 | } |
| 10892 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10893 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10894 | if (!buf2) { |
| 10895 | Py_DECREF(substring); |
| 10896 | if (kind1 != kind) PyMem_Free(buf1); |
| 10897 | return NULL; |
| 10898 | } |
| 10899 | len1 = PyUnicode_GET_LENGTH(self); |
| 10900 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10901 | |
| 10902 | ADJUST_INDICES(start, end, len1); |
| 10903 | switch(kind) { |
| 10904 | case PyUnicode_1BYTE_KIND: |
| 10905 | iresult = ucs1lib_count( |
| 10906 | ((Py_UCS1*)buf1) + start, end - start, |
| 10907 | buf2, len2, PY_SSIZE_T_MAX |
| 10908 | ); |
| 10909 | break; |
| 10910 | case PyUnicode_2BYTE_KIND: |
| 10911 | iresult = ucs2lib_count( |
| 10912 | ((Py_UCS2*)buf1) + start, end - start, |
| 10913 | buf2, len2, PY_SSIZE_T_MAX |
| 10914 | ); |
| 10915 | break; |
| 10916 | case PyUnicode_4BYTE_KIND: |
| 10917 | iresult = ucs4lib_count( |
| 10918 | ((Py_UCS4*)buf1) + start, end - start, |
| 10919 | buf2, len2, PY_SSIZE_T_MAX |
| 10920 | ); |
| 10921 | break; |
| 10922 | default: |
| 10923 | assert(0); iresult = 0; |
| 10924 | } |
| 10925 | |
| 10926 | result = PyLong_FromSsize_t(iresult); |
| 10927 | |
| 10928 | if (kind1 != kind) |
| 10929 | PyMem_Free(buf1); |
| 10930 | if (kind2 != kind) |
| 10931 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10932 | |
| 10933 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | return result; |
| 10936 | } |
| 10937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10938 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10939 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10941 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10942 | 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] | 10943 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10944 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10945 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10946 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | |
| 10948 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10949 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10951 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | char *encoding = NULL; |
| 10953 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10954 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10955 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10956 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10957 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10958 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10959 | } |
| 10960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10961 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10962 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | \n\ |
| 10964 | 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] | 10965 | 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] | 10966 | |
| 10967 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10968 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10969 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10970 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10971 | Py_UCS4 ch; |
| 10972 | PyObject *u; |
| 10973 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10975 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10976 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | |
| 10978 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10979 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10980 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10981 | if (PyUnicode_READY(self) == -1) |
| 10982 | return NULL; |
| 10983 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10984 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10985 | src_len = PyUnicode_GET_LENGTH(self); |
| 10986 | i = j = line_pos = 0; |
| 10987 | kind = PyUnicode_KIND(self); |
| 10988 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10989 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10990 | for (; i < src_len; i++) { |
| 10991 | ch = PyUnicode_READ(kind, src_data, i); |
| 10992 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10993 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10994 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10995 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10996 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10997 | goto overflow; |
| 10998 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10999 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11000 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11001 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11003 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11004 | goto overflow; |
| 11005 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11007 | if (ch == '\n' || ch == '\r') |
| 11008 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11010 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11011 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11012 | Py_INCREF(self); |
| 11013 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11014 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11016 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11017 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11018 | if (!u) |
| 11019 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11020 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11022 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11024 | for (; i < src_len; i++) { |
| 11025 | ch = PyUnicode_READ(kind, src_data, i); |
| 11026 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11027 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11028 | incr = tabsize - (line_pos % tabsize); |
| 11029 | line_pos += incr; |
| 11030 | while (incr--) { |
| 11031 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11032 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11033 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11034 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11035 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11036 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11037 | line_pos++; |
| 11038 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11039 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11040 | if (ch == '\n' || ch == '\r') |
| 11041 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11043 | } |
| 11044 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11045 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11046 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11047 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11048 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11049 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | } |
| 11051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11052 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11053 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11054 | \n\ |
| 11055 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11056 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | arguments start and end are interpreted as in slice notation.\n\ |
| 11058 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11059 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | |
| 11061 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11064 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11065 | Py_ssize_t start; |
| 11066 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11067 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11069 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11070 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | if (PyUnicode_READY(self) == -1) |
| 11074 | return NULL; |
| 11075 | if (PyUnicode_READY(substring) == -1) |
| 11076 | return NULL; |
| 11077 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11078 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | |
| 11080 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11081 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11082 | if (result == -2) |
| 11083 | return NULL; |
| 11084 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11085 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11086 | } |
| 11087 | |
| 11088 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11089 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11090 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11091 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11092 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11094 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | } |
| 11096 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11097 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11098 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11099 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11100 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11102 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11103 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11105 | if (_PyUnicode_HASH(self) != -1) |
| 11106 | return _PyUnicode_HASH(self); |
| 11107 | if (PyUnicode_READY(self) == -1) |
| 11108 | return -1; |
| 11109 | len = PyUnicode_GET_LENGTH(self); |
| 11110 | |
| 11111 | /* The hash function as a macro, gets expanded three times below. */ |
| 11112 | #define HASH(P) \ |
| 11113 | x = (Py_uhash_t)*P << 7; \ |
| 11114 | while (--len >= 0) \ |
| 11115 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11116 | |
| 11117 | switch (PyUnicode_KIND(self)) { |
| 11118 | case PyUnicode_1BYTE_KIND: { |
| 11119 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11120 | HASH(c); |
| 11121 | break; |
| 11122 | } |
| 11123 | case PyUnicode_2BYTE_KIND: { |
| 11124 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11125 | HASH(s); |
| 11126 | break; |
| 11127 | } |
| 11128 | default: { |
| 11129 | Py_UCS4 *l; |
| 11130 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11131 | "Impossible switch case in unicode_hash"); |
| 11132 | l = PyUnicode_4BYTE_DATA(self); |
| 11133 | HASH(l); |
| 11134 | break; |
| 11135 | } |
| 11136 | } |
| 11137 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11138 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11139 | if (x == -1) |
| 11140 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11141 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11142 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11144 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11146 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11147 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11149 | 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] | 11150 | |
| 11151 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11152 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11154 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11155 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11156 | Py_ssize_t start; |
| 11157 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11159 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11160 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | if (PyUnicode_READY(self) == -1) |
| 11164 | return NULL; |
| 11165 | if (PyUnicode_READY(substring) == -1) |
| 11166 | return NULL; |
| 11167 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11168 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | |
| 11170 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | if (result == -2) |
| 11173 | return NULL; |
| 11174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | if (result < 0) { |
| 11176 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11177 | return NULL; |
| 11178 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11179 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11180 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | } |
| 11182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11183 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11184 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11186 | 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] | 11187 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | |
| 11189 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11190 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11192 | Py_ssize_t i, length; |
| 11193 | int kind; |
| 11194 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | int cased; |
| 11196 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11197 | if (PyUnicode_READY(self) == -1) |
| 11198 | return NULL; |
| 11199 | length = PyUnicode_GET_LENGTH(self); |
| 11200 | kind = PyUnicode_KIND(self); |
| 11201 | data = PyUnicode_DATA(self); |
| 11202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11204 | if (length == 1) |
| 11205 | return PyBool_FromLong( |
| 11206 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11208 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11209 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11210 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11213 | for (i = 0; i < length; i++) { |
| 11214 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11215 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11216 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11217 | return PyBool_FromLong(0); |
| 11218 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11219 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11220 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11221 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | } |
| 11223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11224 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11225 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11226 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11227 | 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] | 11228 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11229 | |
| 11230 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11231 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11232 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11233 | Py_ssize_t i, length; |
| 11234 | int kind; |
| 11235 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | int cased; |
| 11237 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11238 | if (PyUnicode_READY(self) == -1) |
| 11239 | return NULL; |
| 11240 | length = PyUnicode_GET_LENGTH(self); |
| 11241 | kind = PyUnicode_KIND(self); |
| 11242 | data = PyUnicode_DATA(self); |
| 11243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11245 | if (length == 1) |
| 11246 | return PyBool_FromLong( |
| 11247 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11248 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11249 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11250 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11251 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11254 | for (i = 0; i < length; i++) { |
| 11255 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11256 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11257 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11258 | return PyBool_FromLong(0); |
| 11259 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11260 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11262 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | } |
| 11264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11265 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11268 | Return True if S is a titlecased string and there is at least one\n\ |
| 11269 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11270 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11271 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | |
| 11273 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11274 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11276 | Py_ssize_t i, length; |
| 11277 | int kind; |
| 11278 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | int cased, previous_is_cased; |
| 11280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | if (PyUnicode_READY(self) == -1) |
| 11282 | return NULL; |
| 11283 | length = PyUnicode_GET_LENGTH(self); |
| 11284 | kind = PyUnicode_KIND(self); |
| 11285 | data = PyUnicode_DATA(self); |
| 11286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11288 | if (length == 1) { |
| 11289 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11290 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11291 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11292 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11294 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11296 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | cased = 0; |
| 11299 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11300 | for (i = 0; i < length; i++) { |
| 11301 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11302 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11304 | if (previous_is_cased) |
| 11305 | return PyBool_FromLong(0); |
| 11306 | previous_is_cased = 1; |
| 11307 | cased = 1; |
| 11308 | } |
| 11309 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11310 | if (!previous_is_cased) |
| 11311 | return PyBool_FromLong(0); |
| 11312 | previous_is_cased = 1; |
| 11313 | cased = 1; |
| 11314 | } |
| 11315 | else |
| 11316 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11318 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | } |
| 11320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11321 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11322 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11324 | Return True if all characters in S are whitespace\n\ |
| 11325 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | |
| 11327 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11328 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11330 | Py_ssize_t i, length; |
| 11331 | int kind; |
| 11332 | void *data; |
| 11333 | |
| 11334 | if (PyUnicode_READY(self) == -1) |
| 11335 | return NULL; |
| 11336 | length = PyUnicode_GET_LENGTH(self); |
| 11337 | kind = PyUnicode_KIND(self); |
| 11338 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11340 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | if (length == 1) |
| 11342 | return PyBool_FromLong( |
| 11343 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11345 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11347 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11349 | for (i = 0; i < length; i++) { |
| 11350 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11351 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11352 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11354 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | } |
| 11356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11357 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11358 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11359 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11360 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11361 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11362 | |
| 11363 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11364 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11365 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11366 | Py_ssize_t i, length; |
| 11367 | int kind; |
| 11368 | void *data; |
| 11369 | |
| 11370 | if (PyUnicode_READY(self) == -1) |
| 11371 | return NULL; |
| 11372 | length = PyUnicode_GET_LENGTH(self); |
| 11373 | kind = PyUnicode_KIND(self); |
| 11374 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11375 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11376 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11377 | if (length == 1) |
| 11378 | return PyBool_FromLong( |
| 11379 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11380 | |
| 11381 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11382 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11383 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11385 | for (i = 0; i < length; i++) { |
| 11386 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11387 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11388 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11389 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11390 | } |
| 11391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11392 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11393 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11394 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11395 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11396 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11397 | |
| 11398 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11399 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11400 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | int kind; |
| 11402 | void *data; |
| 11403 | Py_ssize_t len, i; |
| 11404 | |
| 11405 | if (PyUnicode_READY(self) == -1) |
| 11406 | return NULL; |
| 11407 | |
| 11408 | kind = PyUnicode_KIND(self); |
| 11409 | data = PyUnicode_DATA(self); |
| 11410 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11411 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11412 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11413 | if (len == 1) { |
| 11414 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11415 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11416 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11417 | |
| 11418 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11419 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11420 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | for (i = 0; i < len; i++) { |
| 11423 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11424 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11425 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11426 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11427 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11428 | } |
| 11429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11430 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11431 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11433 | 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] | 11434 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | |
| 11436 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11437 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | Py_ssize_t i, length; |
| 11440 | int kind; |
| 11441 | void *data; |
| 11442 | |
| 11443 | if (PyUnicode_READY(self) == -1) |
| 11444 | return NULL; |
| 11445 | length = PyUnicode_GET_LENGTH(self); |
| 11446 | kind = PyUnicode_KIND(self); |
| 11447 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 | if (length == 1) |
| 11451 | return PyBool_FromLong( |
| 11452 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11454 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11455 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11456 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | for (i = 0; i < length; i++) { |
| 11459 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11460 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11462 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | } |
| 11464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11465 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11466 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11468 | Return True if all characters in S are digits\n\ |
| 11469 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | |
| 11471 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11472 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11473 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | Py_ssize_t i, length; |
| 11475 | int kind; |
| 11476 | void *data; |
| 11477 | |
| 11478 | if (PyUnicode_READY(self) == -1) |
| 11479 | return NULL; |
| 11480 | length = PyUnicode_GET_LENGTH(self); |
| 11481 | kind = PyUnicode_KIND(self); |
| 11482 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11485 | if (length == 1) { |
| 11486 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11487 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11488 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11490 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11491 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11492 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | for (i = 0; i < length; i++) { |
| 11495 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11496 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11498 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | } |
| 11500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11501 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11502 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11504 | 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] | 11505 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 | |
| 11507 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11508 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11510 | Py_ssize_t i, length; |
| 11511 | int kind; |
| 11512 | void *data; |
| 11513 | |
| 11514 | if (PyUnicode_READY(self) == -1) |
| 11515 | return NULL; |
| 11516 | length = PyUnicode_GET_LENGTH(self); |
| 11517 | kind = PyUnicode_KIND(self); |
| 11518 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | if (length == 1) |
| 11522 | return PyBool_FromLong( |
| 11523 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11525 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11526 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11527 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | for (i = 0; i < length; i++) { |
| 11530 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11531 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11533 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | } |
| 11535 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11536 | int |
| 11537 | PyUnicode_IsIdentifier(PyObject *self) |
| 11538 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11539 | int kind; |
| 11540 | void *data; |
| 11541 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11542 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11544 | if (PyUnicode_READY(self) == -1) { |
| 11545 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11546 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | } |
| 11548 | |
| 11549 | /* Special case for empty strings */ |
| 11550 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11551 | return 0; |
| 11552 | kind = PyUnicode_KIND(self); |
| 11553 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11554 | |
| 11555 | /* PEP 3131 says that the first character must be in |
| 11556 | XID_Start and subsequent characters in XID_Continue, |
| 11557 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11558 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11559 | letters, digits, underscore). However, given the current |
| 11560 | definition of XID_Start and XID_Continue, it is sufficient |
| 11561 | to check just for these, except that _ must be allowed |
| 11562 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11563 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11564 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11565 | return 0; |
| 11566 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11567 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11568 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11569 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11570 | return 1; |
| 11571 | } |
| 11572 | |
| 11573 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11574 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11575 | \n\ |
| 11576 | Return True if S is a valid identifier according\n\ |
| 11577 | to the language definition."); |
| 11578 | |
| 11579 | static PyObject* |
| 11580 | unicode_isidentifier(PyObject *self) |
| 11581 | { |
| 11582 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11583 | } |
| 11584 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11585 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11586 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11587 | \n\ |
| 11588 | Return True if all characters in S are considered\n\ |
| 11589 | printable in repr() or S is empty, False otherwise."); |
| 11590 | |
| 11591 | static PyObject* |
| 11592 | unicode_isprintable(PyObject *self) |
| 11593 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11594 | Py_ssize_t i, length; |
| 11595 | int kind; |
| 11596 | void *data; |
| 11597 | |
| 11598 | if (PyUnicode_READY(self) == -1) |
| 11599 | return NULL; |
| 11600 | length = PyUnicode_GET_LENGTH(self); |
| 11601 | kind = PyUnicode_KIND(self); |
| 11602 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11603 | |
| 11604 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | if (length == 1) |
| 11606 | return PyBool_FromLong( |
| 11607 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | for (i = 0; i < length; i++) { |
| 11610 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11611 | Py_RETURN_FALSE; |
| 11612 | } |
| 11613 | } |
| 11614 | Py_RETURN_TRUE; |
| 11615 | } |
| 11616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11617 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11618 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | \n\ |
| 11620 | 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] | 11621 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11622 | |
| 11623 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11624 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11626 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | } |
| 11628 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11629 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11630 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11632 | if (PyUnicode_READY(self) == -1) |
| 11633 | return -1; |
| 11634 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11635 | } |
| 11636 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11637 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11638 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11640 | 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] | 11641 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11642 | |
| 11643 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11644 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11646 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11647 | Py_UCS4 fillchar = ' '; |
| 11648 | |
| 11649 | if (PyUnicode_READY(self) == -1) |
| 11650 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11651 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11652 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | return NULL; |
| 11654 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11657 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11658 | } |
| 11659 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11660 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | } |
| 11662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11663 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11664 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11666 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11667 | |
| 11668 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11669 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | return fixup(self, fixlower); |
| 11672 | } |
| 11673 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11674 | #define LEFTSTRIP 0 |
| 11675 | #define RIGHTSTRIP 1 |
| 11676 | #define BOTHSTRIP 2 |
| 11677 | |
| 11678 | /* Arrays indexed by above */ |
| 11679 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11680 | |
| 11681 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11682 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11683 | /* externally visible for str.strip(unicode) */ |
| 11684 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11685 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11686 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11687 | void *data; |
| 11688 | int kind; |
| 11689 | Py_ssize_t i, j, len; |
| 11690 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11691 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11692 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11693 | return NULL; |
| 11694 | |
| 11695 | kind = PyUnicode_KIND(self); |
| 11696 | data = PyUnicode_DATA(self); |
| 11697 | len = PyUnicode_GET_LENGTH(self); |
| 11698 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11699 | PyUnicode_DATA(sepobj), |
| 11700 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11701 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11702 | i = 0; |
| 11703 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11704 | while (i < len && |
| 11705 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11706 | i++; |
| 11707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11708 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11709 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11710 | j = len; |
| 11711 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11712 | do { |
| 11713 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | } while (j >= i && |
| 11715 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11716 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11717 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11718 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11719 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | } |
| 11721 | |
| 11722 | PyObject* |
| 11723 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11724 | { |
| 11725 | unsigned char *data; |
| 11726 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11727 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11728 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11729 | if (PyUnicode_READY(self) == -1) |
| 11730 | return NULL; |
| 11731 | |
| 11732 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11733 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11734 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11735 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11736 | if (PyUnicode_CheckExact(self)) { |
| 11737 | Py_INCREF(self); |
| 11738 | return self; |
| 11739 | } |
| 11740 | else |
| 11741 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11742 | } |
| 11743 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11744 | length = end - start; |
| 11745 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11746 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11747 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11748 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11749 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11750 | return NULL; |
| 11751 | } |
| 11752 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11753 | if (PyUnicode_IS_ASCII(self)) { |
| 11754 | kind = PyUnicode_KIND(self); |
| 11755 | data = PyUnicode_1BYTE_DATA(self); |
| 11756 | return unicode_fromascii(data + start, length); |
| 11757 | } |
| 11758 | else { |
| 11759 | kind = PyUnicode_KIND(self); |
| 11760 | data = PyUnicode_1BYTE_DATA(self); |
| 11761 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11762 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11763 | length); |
| 11764 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11765 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11766 | |
| 11767 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11768 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11769 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11770 | int kind; |
| 11771 | void *data; |
| 11772 | Py_ssize_t len, i, j; |
| 11773 | |
| 11774 | if (PyUnicode_READY(self) == -1) |
| 11775 | return NULL; |
| 11776 | |
| 11777 | kind = PyUnicode_KIND(self); |
| 11778 | data = PyUnicode_DATA(self); |
| 11779 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11780 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11781 | i = 0; |
| 11782 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11783 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11784 | i++; |
| 11785 | } |
| 11786 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11787 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11788 | j = len; |
| 11789 | if (striptype != LEFTSTRIP) { |
| 11790 | do { |
| 11791 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11792 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11793 | j++; |
| 11794 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11795 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11796 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
| 11798 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11799 | |
| 11800 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11801 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11802 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11803 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11804 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11805 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11806 | return NULL; |
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 | if (sep != NULL && sep != Py_None) { |
| 11809 | if (PyUnicode_Check(sep)) |
| 11810 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11811 | else { |
| 11812 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11813 | "%s arg must be None or str", |
| 11814 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11815 | return NULL; |
| 11816 | } |
| 11817 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11818 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11819 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11820 | } |
| 11821 | |
| 11822 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11823 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11824 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11825 | \n\ |
| 11826 | Return a copy of the string S with leading and trailing\n\ |
| 11827 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11828 | 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] | 11829 | |
| 11830 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11831 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11832 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11833 | if (PyTuple_GET_SIZE(args) == 0) |
| 11834 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11835 | else |
| 11836 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
| 11839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11840 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11841 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11842 | \n\ |
| 11843 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11844 | 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] | 11845 | |
| 11846 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11847 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11849 | if (PyTuple_GET_SIZE(args) == 0) |
| 11850 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11851 | else |
| 11852 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11853 | } |
| 11854 | |
| 11855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11856 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11857 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11858 | \n\ |
| 11859 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11860 | 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] | 11861 | |
| 11862 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11863 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11864 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11865 | if (PyTuple_GET_SIZE(args) == 0) |
| 11866 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11867 | else |
| 11868 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11869 | } |
| 11870 | |
| 11871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11873 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11875 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11878 | if (len < 1) { |
| 11879 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11880 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11881 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11883 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | /* no repeat, return original string */ |
| 11885 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11886 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11888 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11889 | if (PyUnicode_READY(str) == -1) |
| 11890 | return NULL; |
| 11891 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11892 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11893 | PyErr_SetString(PyExc_OverflowError, |
| 11894 | "repeated string is too long"); |
| 11895 | return NULL; |
| 11896 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11897 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11898 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11899 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | if (!u) |
| 11901 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11902 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11905 | const int kind = PyUnicode_KIND(str); |
| 11906 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11907 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11908 | if (kind == PyUnicode_1BYTE_KIND) |
| 11909 | memset(to, (unsigned char)fill_char, len); |
| 11910 | else { |
| 11911 | for (n = 0; n < len; ++n) |
| 11912 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11913 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11914 | } |
| 11915 | else { |
| 11916 | /* number of characters copied this far */ |
| 11917 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11918 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11919 | char *to = (char *) PyUnicode_DATA(u); |
| 11920 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11921 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11922 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | n = (done <= nchars-done) ? done : nchars-done; |
| 11924 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11925 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11926 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | } |
| 11928 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11929 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11930 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | } |
| 11932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11933 | PyObject * |
| 11934 | PyUnicode_Replace(PyObject *obj, |
| 11935 | PyObject *subobj, |
| 11936 | PyObject *replobj, |
| 11937 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11938 | { |
| 11939 | PyObject *self; |
| 11940 | PyObject *str1; |
| 11941 | PyObject *str2; |
| 11942 | PyObject *result; |
| 11943 | |
| 11944 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11945 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11946 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11948 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11949 | Py_DECREF(self); |
| 11950 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | } |
| 11952 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11953 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11954 | Py_DECREF(self); |
| 11955 | Py_DECREF(str1); |
| 11956 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11958 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | Py_DECREF(self); |
| 11960 | Py_DECREF(str1); |
| 11961 | Py_DECREF(str2); |
| 11962 | return result; |
| 11963 | } |
| 11964 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11965 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11966 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11967 | \n\ |
| 11968 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11969 | old replaced by new. If the optional argument count is\n\ |
| 11970 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11971 | |
| 11972 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11975 | PyObject *str1; |
| 11976 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11977 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | PyObject *result; |
| 11979 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11980 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11982 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11983 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | str1 = PyUnicode_FromObject(str1); |
| 11985 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11986 | return NULL; |
| 11987 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11988 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11989 | Py_DECREF(str1); |
| 11990 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11991 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11992 | |
| 11993 | result = replace(self, str1, str2, maxcount); |
| 11994 | |
| 11995 | Py_DECREF(str1); |
| 11996 | Py_DECREF(str2); |
| 11997 | return result; |
| 11998 | } |
| 11999 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12000 | static PyObject * |
| 12001 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12002 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12003 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12004 | Py_ssize_t isize; |
| 12005 | Py_ssize_t osize, squote, dquote, i, o; |
| 12006 | Py_UCS4 max, quote; |
| 12007 | int ikind, okind; |
| 12008 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12009 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12010 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12011 | return NULL; |
| 12012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12013 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12014 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | /* Compute length of output, quote characters, and |
| 12017 | maximum character */ |
| 12018 | osize = 2; /* quotes */ |
| 12019 | max = 127; |
| 12020 | squote = dquote = 0; |
| 12021 | ikind = PyUnicode_KIND(unicode); |
| 12022 | for (i = 0; i < isize; i++) { |
| 12023 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12024 | switch (ch) { |
| 12025 | case '\'': squote++; osize++; break; |
| 12026 | case '"': dquote++; osize++; break; |
| 12027 | case '\\': case '\t': case '\r': case '\n': |
| 12028 | osize += 2; break; |
| 12029 | default: |
| 12030 | /* Fast-path ASCII */ |
| 12031 | if (ch < ' ' || ch == 0x7f) |
| 12032 | osize += 4; /* \xHH */ |
| 12033 | else if (ch < 0x7f) |
| 12034 | osize++; |
| 12035 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12036 | osize++; |
| 12037 | max = ch > max ? ch : max; |
| 12038 | } |
| 12039 | else if (ch < 0x100) |
| 12040 | osize += 4; /* \xHH */ |
| 12041 | else if (ch < 0x10000) |
| 12042 | osize += 6; /* \uHHHH */ |
| 12043 | else |
| 12044 | osize += 10; /* \uHHHHHHHH */ |
| 12045 | } |
| 12046 | } |
| 12047 | |
| 12048 | quote = '\''; |
| 12049 | if (squote) { |
| 12050 | if (dquote) |
| 12051 | /* Both squote and dquote present. Use squote, |
| 12052 | and escape them */ |
| 12053 | osize += squote; |
| 12054 | else |
| 12055 | quote = '"'; |
| 12056 | } |
| 12057 | |
| 12058 | repr = PyUnicode_New(osize, max); |
| 12059 | if (repr == NULL) |
| 12060 | return NULL; |
| 12061 | okind = PyUnicode_KIND(repr); |
| 12062 | odata = PyUnicode_DATA(repr); |
| 12063 | |
| 12064 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12065 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12066 | |
| 12067 | for (i = 0, o = 1; i < isize; i++) { |
| 12068 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12069 | |
| 12070 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | if ((ch == quote) || (ch == '\\')) { |
| 12072 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12073 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12074 | continue; |
| 12075 | } |
| 12076 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12077 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12078 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12080 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12081 | } |
| 12082 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12083 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12084 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12085 | } |
| 12086 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12087 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12088 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12089 | } |
| 12090 | |
| 12091 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12092 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12094 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12095 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12096 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12097 | } |
| 12098 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12099 | /* Copy ASCII characters as-is */ |
| 12100 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12102 | } |
| 12103 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12104 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12105 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12106 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12107 | (categories Z* and C* except ASCII space) |
| 12108 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12109 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12110 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12111 | if (ch <= 0xff) { |
| 12112 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12113 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12114 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12115 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12116 | } |
| 12117 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12118 | else if (ch >= 0x10000) { |
| 12119 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12120 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12121 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12122 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12123 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12124 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12125 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12126 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12127 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12128 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12129 | } |
| 12130 | /* Map 16-bit characters to '\uxxxx' */ |
| 12131 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12132 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12133 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12134 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12135 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12136 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12137 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12138 | } |
| 12139 | } |
| 12140 | /* Copy characters as-is */ |
| 12141 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12143 | } |
| 12144 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12145 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12147 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12148 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12149 | } |
| 12150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12151 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12152 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12153 | \n\ |
| 12154 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12155 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | arguments start and end are interpreted as in slice notation.\n\ |
| 12157 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12158 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | |
| 12160 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12161 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12162 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12163 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12164 | Py_ssize_t start; |
| 12165 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12166 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12167 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12168 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12169 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12170 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12172 | if (PyUnicode_READY(self) == -1) |
| 12173 | return NULL; |
| 12174 | if (PyUnicode_READY(substring) == -1) |
| 12175 | return NULL; |
| 12176 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12177 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | |
| 12179 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | if (result == -2) |
| 12182 | return NULL; |
| 12183 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12184 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12185 | } |
| 12186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12187 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12188 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12189 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12190 | 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] | 12191 | |
| 12192 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12193 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12194 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12195 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12196 | Py_ssize_t start; |
| 12197 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12198 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12200 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12201 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12202 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12204 | if (PyUnicode_READY(self) == -1) |
| 12205 | return NULL; |
| 12206 | if (PyUnicode_READY(substring) == -1) |
| 12207 | return NULL; |
| 12208 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12209 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | |
| 12211 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12213 | if (result == -2) |
| 12214 | return NULL; |
| 12215 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | if (result < 0) { |
| 12217 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12218 | return NULL; |
| 12219 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12220 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12221 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12222 | } |
| 12223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12224 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12225 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12227 | 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] | 12228 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12229 | |
| 12230 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12231 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12233 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | Py_UCS4 fillchar = ' '; |
| 12235 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12236 | 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] | 12237 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12238 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12239 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12240 | return NULL; |
| 12241 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12242 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12244 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12245 | } |
| 12246 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12247 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | } |
| 12249 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12250 | PyObject * |
| 12251 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12252 | { |
| 12253 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | s = PyUnicode_FromObject(s); |
| 12256 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12257 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12258 | if (sep != NULL) { |
| 12259 | sep = PyUnicode_FromObject(sep); |
| 12260 | if (sep == NULL) { |
| 12261 | Py_DECREF(s); |
| 12262 | return NULL; |
| 12263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | } |
| 12265 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12266 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | |
| 12268 | Py_DECREF(s); |
| 12269 | Py_XDECREF(sep); |
| 12270 | return result; |
| 12271 | } |
| 12272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12273 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12274 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | \n\ |
| 12276 | Return a list of the words in S, using sep as the\n\ |
| 12277 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12278 | 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] | 12279 | whitespace string is a separator and empty strings are\n\ |
| 12280 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | |
| 12282 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12283 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12284 | { |
| 12285 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12286 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12287 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12288 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | return NULL; |
| 12290 | |
| 12291 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12292 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12294 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12296 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 | } |
| 12298 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12299 | PyObject * |
| 12300 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12301 | { |
| 12302 | PyObject* str_obj; |
| 12303 | PyObject* sep_obj; |
| 12304 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12305 | int kind1, kind2, kind; |
| 12306 | void *buf1 = NULL, *buf2 = NULL; |
| 12307 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12308 | |
| 12309 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12310 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12311 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12312 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12313 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12314 | Py_DECREF(str_obj); |
| 12315 | return NULL; |
| 12316 | } |
| 12317 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12318 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12319 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12320 | kind = Py_MAX(kind1, kind2); |
| 12321 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12323 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12324 | if (!buf1) |
| 12325 | goto onError; |
| 12326 | buf2 = PyUnicode_DATA(sep_obj); |
| 12327 | if (kind2 != kind) |
| 12328 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12329 | if (!buf2) |
| 12330 | goto onError; |
| 12331 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12332 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12333 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12334 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12335 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12336 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12337 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12338 | else |
| 12339 | 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] | 12340 | break; |
| 12341 | case PyUnicode_2BYTE_KIND: |
| 12342 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12343 | break; |
| 12344 | case PyUnicode_4BYTE_KIND: |
| 12345 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12346 | break; |
| 12347 | default: |
| 12348 | assert(0); |
| 12349 | out = 0; |
| 12350 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12351 | |
| 12352 | Py_DECREF(sep_obj); |
| 12353 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12354 | if (kind1 != kind) |
| 12355 | PyMem_Free(buf1); |
| 12356 | if (kind2 != kind) |
| 12357 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12358 | |
| 12359 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12360 | onError: |
| 12361 | Py_DECREF(sep_obj); |
| 12362 | Py_DECREF(str_obj); |
| 12363 | if (kind1 != kind && buf1) |
| 12364 | PyMem_Free(buf1); |
| 12365 | if (kind2 != kind && buf2) |
| 12366 | PyMem_Free(buf2); |
| 12367 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12368 | } |
| 12369 | |
| 12370 | |
| 12371 | PyObject * |
| 12372 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12373 | { |
| 12374 | PyObject* str_obj; |
| 12375 | PyObject* sep_obj; |
| 12376 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12377 | int kind1, kind2, kind; |
| 12378 | void *buf1 = NULL, *buf2 = NULL; |
| 12379 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12380 | |
| 12381 | str_obj = PyUnicode_FromObject(str_in); |
| 12382 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12383 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12384 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12385 | if (!sep_obj) { |
| 12386 | Py_DECREF(str_obj); |
| 12387 | return NULL; |
| 12388 | } |
| 12389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12390 | kind1 = PyUnicode_KIND(str_in); |
| 12391 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12392 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12393 | buf1 = PyUnicode_DATA(str_in); |
| 12394 | if (kind1 != kind) |
| 12395 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12396 | if (!buf1) |
| 12397 | goto onError; |
| 12398 | buf2 = PyUnicode_DATA(sep_obj); |
| 12399 | if (kind2 != kind) |
| 12400 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12401 | if (!buf2) |
| 12402 | goto onError; |
| 12403 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12404 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12405 | |
| 12406 | switch(PyUnicode_KIND(str_in)) { |
| 12407 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12408 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12409 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12410 | else |
| 12411 | 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] | 12412 | break; |
| 12413 | case PyUnicode_2BYTE_KIND: |
| 12414 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12415 | break; |
| 12416 | case PyUnicode_4BYTE_KIND: |
| 12417 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12418 | break; |
| 12419 | default: |
| 12420 | assert(0); |
| 12421 | out = 0; |
| 12422 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12423 | |
| 12424 | Py_DECREF(sep_obj); |
| 12425 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12426 | if (kind1 != kind) |
| 12427 | PyMem_Free(buf1); |
| 12428 | if (kind2 != kind) |
| 12429 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12430 | |
| 12431 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12432 | onError: |
| 12433 | Py_DECREF(sep_obj); |
| 12434 | Py_DECREF(str_obj); |
| 12435 | if (kind1 != kind && buf1) |
| 12436 | PyMem_Free(buf1); |
| 12437 | if (kind2 != kind && buf2) |
| 12438 | PyMem_Free(buf2); |
| 12439 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12440 | } |
| 12441 | |
| 12442 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12443 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12444 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12445 | 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] | 12446 | 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] | 12447 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12448 | |
| 12449 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12450 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12451 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12452 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12453 | } |
| 12454 | |
| 12455 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12456 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12457 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12458 | 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] | 12459 | 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] | 12460 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12461 | |
| 12462 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12463 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12464 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12465 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12466 | } |
| 12467 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12468 | PyObject * |
| 12469 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12470 | { |
| 12471 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12472 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12473 | s = PyUnicode_FromObject(s); |
| 12474 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12475 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12476 | if (sep != NULL) { |
| 12477 | sep = PyUnicode_FromObject(sep); |
| 12478 | if (sep == NULL) { |
| 12479 | Py_DECREF(s); |
| 12480 | return NULL; |
| 12481 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12482 | } |
| 12483 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12484 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12485 | |
| 12486 | Py_DECREF(s); |
| 12487 | Py_XDECREF(sep); |
| 12488 | return result; |
| 12489 | } |
| 12490 | |
| 12491 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12492 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12493 | \n\ |
| 12494 | Return a list of the words in S, using sep as the\n\ |
| 12495 | delimiter string, starting at the end of the string and\n\ |
| 12496 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12497 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12498 | is a separator."); |
| 12499 | |
| 12500 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12501 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12502 | { |
| 12503 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12504 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12505 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12506 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12507 | return NULL; |
| 12508 | |
| 12509 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12510 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12511 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12512 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12513 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12514 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12515 | } |
| 12516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12517 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12518 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | \n\ |
| 12520 | 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] | 12521 | 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] | 12522 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12523 | |
| 12524 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12525 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12527 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12528 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12530 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12531 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12532 | return NULL; |
| 12533 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12534 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12535 | } |
| 12536 | |
| 12537 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12538 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12540 | if (PyUnicode_CheckExact(self)) { |
| 12541 | Py_INCREF(self); |
| 12542 | return self; |
| 12543 | } else |
| 12544 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12545 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | } |
| 12547 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12548 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12549 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12550 | \n\ |
| 12551 | 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] | 12552 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12553 | |
| 12554 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12555 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12556 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | return fixup(self, fixswapcase); |
| 12558 | } |
| 12559 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12560 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12561 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12562 | \n\ |
| 12563 | Return a translation table usable for str.translate().\n\ |
| 12564 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12565 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12566 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12567 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12568 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12569 | character at the same position in y. If there is a third argument, it\n\ |
| 12570 | must be a string, whose characters will be mapped to None in the result."); |
| 12571 | |
| 12572 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12573 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12574 | { |
| 12575 | PyObject *x, *y = NULL, *z = NULL; |
| 12576 | PyObject *new = NULL, *key, *value; |
| 12577 | Py_ssize_t i = 0; |
| 12578 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12579 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12580 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12581 | return NULL; |
| 12582 | new = PyDict_New(); |
| 12583 | if (!new) |
| 12584 | return NULL; |
| 12585 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12586 | int x_kind, y_kind, z_kind; |
| 12587 | void *x_data, *y_data, *z_data; |
| 12588 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12589 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12590 | if (!PyUnicode_Check(x)) { |
| 12591 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12592 | "be a string if there is a second argument"); |
| 12593 | goto err; |
| 12594 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12595 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12596 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12597 | "arguments must have equal length"); |
| 12598 | goto err; |
| 12599 | } |
| 12600 | /* 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] | 12601 | x_kind = PyUnicode_KIND(x); |
| 12602 | y_kind = PyUnicode_KIND(y); |
| 12603 | x_data = PyUnicode_DATA(x); |
| 12604 | y_data = PyUnicode_DATA(y); |
| 12605 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12606 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12607 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12608 | if (!key || !value) |
| 12609 | goto err; |
| 12610 | res = PyDict_SetItem(new, key, value); |
| 12611 | Py_DECREF(key); |
| 12612 | Py_DECREF(value); |
| 12613 | if (res < 0) |
| 12614 | goto err; |
| 12615 | } |
| 12616 | /* create entries for deleting chars in z */ |
| 12617 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12618 | z_kind = PyUnicode_KIND(z); |
| 12619 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12620 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12621 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12622 | if (!key) |
| 12623 | goto err; |
| 12624 | res = PyDict_SetItem(new, key, Py_None); |
| 12625 | Py_DECREF(key); |
| 12626 | if (res < 0) |
| 12627 | goto err; |
| 12628 | } |
| 12629 | } |
| 12630 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12631 | int kind; |
| 12632 | void *data; |
| 12633 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12634 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12635 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12636 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12637 | "to maketrans it must be a dict"); |
| 12638 | goto err; |
| 12639 | } |
| 12640 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12641 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12642 | if (PyUnicode_Check(key)) { |
| 12643 | /* convert string keys to integer keys */ |
| 12644 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12645 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12646 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12647 | "table must be of length 1"); |
| 12648 | goto err; |
| 12649 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12650 | kind = PyUnicode_KIND(key); |
| 12651 | data = PyUnicode_DATA(key); |
| 12652 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12653 | if (!newkey) |
| 12654 | goto err; |
| 12655 | res = PyDict_SetItem(new, newkey, value); |
| 12656 | Py_DECREF(newkey); |
| 12657 | if (res < 0) |
| 12658 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12659 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12660 | /* just keep integer keys */ |
| 12661 | if (PyDict_SetItem(new, key, value) < 0) |
| 12662 | goto err; |
| 12663 | } else { |
| 12664 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12665 | "be strings or integers"); |
| 12666 | goto err; |
| 12667 | } |
| 12668 | } |
| 12669 | } |
| 12670 | return new; |
| 12671 | err: |
| 12672 | Py_DECREF(new); |
| 12673 | return NULL; |
| 12674 | } |
| 12675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12676 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12677 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | \n\ |
| 12679 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12680 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12681 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12682 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12683 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | |
| 12685 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12686 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12688 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | } |
| 12690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12691 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12692 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12693 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12694 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12695 | |
| 12696 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12697 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12698 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | return fixup(self, fixupper); |
| 12700 | } |
| 12701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12702 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12703 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12704 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12705 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12706 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12707 | |
| 12708 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12709 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12710 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12711 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12712 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12713 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | int kind; |
| 12715 | void *data; |
| 12716 | Py_UCS4 chr; |
| 12717 | |
| 12718 | if (PyUnicode_READY(self) == -1) |
| 12719 | return NULL; |
| 12720 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12721 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12722 | return NULL; |
| 12723 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12724 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12725 | if (PyUnicode_CheckExact(self)) { |
| 12726 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12727 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12728 | } |
| 12729 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12730 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12731 | } |
| 12732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12733 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12734 | |
| 12735 | u = pad(self, fill, 0, '0'); |
| 12736 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12737 | if (u == NULL) |
| 12738 | return NULL; |
| 12739 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12740 | kind = PyUnicode_KIND(u); |
| 12741 | data = PyUnicode_DATA(u); |
| 12742 | chr = PyUnicode_READ(kind, data, fill); |
| 12743 | |
| 12744 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12746 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12747 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12748 | } |
| 12749 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12750 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12751 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12752 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12753 | |
| 12754 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12755 | static PyObject * |
| 12756 | unicode__decimal2ascii(PyObject *self) |
| 12757 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12758 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12760 | #endif |
| 12761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12762 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12763 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12764 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12765 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12766 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12767 | With optional end, stop comparing S at that position.\n\ |
| 12768 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | |
| 12770 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12771 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12772 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12773 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12775 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12776 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12777 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12778 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12779 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12780 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12781 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12782 | if (PyTuple_Check(subobj)) { |
| 12783 | Py_ssize_t i; |
| 12784 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12785 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12786 | if (substring == NULL) |
| 12787 | return NULL; |
| 12788 | result = tailmatch(self, substring, start, end, -1); |
| 12789 | Py_DECREF(substring); |
| 12790 | if (result) { |
| 12791 | Py_RETURN_TRUE; |
| 12792 | } |
| 12793 | } |
| 12794 | /* nothing matched */ |
| 12795 | Py_RETURN_FALSE; |
| 12796 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12797 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12798 | if (substring == NULL) { |
| 12799 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12800 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12801 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12803 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12804 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12805 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12806 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12807 | } |
| 12808 | |
| 12809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12810 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12811 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12812 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12813 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12814 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12815 | With optional end, stop comparing S at that position.\n\ |
| 12816 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12817 | |
| 12818 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12819 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12820 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12822 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12823 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12824 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12825 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12826 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12827 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12828 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12829 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12830 | if (PyTuple_Check(subobj)) { |
| 12831 | Py_ssize_t i; |
| 12832 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12833 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12834 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12835 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12836 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12837 | result = tailmatch(self, substring, start, end, +1); |
| 12838 | Py_DECREF(substring); |
| 12839 | if (result) { |
| 12840 | Py_RETURN_TRUE; |
| 12841 | } |
| 12842 | } |
| 12843 | Py_RETURN_FALSE; |
| 12844 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12845 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12846 | if (substring == NULL) { |
| 12847 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12848 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12849 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12850 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12851 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12852 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12853 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12854 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12857 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12858 | |
| 12859 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12860 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12861 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12862 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12863 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12864 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12865 | PyDoc_STRVAR(format_map__doc__, |
| 12866 | "S.format_map(mapping) -> str\n\ |
| 12867 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12868 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12869 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12870 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12871 | static PyObject * |
| 12872 | unicode__format__(PyObject* self, PyObject* args) |
| 12873 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12874 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12875 | |
| 12876 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12877 | return NULL; |
| 12878 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12879 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12880 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12881 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12882 | } |
| 12883 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12884 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12885 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12886 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12887 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12888 | |
| 12889 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12890 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12891 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12892 | Py_ssize_t size; |
| 12893 | |
| 12894 | /* If it's a compact object, account for base structure + |
| 12895 | character data. */ |
| 12896 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12897 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12898 | else if (PyUnicode_IS_COMPACT(v)) |
| 12899 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12900 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12901 | else { |
| 12902 | /* If it is a two-block object, account for base object, and |
| 12903 | for character block if present. */ |
| 12904 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12905 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12906 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12907 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12908 | } |
| 12909 | /* 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] | 12910 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12911 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12912 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12913 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12914 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12915 | |
| 12916 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12917 | } |
| 12918 | |
| 12919 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12920 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12921 | |
| 12922 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12923 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12924 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12925 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12926 | if (!copy) |
| 12927 | return NULL; |
| 12928 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12929 | } |
| 12930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12931 | static PyMethodDef unicode_methods[] = { |
| 12932 | |
| 12933 | /* Order is according to common usage: often used methods should |
| 12934 | appear first, since lookup is done sequentially. */ |
| 12935 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12936 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12937 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12938 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12939 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12940 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12941 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12942 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12943 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12944 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12945 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12946 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12947 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12948 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12949 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12950 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12951 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12952 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12953 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12954 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12955 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12956 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12957 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12958 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12959 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12960 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12961 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12962 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12963 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12964 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12965 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12966 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12967 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12968 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12969 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12970 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12971 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12972 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12973 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12974 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12975 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12976 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12977 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12978 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12979 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12980 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12981 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12982 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12983 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12984 | #endif |
| 12985 | |
| 12986 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12987 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12988 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12989 | #endif |
| 12990 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12991 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12992 | {NULL, NULL} |
| 12993 | }; |
| 12994 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12995 | static PyObject * |
| 12996 | unicode_mod(PyObject *v, PyObject *w) |
| 12997 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12998 | if (!PyUnicode_Check(v)) |
| 12999 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13000 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13001 | } |
| 13002 | |
| 13003 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13004 | 0, /*nb_add*/ |
| 13005 | 0, /*nb_subtract*/ |
| 13006 | 0, /*nb_multiply*/ |
| 13007 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13008 | }; |
| 13009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13010 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13011 | (lenfunc) unicode_length, /* sq_length */ |
| 13012 | PyUnicode_Concat, /* sq_concat */ |
| 13013 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13014 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13015 | 0, /* sq_slice */ |
| 13016 | 0, /* sq_ass_item */ |
| 13017 | 0, /* sq_ass_slice */ |
| 13018 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13019 | }; |
| 13020 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13021 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13022 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13023 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13024 | if (PyUnicode_READY(self) == -1) |
| 13025 | return NULL; |
| 13026 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13027 | if (PyIndex_Check(item)) { |
| 13028 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13029 | if (i == -1 && PyErr_Occurred()) |
| 13030 | return NULL; |
| 13031 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13032 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13033 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13034 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13035 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13036 | PyObject *result; |
| 13037 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13038 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13039 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13040 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13041 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13042 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13043 | return NULL; |
| 13044 | } |
| 13045 | |
| 13046 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13047 | return PyUnicode_New(0, 0); |
| 13048 | } else if (start == 0 && step == 1 && |
| 13049 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13050 | PyUnicode_CheckExact(self)) { |
| 13051 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13052 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13053 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13054 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13055 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13056 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13057 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13058 | src_kind = PyUnicode_KIND(self); |
| 13059 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13060 | if (!PyUnicode_IS_ASCII(self)) { |
| 13061 | kind_limit = kind_maxchar_limit(src_kind); |
| 13062 | max_char = 0; |
| 13063 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13064 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13065 | if (ch > max_char) { |
| 13066 | max_char = ch; |
| 13067 | if (max_char >= kind_limit) |
| 13068 | break; |
| 13069 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13070 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13071 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13072 | else |
| 13073 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13074 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13075 | if (result == NULL) |
| 13076 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13077 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13078 | dest_data = PyUnicode_DATA(result); |
| 13079 | |
| 13080 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13081 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13082 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13083 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13084 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13085 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13086 | } else { |
| 13087 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13088 | return NULL; |
| 13089 | } |
| 13090 | } |
| 13091 | |
| 13092 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13093 | (lenfunc)unicode_length, /* mp_length */ |
| 13094 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13095 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13096 | }; |
| 13097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | /* Helpers for PyUnicode_Format() */ |
| 13100 | |
| 13101 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13102 | 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] | 13103 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13104 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13105 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13106 | (*p_argidx)++; |
| 13107 | if (arglen < 0) |
| 13108 | return args; |
| 13109 | else |
| 13110 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | } |
| 13112 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13113 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | return NULL; |
| 13115 | } |
| 13116 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13117 | /* 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] | 13118 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13119 | static PyObject * |
| 13120 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13121 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13122 | char *p; |
| 13123 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13124 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13126 | x = PyFloat_AsDouble(v); |
| 13127 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13128 | return NULL; |
| 13129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13131 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13132 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13133 | p = PyOS_double_to_string(x, type, prec, |
| 13134 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13135 | if (p == NULL) |
| 13136 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13137 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13138 | PyMem_Free(p); |
| 13139 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13140 | } |
| 13141 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13142 | static PyObject* |
| 13143 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13144 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13145 | char *buf; |
| 13146 | int len; |
| 13147 | PyObject *str; /* temporary string object. */ |
| 13148 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13149 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13150 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13151 | if (!str) |
| 13152 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13153 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13154 | Py_DECREF(str); |
| 13155 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13156 | } |
| 13157 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13158 | static Py_UCS4 |
| 13159 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13160 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13161 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13162 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13163 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13164 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13166 | goto onError; |
| 13167 | } |
| 13168 | else { |
| 13169 | /* Integer input truncated to a character */ |
| 13170 | long x; |
| 13171 | x = PyLong_AsLong(v); |
| 13172 | if (x == -1 && PyErr_Occurred()) |
| 13173 | goto onError; |
| 13174 | |
| 13175 | if (x < 0 || x > 0x10ffff) { |
| 13176 | PyErr_SetString(PyExc_OverflowError, |
| 13177 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13178 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13179 | } |
| 13180 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13181 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13182 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13183 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13184 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13185 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13186 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13187 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | } |
| 13189 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13190 | static int |
| 13191 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13192 | { |
| 13193 | int r; |
| 13194 | assert(count > 0); |
| 13195 | assert(PyUnicode_Check(obj)); |
| 13196 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13197 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13198 | if (repeated == NULL) |
| 13199 | return -1; |
| 13200 | r = _PyAccu_Accumulate(acc, repeated); |
| 13201 | Py_DECREF(repeated); |
| 13202 | return r; |
| 13203 | } |
| 13204 | else { |
| 13205 | do { |
| 13206 | if (_PyAccu_Accumulate(acc, obj)) |
| 13207 | return -1; |
| 13208 | } while (--count); |
| 13209 | return 0; |
| 13210 | } |
| 13211 | } |
| 13212 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13213 | PyObject * |
| 13214 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13215 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13216 | void *fmt; |
| 13217 | int fmtkind; |
| 13218 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13220 | int r; |
| 13221 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13222 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13223 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13224 | PyObject *temp = NULL; |
| 13225 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13226 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13227 | _PyAccu acc; |
| 13228 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13229 | |
| 13230 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13231 | return NULL; |
| 13232 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13233 | return NULL; |
| 13234 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13235 | return NULL; |
| 13236 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13237 | return NULL; |
| 13238 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13239 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13241 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13242 | PyErr_BadInternalCall(); |
| 13243 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13244 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13245 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13246 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13247 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13248 | if (_PyAccu_Init(&acc)) |
| 13249 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13250 | fmt = PyUnicode_DATA(uformat); |
| 13251 | fmtkind = PyUnicode_KIND(uformat); |
| 13252 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13253 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13255 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13256 | arglen = PyTuple_Size(args); |
| 13257 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13258 | } |
| 13259 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13260 | arglen = -1; |
| 13261 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13262 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13263 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13264 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13265 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13266 | |
| 13267 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13268 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13269 | PyObject *nonfmt; |
| 13270 | Py_ssize_t nonfmtpos; |
| 13271 | nonfmtpos = fmtpos++; |
| 13272 | while (fmtcnt >= 0 && |
| 13273 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13274 | fmtpos++; |
| 13275 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13276 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13277 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13278 | if (nonfmt == NULL) |
| 13279 | goto onError; |
| 13280 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13281 | Py_DECREF(nonfmt); |
| 13282 | if (r) |
| 13283 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13284 | } |
| 13285 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13286 | /* Got a format specifier */ |
| 13287 | int flags = 0; |
| 13288 | Py_ssize_t width = -1; |
| 13289 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13290 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13291 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13292 | int isnumok; |
| 13293 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13294 | void *pbuf = NULL; |
| 13295 | Py_ssize_t pindex, len; |
| 13296 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13298 | fmtpos++; |
| 13299 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13300 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13301 | Py_ssize_t keylen; |
| 13302 | PyObject *key; |
| 13303 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13304 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13305 | if (dict == NULL) { |
| 13306 | PyErr_SetString(PyExc_TypeError, |
| 13307 | "format requires a mapping"); |
| 13308 | goto onError; |
| 13309 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13310 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13311 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13312 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13313 | /* Skip over balanced parentheses */ |
| 13314 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13315 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13316 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13317 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13318 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13319 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13320 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13321 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13322 | if (fmtcnt < 0 || pcount > 0) { |
| 13323 | PyErr_SetString(PyExc_ValueError, |
| 13324 | "incomplete format key"); |
| 13325 | goto onError; |
| 13326 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13327 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13328 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13329 | if (key == NULL) |
| 13330 | goto onError; |
| 13331 | if (args_owned) { |
| 13332 | Py_DECREF(args); |
| 13333 | args_owned = 0; |
| 13334 | } |
| 13335 | args = PyObject_GetItem(dict, key); |
| 13336 | Py_DECREF(key); |
| 13337 | if (args == NULL) { |
| 13338 | goto onError; |
| 13339 | } |
| 13340 | args_owned = 1; |
| 13341 | arglen = -1; |
| 13342 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13344 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13345 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13346 | case '-': flags |= F_LJUST; continue; |
| 13347 | case '+': flags |= F_SIGN; continue; |
| 13348 | case ' ': flags |= F_BLANK; continue; |
| 13349 | case '#': flags |= F_ALT; continue; |
| 13350 | case '0': flags |= F_ZERO; continue; |
| 13351 | } |
| 13352 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13353 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13354 | if (c == '*') { |
| 13355 | v = getnextarg(args, arglen, &argidx); |
| 13356 | if (v == NULL) |
| 13357 | goto onError; |
| 13358 | if (!PyLong_Check(v)) { |
| 13359 | PyErr_SetString(PyExc_TypeError, |
| 13360 | "* wants int"); |
| 13361 | goto onError; |
| 13362 | } |
| 13363 | width = PyLong_AsLong(v); |
| 13364 | if (width == -1 && PyErr_Occurred()) |
| 13365 | goto onError; |
| 13366 | if (width < 0) { |
| 13367 | flags |= F_LJUST; |
| 13368 | width = -width; |
| 13369 | } |
| 13370 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13371 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13372 | } |
| 13373 | else if (c >= '0' && c <= '9') { |
| 13374 | width = c - '0'; |
| 13375 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13376 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13377 | if (c < '0' || c > '9') |
| 13378 | break; |
| 13379 | if ((width*10) / 10 != width) { |
| 13380 | PyErr_SetString(PyExc_ValueError, |
| 13381 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13382 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13383 | } |
| 13384 | width = width*10 + (c - '0'); |
| 13385 | } |
| 13386 | } |
| 13387 | if (c == '.') { |
| 13388 | prec = 0; |
| 13389 | if (--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 == '*') { |
| 13392 | v = getnextarg(args, arglen, &argidx); |
| 13393 | if (v == NULL) |
| 13394 | goto onError; |
| 13395 | if (!PyLong_Check(v)) { |
| 13396 | PyErr_SetString(PyExc_TypeError, |
| 13397 | "* wants int"); |
| 13398 | goto onError; |
| 13399 | } |
| 13400 | prec = PyLong_AsLong(v); |
| 13401 | if (prec == -1 && PyErr_Occurred()) |
| 13402 | goto onError; |
| 13403 | if (prec < 0) |
| 13404 | prec = 0; |
| 13405 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13406 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13407 | } |
| 13408 | else if (c >= '0' && c <= '9') { |
| 13409 | prec = c - '0'; |
| 13410 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13411 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13412 | if (c < '0' || c > '9') |
| 13413 | break; |
| 13414 | if ((prec*10) / 10 != prec) { |
| 13415 | PyErr_SetString(PyExc_ValueError, |
| 13416 | "prec too big"); |
| 13417 | goto onError; |
| 13418 | } |
| 13419 | prec = prec*10 + (c - '0'); |
| 13420 | } |
| 13421 | } |
| 13422 | } /* prec */ |
| 13423 | if (fmtcnt >= 0) { |
| 13424 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13425 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13426 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13427 | } |
| 13428 | } |
| 13429 | if (fmtcnt < 0) { |
| 13430 | PyErr_SetString(PyExc_ValueError, |
| 13431 | "incomplete format"); |
| 13432 | goto onError; |
| 13433 | } |
| 13434 | if (c != '%') { |
| 13435 | v = getnextarg(args, arglen, &argidx); |
| 13436 | if (v == NULL) |
| 13437 | goto onError; |
| 13438 | } |
| 13439 | sign = 0; |
| 13440 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13441 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13442 | switch (c) { |
| 13443 | |
| 13444 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13445 | _PyAccu_Accumulate(&acc, percent); |
| 13446 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13447 | |
| 13448 | case 's': |
| 13449 | case 'r': |
| 13450 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13451 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13452 | temp = v; |
| 13453 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13454 | } |
| 13455 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13456 | if (c == 's') |
| 13457 | temp = PyObject_Str(v); |
| 13458 | else if (c == 'r') |
| 13459 | temp = PyObject_Repr(v); |
| 13460 | else |
| 13461 | temp = PyObject_ASCII(v); |
| 13462 | if (temp == NULL) |
| 13463 | goto onError; |
| 13464 | if (PyUnicode_Check(temp)) |
| 13465 | /* nothing to do */; |
| 13466 | else { |
| 13467 | Py_DECREF(temp); |
| 13468 | PyErr_SetString(PyExc_TypeError, |
| 13469 | "%s argument has non-string str()"); |
| 13470 | goto onError; |
| 13471 | } |
| 13472 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13473 | if (PyUnicode_READY(temp) == -1) { |
| 13474 | Py_CLEAR(temp); |
| 13475 | goto onError; |
| 13476 | } |
| 13477 | pbuf = PyUnicode_DATA(temp); |
| 13478 | kind = PyUnicode_KIND(temp); |
| 13479 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13480 | if (prec >= 0 && len > prec) |
| 13481 | len = prec; |
| 13482 | break; |
| 13483 | |
| 13484 | case 'i': |
| 13485 | case 'd': |
| 13486 | case 'u': |
| 13487 | case 'o': |
| 13488 | case 'x': |
| 13489 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13490 | isnumok = 0; |
| 13491 | if (PyNumber_Check(v)) { |
| 13492 | PyObject *iobj=NULL; |
| 13493 | |
| 13494 | if (PyLong_Check(v)) { |
| 13495 | iobj = v; |
| 13496 | Py_INCREF(iobj); |
| 13497 | } |
| 13498 | else { |
| 13499 | iobj = PyNumber_Long(v); |
| 13500 | } |
| 13501 | if (iobj!=NULL) { |
| 13502 | if (PyLong_Check(iobj)) { |
| 13503 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13504 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13505 | Py_DECREF(iobj); |
| 13506 | if (!temp) |
| 13507 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13508 | if (PyUnicode_READY(temp) == -1) { |
| 13509 | Py_CLEAR(temp); |
| 13510 | goto onError; |
| 13511 | } |
| 13512 | pbuf = PyUnicode_DATA(temp); |
| 13513 | kind = PyUnicode_KIND(temp); |
| 13514 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13515 | sign = 1; |
| 13516 | } |
| 13517 | else { |
| 13518 | Py_DECREF(iobj); |
| 13519 | } |
| 13520 | } |
| 13521 | } |
| 13522 | if (!isnumok) { |
| 13523 | PyErr_Format(PyExc_TypeError, |
| 13524 | "%%%c format: a number is required, " |
| 13525 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13526 | goto onError; |
| 13527 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13528 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13529 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13530 | fillobj = zero; |
| 13531 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13532 | break; |
| 13533 | |
| 13534 | case 'e': |
| 13535 | case 'E': |
| 13536 | case 'f': |
| 13537 | case 'F': |
| 13538 | case 'g': |
| 13539 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13540 | temp = formatfloat(v, flags, prec, c); |
| 13541 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13542 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13543 | if (PyUnicode_READY(temp) == -1) { |
| 13544 | Py_CLEAR(temp); |
| 13545 | goto onError; |
| 13546 | } |
| 13547 | pbuf = PyUnicode_DATA(temp); |
| 13548 | kind = PyUnicode_KIND(temp); |
| 13549 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13550 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13551 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13552 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13553 | fillobj = zero; |
| 13554 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13555 | break; |
| 13556 | |
| 13557 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13558 | { |
| 13559 | Py_UCS4 ch = formatchar(v); |
| 13560 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13561 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13562 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13563 | if (temp == NULL) |
| 13564 | goto onError; |
| 13565 | pbuf = PyUnicode_DATA(temp); |
| 13566 | kind = PyUnicode_KIND(temp); |
| 13567 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13569 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13570 | |
| 13571 | default: |
| 13572 | PyErr_Format(PyExc_ValueError, |
| 13573 | "unsupported format character '%c' (0x%x) " |
| 13574 | "at index %zd", |
| 13575 | (31<=c && c<=126) ? (char)c : '?', |
| 13576 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13577 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13578 | goto onError; |
| 13579 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13580 | /* pbuf is initialized here. */ |
| 13581 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13583 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13584 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13585 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13586 | pindex++; |
| 13587 | } |
| 13588 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13589 | signobj = plus; |
| 13590 | len--; |
| 13591 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13592 | } |
| 13593 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13594 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13595 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13596 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13597 | else |
| 13598 | sign = 0; |
| 13599 | } |
| 13600 | if (width < len) |
| 13601 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13603 | if (fill != ' ') { |
| 13604 | assert(signobj != NULL); |
| 13605 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13606 | goto onError; |
| 13607 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13608 | if (width > len) |
| 13609 | width--; |
| 13610 | } |
| 13611 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13612 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13613 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13614 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13615 | second = get_latin1_char( |
| 13616 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13617 | pindex += 2; |
| 13618 | if (second == NULL || |
| 13619 | _PyAccu_Accumulate(&acc, zero) || |
| 13620 | _PyAccu_Accumulate(&acc, second)) |
| 13621 | goto onError; |
| 13622 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13623 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13624 | width -= 2; |
| 13625 | if (width < 0) |
| 13626 | width = 0; |
| 13627 | len -= 2; |
| 13628 | } |
| 13629 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13630 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13631 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13632 | goto onError; |
| 13633 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13634 | } |
| 13635 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13636 | if (sign) { |
| 13637 | assert(signobj != NULL); |
| 13638 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13639 | goto onError; |
| 13640 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13641 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13642 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13643 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13644 | second = get_latin1_char( |
| 13645 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13646 | pindex += 2; |
| 13647 | if (second == NULL || |
| 13648 | _PyAccu_Accumulate(&acc, zero) || |
| 13649 | _PyAccu_Accumulate(&acc, second)) |
| 13650 | goto onError; |
| 13651 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13652 | } |
| 13653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13654 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13655 | if (temp != NULL) { |
| 13656 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13657 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13658 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13659 | else { |
| 13660 | const char *p = (const char *) pbuf; |
| 13661 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13662 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13663 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13664 | } |
| 13665 | if (v == NULL) |
| 13666 | goto onError; |
| 13667 | r = _PyAccu_Accumulate(&acc, v); |
| 13668 | Py_DECREF(v); |
| 13669 | if (r) |
| 13670 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13671 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13672 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13673 | if (dict && (argidx < arglen) && c != '%') { |
| 13674 | PyErr_SetString(PyExc_TypeError, |
| 13675 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13676 | goto onError; |
| 13677 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13678 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13679 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13680 | } /* until end */ |
| 13681 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13682 | PyErr_SetString(PyExc_TypeError, |
| 13683 | "not all arguments converted during string formatting"); |
| 13684 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13685 | } |
| 13686 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13687 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13688 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13689 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13690 | } |
| 13691 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13692 | Py_XDECREF(temp); |
| 13693 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13694 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13695 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13696 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13697 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13698 | Py_XDECREF(temp); |
| 13699 | Py_XDECREF(second); |
| 13700 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13701 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13702 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13703 | } |
| 13704 | return NULL; |
| 13705 | } |
| 13706 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13707 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13708 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13709 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13710 | static PyObject * |
| 13711 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13712 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13713 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13714 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13715 | char *encoding = NULL; |
| 13716 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13717 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13718 | if (type != &PyUnicode_Type) |
| 13719 | return unicode_subtype_new(type, args, kwds); |
| 13720 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13721 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13722 | return NULL; |
| 13723 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13724 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13725 | if (encoding == NULL && errors == NULL) |
| 13726 | return PyObject_Str(x); |
| 13727 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13728 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13729 | } |
| 13730 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13731 | static PyObject * |
| 13732 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13733 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13734 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13735 | Py_ssize_t length, char_size; |
| 13736 | int share_wstr, share_utf8; |
| 13737 | unsigned int kind; |
| 13738 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13739 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13740 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13741 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13742 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13743 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13744 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13745 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13746 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13747 | return NULL; |
| 13748 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13749 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13750 | if (self == NULL) { |
| 13751 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13752 | return NULL; |
| 13753 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13754 | kind = PyUnicode_KIND(unicode); |
| 13755 | length = PyUnicode_GET_LENGTH(unicode); |
| 13756 | |
| 13757 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13758 | #ifdef Py_DEBUG |
| 13759 | _PyUnicode_HASH(self) = -1; |
| 13760 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13761 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13762 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13763 | _PyUnicode_STATE(self).interned = 0; |
| 13764 | _PyUnicode_STATE(self).kind = kind; |
| 13765 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13766 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13767 | _PyUnicode_STATE(self).ready = 1; |
| 13768 | _PyUnicode_WSTR(self) = NULL; |
| 13769 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13770 | _PyUnicode_UTF8(self) = NULL; |
| 13771 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13772 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13773 | |
| 13774 | share_utf8 = 0; |
| 13775 | share_wstr = 0; |
| 13776 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13777 | char_size = 1; |
| 13778 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13779 | share_utf8 = 1; |
| 13780 | } |
| 13781 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13782 | char_size = 2; |
| 13783 | if (sizeof(wchar_t) == 2) |
| 13784 | share_wstr = 1; |
| 13785 | } |
| 13786 | else { |
| 13787 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13788 | char_size = 4; |
| 13789 | if (sizeof(wchar_t) == 4) |
| 13790 | share_wstr = 1; |
| 13791 | } |
| 13792 | |
| 13793 | /* Ensure we won't overflow the length. */ |
| 13794 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13795 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13796 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13797 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13798 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13799 | if (data == NULL) { |
| 13800 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13801 | goto onError; |
| 13802 | } |
| 13803 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13804 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13805 | if (share_utf8) { |
| 13806 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13807 | _PyUnicode_UTF8(self) = data; |
| 13808 | } |
| 13809 | if (share_wstr) { |
| 13810 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13811 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13812 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13813 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13814 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13815 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13816 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13817 | #ifdef Py_DEBUG |
| 13818 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13819 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13820 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13821 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13822 | |
| 13823 | onError: |
| 13824 | Py_DECREF(unicode); |
| 13825 | Py_DECREF(self); |
| 13826 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13827 | } |
| 13828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13829 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13830 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13831 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13832 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13833 | encoding defaults to the current default string encoding.\n\ |
| 13834 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13835 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13836 | static PyObject *unicode_iter(PyObject *seq); |
| 13837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13838 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13839 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13840 | "str", /* tp_name */ |
| 13841 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13842 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13843 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13844 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13845 | 0, /* tp_print */ |
| 13846 | 0, /* tp_getattr */ |
| 13847 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13848 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13849 | unicode_repr, /* tp_repr */ |
| 13850 | &unicode_as_number, /* tp_as_number */ |
| 13851 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13852 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13853 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13854 | 0, /* tp_call*/ |
| 13855 | (reprfunc) unicode_str, /* tp_str */ |
| 13856 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13857 | 0, /* tp_setattro */ |
| 13858 | 0, /* tp_as_buffer */ |
| 13859 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13860 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13861 | unicode_doc, /* tp_doc */ |
| 13862 | 0, /* tp_traverse */ |
| 13863 | 0, /* tp_clear */ |
| 13864 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13865 | 0, /* tp_weaklistoffset */ |
| 13866 | unicode_iter, /* tp_iter */ |
| 13867 | 0, /* tp_iternext */ |
| 13868 | unicode_methods, /* tp_methods */ |
| 13869 | 0, /* tp_members */ |
| 13870 | 0, /* tp_getset */ |
| 13871 | &PyBaseObject_Type, /* tp_base */ |
| 13872 | 0, /* tp_dict */ |
| 13873 | 0, /* tp_descr_get */ |
| 13874 | 0, /* tp_descr_set */ |
| 13875 | 0, /* tp_dictoffset */ |
| 13876 | 0, /* tp_init */ |
| 13877 | 0, /* tp_alloc */ |
| 13878 | unicode_new, /* tp_new */ |
| 13879 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13880 | }; |
| 13881 | |
| 13882 | /* Initialize the Unicode implementation */ |
| 13883 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13884 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13885 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13886 | int i; |
| 13887 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13888 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13889 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13890 | 0x000A, /* LINE FEED */ |
| 13891 | 0x000D, /* CARRIAGE RETURN */ |
| 13892 | 0x001C, /* FILE SEPARATOR */ |
| 13893 | 0x001D, /* GROUP SEPARATOR */ |
| 13894 | 0x001E, /* RECORD SEPARATOR */ |
| 13895 | 0x0085, /* NEXT LINE */ |
| 13896 | 0x2028, /* LINE SEPARATOR */ |
| 13897 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13898 | }; |
| 13899 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13900 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13901 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13902 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13903 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 13904 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13905 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13906 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13907 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13908 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13909 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13910 | |
| 13911 | /* initialize the linebreak bloom filter */ |
| 13912 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13913 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13914 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13915 | |
| 13916 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13917 | |
| 13918 | #ifdef HAVE_MBCS |
| 13919 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13920 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13921 | PyErr_SetFromWindowsErr(0); |
| 13922 | return -1; |
| 13923 | } |
| 13924 | #endif |
| 13925 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13926 | } |
| 13927 | |
| 13928 | /* Finalize the Unicode implementation */ |
| 13929 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13930 | int |
| 13931 | PyUnicode_ClearFreeList(void) |
| 13932 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13933 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13934 | } |
| 13935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13936 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13937 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13938 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13939 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13940 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13941 | Py_XDECREF(unicode_empty); |
| 13942 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13943 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13944 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13945 | if (unicode_latin1[i]) { |
| 13946 | Py_DECREF(unicode_latin1[i]); |
| 13947 | unicode_latin1[i] = NULL; |
| 13948 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13949 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13950 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13951 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13952 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13953 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13954 | void |
| 13955 | PyUnicode_InternInPlace(PyObject **p) |
| 13956 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13957 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13958 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13959 | #ifdef Py_DEBUG |
| 13960 | assert(s != NULL); |
| 13961 | assert(_PyUnicode_CHECK(s)); |
| 13962 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13963 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13964 | return; |
| 13965 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13966 | /* If it's a subclass, we don't really know what putting |
| 13967 | it in the interned dict might do. */ |
| 13968 | if (!PyUnicode_CheckExact(s)) |
| 13969 | return; |
| 13970 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13971 | return; |
| 13972 | if (interned == NULL) { |
| 13973 | interned = PyDict_New(); |
| 13974 | if (interned == NULL) { |
| 13975 | PyErr_Clear(); /* Don't leave an exception */ |
| 13976 | return; |
| 13977 | } |
| 13978 | } |
| 13979 | /* It might be that the GetItem call fails even |
| 13980 | though the key is present in the dictionary, |
| 13981 | namely when this happens during a stack overflow. */ |
| 13982 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13983 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13984 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13986 | if (t) { |
| 13987 | Py_INCREF(t); |
| 13988 | Py_DECREF(*p); |
| 13989 | *p = t; |
| 13990 | return; |
| 13991 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13992 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13993 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13994 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13995 | PyErr_Clear(); |
| 13996 | PyThreadState_GET()->recursion_critical = 0; |
| 13997 | return; |
| 13998 | } |
| 13999 | PyThreadState_GET()->recursion_critical = 0; |
| 14000 | /* The two references in interned are not counted by refcnt. |
| 14001 | The deallocator will take care of this */ |
| 14002 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14003 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14004 | } |
| 14005 | |
| 14006 | void |
| 14007 | PyUnicode_InternImmortal(PyObject **p) |
| 14008 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14009 | PyUnicode_InternInPlace(p); |
| 14010 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14011 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14012 | Py_INCREF(*p); |
| 14013 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14014 | } |
| 14015 | |
| 14016 | PyObject * |
| 14017 | PyUnicode_InternFromString(const char *cp) |
| 14018 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14019 | PyObject *s = PyUnicode_FromString(cp); |
| 14020 | if (s == NULL) |
| 14021 | return NULL; |
| 14022 | PyUnicode_InternInPlace(&s); |
| 14023 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14024 | } |
| 14025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14026 | void |
| 14027 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14028 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14029 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14030 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14031 | Py_ssize_t i, n; |
| 14032 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14033 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14034 | if (interned == NULL || !PyDict_Check(interned)) |
| 14035 | return; |
| 14036 | keys = PyDict_Keys(interned); |
| 14037 | if (keys == NULL || !PyList_Check(keys)) { |
| 14038 | PyErr_Clear(); |
| 14039 | return; |
| 14040 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14041 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14043 | detector, interned unicode strings are not forcibly deallocated; |
| 14044 | rather, we give them their stolen references back, and then clear |
| 14045 | and DECREF the interned dict. */ |
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 | n = PyList_GET_SIZE(keys); |
| 14048 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14049 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14050 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14051 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14052 | if (PyUnicode_READY(s) == -1) { |
| 14053 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14054 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14056 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | case SSTATE_NOT_INTERNED: |
| 14058 | /* XXX Shouldn't happen */ |
| 14059 | break; |
| 14060 | case SSTATE_INTERNED_IMMORTAL: |
| 14061 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14062 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14063 | break; |
| 14064 | case SSTATE_INTERNED_MORTAL: |
| 14065 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14066 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14067 | break; |
| 14068 | default: |
| 14069 | Py_FatalError("Inconsistent interned string state."); |
| 14070 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14071 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14072 | } |
| 14073 | fprintf(stderr, "total size of all interned strings: " |
| 14074 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14075 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14076 | Py_DECREF(keys); |
| 14077 | PyDict_Clear(interned); |
| 14078 | Py_DECREF(interned); |
| 14079 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14080 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14081 | |
| 14082 | |
| 14083 | /********************* Unicode Iterator **************************/ |
| 14084 | |
| 14085 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14086 | PyObject_HEAD |
| 14087 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14088 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14089 | } unicodeiterobject; |
| 14090 | |
| 14091 | static void |
| 14092 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14093 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14094 | _PyObject_GC_UNTRACK(it); |
| 14095 | Py_XDECREF(it->it_seq); |
| 14096 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14097 | } |
| 14098 | |
| 14099 | static int |
| 14100 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14101 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14102 | Py_VISIT(it->it_seq); |
| 14103 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14104 | } |
| 14105 | |
| 14106 | static PyObject * |
| 14107 | unicodeiter_next(unicodeiterobject *it) |
| 14108 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14109 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14110 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14111 | assert(it != NULL); |
| 14112 | seq = it->it_seq; |
| 14113 | if (seq == NULL) |
| 14114 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14115 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14117 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14118 | int kind = PyUnicode_KIND(seq); |
| 14119 | void *data = PyUnicode_DATA(seq); |
| 14120 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14121 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14122 | if (item != NULL) |
| 14123 | ++it->it_index; |
| 14124 | return item; |
| 14125 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14126 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14127 | Py_DECREF(seq); |
| 14128 | it->it_seq = NULL; |
| 14129 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14130 | } |
| 14131 | |
| 14132 | static PyObject * |
| 14133 | unicodeiter_len(unicodeiterobject *it) |
| 14134 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14135 | Py_ssize_t len = 0; |
| 14136 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14137 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14138 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14139 | } |
| 14140 | |
| 14141 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14142 | |
| 14143 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14144 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14145 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14146 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14147 | }; |
| 14148 | |
| 14149 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14150 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14151 | "str_iterator", /* tp_name */ |
| 14152 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14153 | 0, /* tp_itemsize */ |
| 14154 | /* methods */ |
| 14155 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14156 | 0, /* tp_print */ |
| 14157 | 0, /* tp_getattr */ |
| 14158 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14159 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14160 | 0, /* tp_repr */ |
| 14161 | 0, /* tp_as_number */ |
| 14162 | 0, /* tp_as_sequence */ |
| 14163 | 0, /* tp_as_mapping */ |
| 14164 | 0, /* tp_hash */ |
| 14165 | 0, /* tp_call */ |
| 14166 | 0, /* tp_str */ |
| 14167 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14168 | 0, /* tp_setattro */ |
| 14169 | 0, /* tp_as_buffer */ |
| 14170 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14171 | 0, /* tp_doc */ |
| 14172 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14173 | 0, /* tp_clear */ |
| 14174 | 0, /* tp_richcompare */ |
| 14175 | 0, /* tp_weaklistoffset */ |
| 14176 | PyObject_SelfIter, /* tp_iter */ |
| 14177 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14178 | unicodeiter_methods, /* tp_methods */ |
| 14179 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14180 | }; |
| 14181 | |
| 14182 | static PyObject * |
| 14183 | unicode_iter(PyObject *seq) |
| 14184 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14185 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14186 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14187 | if (!PyUnicode_Check(seq)) { |
| 14188 | PyErr_BadInternalCall(); |
| 14189 | return NULL; |
| 14190 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14191 | if (PyUnicode_READY(seq) == -1) |
| 14192 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14193 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14194 | if (it == NULL) |
| 14195 | return NULL; |
| 14196 | it->it_index = 0; |
| 14197 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14198 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14199 | _PyObject_GC_TRACK(it); |
| 14200 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14201 | } |
| 14202 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14203 | |
| 14204 | size_t |
| 14205 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14206 | { |
| 14207 | int res = 0; |
| 14208 | while(*u++) |
| 14209 | res++; |
| 14210 | return res; |
| 14211 | } |
| 14212 | |
| 14213 | Py_UNICODE* |
| 14214 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14215 | { |
| 14216 | Py_UNICODE *u = s1; |
| 14217 | while ((*u++ = *s2++)); |
| 14218 | return s1; |
| 14219 | } |
| 14220 | |
| 14221 | Py_UNICODE* |
| 14222 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14223 | { |
| 14224 | Py_UNICODE *u = s1; |
| 14225 | while ((*u++ = *s2++)) |
| 14226 | if (n-- == 0) |
| 14227 | break; |
| 14228 | return s1; |
| 14229 | } |
| 14230 | |
| 14231 | Py_UNICODE* |
| 14232 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14233 | { |
| 14234 | Py_UNICODE *u1 = s1; |
| 14235 | u1 += Py_UNICODE_strlen(u1); |
| 14236 | Py_UNICODE_strcpy(u1, s2); |
| 14237 | return s1; |
| 14238 | } |
| 14239 | |
| 14240 | int |
| 14241 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14242 | { |
| 14243 | while (*s1 && *s2 && *s1 == *s2) |
| 14244 | s1++, s2++; |
| 14245 | if (*s1 && *s2) |
| 14246 | return (*s1 < *s2) ? -1 : +1; |
| 14247 | if (*s1) |
| 14248 | return 1; |
| 14249 | if (*s2) |
| 14250 | return -1; |
| 14251 | return 0; |
| 14252 | } |
| 14253 | |
| 14254 | int |
| 14255 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14256 | { |
| 14257 | register Py_UNICODE u1, u2; |
| 14258 | for (; n != 0; n--) { |
| 14259 | u1 = *s1; |
| 14260 | u2 = *s2; |
| 14261 | if (u1 != u2) |
| 14262 | return (u1 < u2) ? -1 : +1; |
| 14263 | if (u1 == '\0') |
| 14264 | return 0; |
| 14265 | s1++; |
| 14266 | s2++; |
| 14267 | } |
| 14268 | return 0; |
| 14269 | } |
| 14270 | |
| 14271 | Py_UNICODE* |
| 14272 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14273 | { |
| 14274 | const Py_UNICODE *p; |
| 14275 | for (p = s; *p; p++) |
| 14276 | if (*p == c) |
| 14277 | return (Py_UNICODE*)p; |
| 14278 | return NULL; |
| 14279 | } |
| 14280 | |
| 14281 | Py_UNICODE* |
| 14282 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14283 | { |
| 14284 | const Py_UNICODE *p; |
| 14285 | p = s + Py_UNICODE_strlen(s); |
| 14286 | while (p != s) { |
| 14287 | p--; |
| 14288 | if (*p == c) |
| 14289 | return (Py_UNICODE*)p; |
| 14290 | } |
| 14291 | return NULL; |
| 14292 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14293 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14294 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14295 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14296 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14297 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14298 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14300 | if (!PyUnicode_Check(unicode)) { |
| 14301 | PyErr_BadArgument(); |
| 14302 | return NULL; |
| 14303 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14304 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14305 | if (u == NULL) |
| 14306 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14307 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14308 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14309 | PyErr_NoMemory(); |
| 14310 | return NULL; |
| 14311 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14312 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14313 | size *= sizeof(Py_UNICODE); |
| 14314 | copy = PyMem_Malloc(size); |
| 14315 | if (copy == NULL) { |
| 14316 | PyErr_NoMemory(); |
| 14317 | return NULL; |
| 14318 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14319 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14320 | return copy; |
| 14321 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14322 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14323 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14324 | to the string.Formatter class implemented in Python. */ |
| 14325 | |
| 14326 | static PyMethodDef _string_methods[] = { |
| 14327 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14328 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14329 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14330 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14331 | {NULL, NULL} |
| 14332 | }; |
| 14333 | |
| 14334 | static struct PyModuleDef _string_module = { |
| 14335 | PyModuleDef_HEAD_INIT, |
| 14336 | "_string", |
| 14337 | PyDoc_STR("string helper module"), |
| 14338 | 0, |
| 14339 | _string_methods, |
| 14340 | NULL, |
| 14341 | NULL, |
| 14342 | NULL, |
| 14343 | NULL |
| 14344 | }; |
| 14345 | |
| 14346 | PyMODINIT_FUNC |
| 14347 | PyInit__string(void) |
| 14348 | { |
| 14349 | return PyModule_Create(&_string_module); |
| 14350 | } |
| 14351 | |
| 14352 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14353 | #ifdef __cplusplus |
| 14354 | } |
| 14355 | #endif |