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 | { |
| 1909 | switch(kind) { |
| 1910 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1911 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1912 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1913 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1914 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1915 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1916 | default: |
| 1917 | assert(0 && "invalid kind"); |
| 1918 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1919 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1920 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1923 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1924 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1925 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1926 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1927 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1928 | { |
| 1929 | PyObject *unicode, *copy; |
| 1930 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1931 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1932 | unsigned int kind; |
| 1933 | |
| 1934 | assert(p_unicode != NULL); |
| 1935 | unicode = *p_unicode; |
| 1936 | assert(PyUnicode_IS_READY(unicode)); |
| 1937 | if (PyUnicode_IS_ASCII(unicode)) |
| 1938 | return; |
| 1939 | |
| 1940 | len = PyUnicode_GET_LENGTH(unicode); |
| 1941 | kind = PyUnicode_KIND(unicode); |
| 1942 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1943 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1944 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1945 | if (max_char >= 128) |
| 1946 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1947 | } |
| 1948 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1949 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1950 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1951 | if (max_char >= 256) |
| 1952 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1953 | } |
| 1954 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1955 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1956 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1957 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1958 | if (max_char >= 0x10000) |
| 1959 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1960 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1961 | copy = PyUnicode_New(len, max_char); |
| 1962 | copy_characters(copy, 0, unicode, 0, len); |
| 1963 | Py_DECREF(unicode); |
| 1964 | *p_unicode = copy; |
| 1965 | } |
| 1966 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1967 | PyObject* |
| 1968 | PyUnicode_Copy(PyObject *unicode) |
| 1969 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1970 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1971 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1972 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1973 | if (!PyUnicode_Check(unicode)) { |
| 1974 | PyErr_BadInternalCall(); |
| 1975 | return NULL; |
| 1976 | } |
| 1977 | if (PyUnicode_READY(unicode)) |
| 1978 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1979 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1980 | length = PyUnicode_GET_LENGTH(unicode); |
| 1981 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1982 | if (!copy) |
| 1983 | return NULL; |
| 1984 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1985 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 1986 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 1987 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1988 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1989 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1990 | } |
| 1991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1993 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1994 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | |
| 1996 | void* |
| 1997 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1998 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1999 | Py_ssize_t len; |
| 2000 | void *result; |
| 2001 | unsigned int skind; |
| 2002 | |
| 2003 | if (PyUnicode_READY(s)) |
| 2004 | return NULL; |
| 2005 | |
| 2006 | len = PyUnicode_GET_LENGTH(s); |
| 2007 | skind = PyUnicode_KIND(s); |
| 2008 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2009 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | return NULL; |
| 2011 | } |
| 2012 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2013 | case PyUnicode_2BYTE_KIND: |
| 2014 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2015 | if (!result) |
| 2016 | return PyErr_NoMemory(); |
| 2017 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2018 | _PyUnicode_CONVERT_BYTES( |
| 2019 | Py_UCS1, Py_UCS2, |
| 2020 | PyUnicode_1BYTE_DATA(s), |
| 2021 | PyUnicode_1BYTE_DATA(s) + len, |
| 2022 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2023 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2024 | case PyUnicode_4BYTE_KIND: |
| 2025 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2026 | if (!result) |
| 2027 | return PyErr_NoMemory(); |
| 2028 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2029 | _PyUnicode_CONVERT_BYTES( |
| 2030 | Py_UCS2, Py_UCS4, |
| 2031 | PyUnicode_2BYTE_DATA(s), |
| 2032 | PyUnicode_2BYTE_DATA(s) + len, |
| 2033 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2034 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2035 | else { |
| 2036 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2037 | _PyUnicode_CONVERT_BYTES( |
| 2038 | Py_UCS1, Py_UCS4, |
| 2039 | PyUnicode_1BYTE_DATA(s), |
| 2040 | PyUnicode_1BYTE_DATA(s) + len, |
| 2041 | result); |
| 2042 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2043 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2044 | default: |
| 2045 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2046 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2047 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2048 | return NULL; |
| 2049 | } |
| 2050 | |
| 2051 | static Py_UCS4* |
| 2052 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2053 | int copy_null) |
| 2054 | { |
| 2055 | int kind; |
| 2056 | void *data; |
| 2057 | Py_ssize_t len, targetlen; |
| 2058 | if (PyUnicode_READY(string) == -1) |
| 2059 | return NULL; |
| 2060 | kind = PyUnicode_KIND(string); |
| 2061 | data = PyUnicode_DATA(string); |
| 2062 | len = PyUnicode_GET_LENGTH(string); |
| 2063 | targetlen = len; |
| 2064 | if (copy_null) |
| 2065 | targetlen++; |
| 2066 | if (!target) { |
| 2067 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2068 | PyErr_NoMemory(); |
| 2069 | return NULL; |
| 2070 | } |
| 2071 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2072 | if (!target) { |
| 2073 | PyErr_NoMemory(); |
| 2074 | return NULL; |
| 2075 | } |
| 2076 | } |
| 2077 | else { |
| 2078 | if (targetsize < targetlen) { |
| 2079 | PyErr_Format(PyExc_SystemError, |
| 2080 | "string is longer than the buffer"); |
| 2081 | if (copy_null && 0 < targetsize) |
| 2082 | target[0] = 0; |
| 2083 | return NULL; |
| 2084 | } |
| 2085 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2086 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2087 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2088 | _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] | 2089 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2090 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2091 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2092 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2093 | } |
| 2094 | else { |
| 2095 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2096 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2097 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2098 | if (copy_null) |
| 2099 | target[len] = 0; |
| 2100 | return target; |
| 2101 | } |
| 2102 | |
| 2103 | Py_UCS4* |
| 2104 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2105 | int copy_null) |
| 2106 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2107 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2108 | PyErr_BadInternalCall(); |
| 2109 | return NULL; |
| 2110 | } |
| 2111 | return as_ucs4(string, target, targetsize, copy_null); |
| 2112 | } |
| 2113 | |
| 2114 | Py_UCS4* |
| 2115 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2116 | { |
| 2117 | return as_ucs4(string, NULL, 0, 1); |
| 2118 | } |
| 2119 | |
| 2120 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2121 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2122 | PyObject * |
| 2123 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2126 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2127 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2128 | PyErr_BadInternalCall(); |
| 2129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2132 | if (size == -1) { |
| 2133 | size = wcslen(w); |
| 2134 | } |
| 2135 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2136 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2139 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2140 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2141 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2142 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2143 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2144 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2145 | *fmt++ = '%'; |
| 2146 | if (width) { |
| 2147 | if (zeropad) |
| 2148 | *fmt++ = '0'; |
| 2149 | fmt += sprintf(fmt, "%d", width); |
| 2150 | } |
| 2151 | if (precision) |
| 2152 | fmt += sprintf(fmt, ".%d", precision); |
| 2153 | if (longflag) |
| 2154 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2155 | else if (longlongflag) { |
| 2156 | /* longlongflag should only ever be nonzero on machines with |
| 2157 | HAVE_LONG_LONG defined */ |
| 2158 | #ifdef HAVE_LONG_LONG |
| 2159 | char *f = PY_FORMAT_LONG_LONG; |
| 2160 | while (*f) |
| 2161 | *fmt++ = *f++; |
| 2162 | #else |
| 2163 | /* we shouldn't ever get here */ |
| 2164 | assert(0); |
| 2165 | *fmt++ = 'l'; |
| 2166 | #endif |
| 2167 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2168 | else if (size_tflag) { |
| 2169 | char *f = PY_FORMAT_SIZE_T; |
| 2170 | while (*f) |
| 2171 | *fmt++ = *f++; |
| 2172 | } |
| 2173 | *fmt++ = c; |
| 2174 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2177 | /* helper for PyUnicode_FromFormatV() */ |
| 2178 | |
| 2179 | static const char* |
| 2180 | parse_format_flags(const char *f, |
| 2181 | int *p_width, int *p_precision, |
| 2182 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2183 | { |
| 2184 | int width, precision, longflag, longlongflag, size_tflag; |
| 2185 | |
| 2186 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2187 | f++; |
| 2188 | width = 0; |
| 2189 | while (Py_ISDIGIT((unsigned)*f)) |
| 2190 | width = (width*10) + *f++ - '0'; |
| 2191 | precision = 0; |
| 2192 | if (*f == '.') { |
| 2193 | f++; |
| 2194 | while (Py_ISDIGIT((unsigned)*f)) |
| 2195 | precision = (precision*10) + *f++ - '0'; |
| 2196 | if (*f == '%') { |
| 2197 | /* "%.3%s" => f points to "3" */ |
| 2198 | f--; |
| 2199 | } |
| 2200 | } |
| 2201 | if (*f == '\0') { |
| 2202 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2203 | f--; |
| 2204 | } |
| 2205 | if (p_width != NULL) |
| 2206 | *p_width = width; |
| 2207 | if (p_precision != NULL) |
| 2208 | *p_precision = precision; |
| 2209 | |
| 2210 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2211 | longflag = 0; |
| 2212 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2213 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2214 | |
| 2215 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2216 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2217 | longflag = 1; |
| 2218 | ++f; |
| 2219 | } |
| 2220 | #ifdef HAVE_LONG_LONG |
| 2221 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2222 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2223 | longlongflag = 1; |
| 2224 | f += 2; |
| 2225 | } |
| 2226 | #endif |
| 2227 | } |
| 2228 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2229 | 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] | 2230 | size_tflag = 1; |
| 2231 | ++f; |
| 2232 | } |
| 2233 | if (p_longflag != NULL) |
| 2234 | *p_longflag = longflag; |
| 2235 | if (p_longlongflag != NULL) |
| 2236 | *p_longlongflag = longlongflag; |
| 2237 | if (p_size_tflag != NULL) |
| 2238 | *p_size_tflag = size_tflag; |
| 2239 | return f; |
| 2240 | } |
| 2241 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2242 | /* maximum number of characters required for output of %ld. 21 characters |
| 2243 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2244 | #define MAX_LONG_CHARS 21 |
| 2245 | /* maximum number of characters required for output of %lld. |
| 2246 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2247 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2248 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2249 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2250 | PyObject * |
| 2251 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2252 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2253 | va_list count; |
| 2254 | Py_ssize_t callcount = 0; |
| 2255 | PyObject **callresults = NULL; |
| 2256 | PyObject **callresult = NULL; |
| 2257 | Py_ssize_t n = 0; |
| 2258 | int width = 0; |
| 2259 | int precision = 0; |
| 2260 | int zeropad; |
| 2261 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2262 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2263 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2264 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2265 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2266 | Py_UCS4 argmaxchar; |
| 2267 | Py_ssize_t numbersize = 0; |
| 2268 | char *numberresults = NULL; |
| 2269 | char *numberresult = NULL; |
| 2270 | Py_ssize_t i; |
| 2271 | int kind; |
| 2272 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2273 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2274 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2275 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2276 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2277 | * 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] | 2278 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2279 | * also estimate a upper bound for all the number formats in the string, |
| 2280 | * 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] | 2281 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2282 | for (f = format; *f; f++) { |
| 2283 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2284 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2285 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2286 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2287 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2288 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2290 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2291 | #ifdef HAVE_LONG_LONG |
| 2292 | if (longlongflag) { |
| 2293 | if (width < MAX_LONG_LONG_CHARS) |
| 2294 | width = MAX_LONG_LONG_CHARS; |
| 2295 | } |
| 2296 | else |
| 2297 | #endif |
| 2298 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2299 | including sign. Decimal takes the most space. This |
| 2300 | isn't enough for octal. If a width is specified we |
| 2301 | need more (which we allocate later). */ |
| 2302 | if (width < MAX_LONG_CHARS) |
| 2303 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2304 | |
| 2305 | /* account for the size + '\0' to separate numbers |
| 2306 | inside of the numberresults buffer */ |
| 2307 | numbersize += (width + 1); |
| 2308 | } |
| 2309 | } |
| 2310 | else if ((unsigned char)*f > 127) { |
| 2311 | PyErr_Format(PyExc_ValueError, |
| 2312 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2313 | "string, got a non-ASCII byte: 0x%02x", |
| 2314 | (unsigned char)*f); |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | } |
| 2318 | /* step 2: allocate memory for the results of |
| 2319 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2320 | if (callcount) { |
| 2321 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2322 | if (!callresults) { |
| 2323 | PyErr_NoMemory(); |
| 2324 | return NULL; |
| 2325 | } |
| 2326 | callresult = callresults; |
| 2327 | } |
| 2328 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2329 | if (numbersize) { |
| 2330 | numberresults = PyObject_Malloc(numbersize); |
| 2331 | if (!numberresults) { |
| 2332 | PyErr_NoMemory(); |
| 2333 | goto fail; |
| 2334 | } |
| 2335 | numberresult = numberresults; |
| 2336 | } |
| 2337 | |
| 2338 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2339 | for (f = format; *f; f++) { |
| 2340 | if (*f == '%') { |
| 2341 | const char* p; |
| 2342 | int longflag; |
| 2343 | int longlongflag; |
| 2344 | int size_tflag; |
| 2345 | int numprinted; |
| 2346 | |
| 2347 | p = f; |
| 2348 | zeropad = (f[1] == '0'); |
| 2349 | f = parse_format_flags(f, &width, &precision, |
| 2350 | &longflag, &longlongflag, &size_tflag); |
| 2351 | switch (*f) { |
| 2352 | case 'c': |
| 2353 | { |
| 2354 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2355 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2356 | n++; |
| 2357 | break; |
| 2358 | } |
| 2359 | case '%': |
| 2360 | n++; |
| 2361 | break; |
| 2362 | case 'i': |
| 2363 | case 'd': |
| 2364 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2365 | width, precision, *f); |
| 2366 | if (longflag) |
| 2367 | numprinted = sprintf(numberresult, fmt, |
| 2368 | va_arg(count, long)); |
| 2369 | #ifdef HAVE_LONG_LONG |
| 2370 | else if (longlongflag) |
| 2371 | numprinted = sprintf(numberresult, fmt, |
| 2372 | va_arg(count, PY_LONG_LONG)); |
| 2373 | #endif |
| 2374 | else if (size_tflag) |
| 2375 | numprinted = sprintf(numberresult, fmt, |
| 2376 | va_arg(count, Py_ssize_t)); |
| 2377 | else |
| 2378 | numprinted = sprintf(numberresult, fmt, |
| 2379 | va_arg(count, int)); |
| 2380 | n += numprinted; |
| 2381 | /* advance by +1 to skip over the '\0' */ |
| 2382 | numberresult += (numprinted + 1); |
| 2383 | assert(*(numberresult - 1) == '\0'); |
| 2384 | assert(*(numberresult - 2) != '\0'); |
| 2385 | assert(numprinted >= 0); |
| 2386 | assert(numberresult <= numberresults + numbersize); |
| 2387 | break; |
| 2388 | case 'u': |
| 2389 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2390 | width, precision, 'u'); |
| 2391 | if (longflag) |
| 2392 | numprinted = sprintf(numberresult, fmt, |
| 2393 | va_arg(count, unsigned long)); |
| 2394 | #ifdef HAVE_LONG_LONG |
| 2395 | else if (longlongflag) |
| 2396 | numprinted = sprintf(numberresult, fmt, |
| 2397 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2398 | #endif |
| 2399 | else if (size_tflag) |
| 2400 | numprinted = sprintf(numberresult, fmt, |
| 2401 | va_arg(count, size_t)); |
| 2402 | else |
| 2403 | numprinted = sprintf(numberresult, fmt, |
| 2404 | va_arg(count, unsigned int)); |
| 2405 | n += numprinted; |
| 2406 | numberresult += (numprinted + 1); |
| 2407 | assert(*(numberresult - 1) == '\0'); |
| 2408 | assert(*(numberresult - 2) != '\0'); |
| 2409 | assert(numprinted >= 0); |
| 2410 | assert(numberresult <= numberresults + numbersize); |
| 2411 | break; |
| 2412 | case 'x': |
| 2413 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2414 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2415 | n += numprinted; |
| 2416 | numberresult += (numprinted + 1); |
| 2417 | assert(*(numberresult - 1) == '\0'); |
| 2418 | assert(*(numberresult - 2) != '\0'); |
| 2419 | assert(numprinted >= 0); |
| 2420 | assert(numberresult <= numberresults + numbersize); |
| 2421 | break; |
| 2422 | case 'p': |
| 2423 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2424 | /* %p is ill-defined: ensure leading 0x. */ |
| 2425 | if (numberresult[1] == 'X') |
| 2426 | numberresult[1] = 'x'; |
| 2427 | else if (numberresult[1] != 'x') { |
| 2428 | memmove(numberresult + 2, numberresult, |
| 2429 | strlen(numberresult) + 1); |
| 2430 | numberresult[0] = '0'; |
| 2431 | numberresult[1] = 'x'; |
| 2432 | numprinted += 2; |
| 2433 | } |
| 2434 | n += numprinted; |
| 2435 | numberresult += (numprinted + 1); |
| 2436 | assert(*(numberresult - 1) == '\0'); |
| 2437 | assert(*(numberresult - 2) != '\0'); |
| 2438 | assert(numprinted >= 0); |
| 2439 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | case 's': |
| 2442 | { |
| 2443 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2444 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2445 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2446 | if (!str) |
| 2447 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2449 | unicode objects, there is no need to call ready on them */ |
| 2450 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2451 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2453 | /* Remember the str and switch to the next slot */ |
| 2454 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2455 | break; |
| 2456 | } |
| 2457 | case 'U': |
| 2458 | { |
| 2459 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2460 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2461 | if (PyUnicode_READY(obj) == -1) |
| 2462 | goto fail; |
| 2463 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2464 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2465 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | case 'V': |
| 2469 | { |
| 2470 | PyObject *obj = va_arg(count, PyObject *); |
| 2471 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2472 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2473 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2474 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2475 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2476 | if (PyUnicode_READY(obj) == -1) |
| 2477 | goto fail; |
| 2478 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2479 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2480 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2481 | *callresult++ = NULL; |
| 2482 | } |
| 2483 | else { |
| 2484 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2485 | if (!str_obj) |
| 2486 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2487 | if (PyUnicode_READY(str_obj)) { |
| 2488 | Py_DECREF(str_obj); |
| 2489 | goto fail; |
| 2490 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2491 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2492 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2493 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2494 | *callresult++ = str_obj; |
| 2495 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | case 'S': |
| 2499 | { |
| 2500 | PyObject *obj = va_arg(count, PyObject *); |
| 2501 | PyObject *str; |
| 2502 | assert(obj); |
| 2503 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2504 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2505 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2506 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2507 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2508 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2509 | /* Remember the str and switch to the next slot */ |
| 2510 | *callresult++ = str; |
| 2511 | break; |
| 2512 | } |
| 2513 | case 'R': |
| 2514 | { |
| 2515 | PyObject *obj = va_arg(count, PyObject *); |
| 2516 | PyObject *repr; |
| 2517 | assert(obj); |
| 2518 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2519 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2520 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2521 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2522 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2523 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2524 | /* Remember the repr and switch to the next slot */ |
| 2525 | *callresult++ = repr; |
| 2526 | break; |
| 2527 | } |
| 2528 | case 'A': |
| 2529 | { |
| 2530 | PyObject *obj = va_arg(count, PyObject *); |
| 2531 | PyObject *ascii; |
| 2532 | assert(obj); |
| 2533 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2534 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2535 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2536 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2537 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2538 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | /* Remember the repr and switch to the next slot */ |
| 2540 | *callresult++ = ascii; |
| 2541 | break; |
| 2542 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2543 | default: |
| 2544 | /* if we stumble upon an unknown |
| 2545 | formatting code, copy the rest of |
| 2546 | the format string to the output |
| 2547 | string. (we cannot just skip the |
| 2548 | code, since there's no way to know |
| 2549 | what's in the argument list) */ |
| 2550 | n += strlen(p); |
| 2551 | goto expand; |
| 2552 | } |
| 2553 | } else |
| 2554 | n++; |
| 2555 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2556 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2557 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2558 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | we don't have to resize the string. |
| 2560 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2561 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2562 | if (!string) |
| 2563 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2564 | kind = PyUnicode_KIND(string); |
| 2565 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2566 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2567 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2569 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2570 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2571 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2572 | |
| 2573 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2574 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2575 | /* checking for == because the last argument could be a empty |
| 2576 | string, which causes i to point to end, the assert at the end of |
| 2577 | the loop */ |
| 2578 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2579 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2580 | switch (*f) { |
| 2581 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2583 | const int ordinal = va_arg(vargs, int); |
| 2584 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2585 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2586 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2587 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2589 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2590 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2591 | case 'p': |
| 2592 | /* unused, since we already have the result */ |
| 2593 | if (*f == 'p') |
| 2594 | (void) va_arg(vargs, void *); |
| 2595 | else |
| 2596 | (void) va_arg(vargs, int); |
| 2597 | /* extract the result from numberresults and append. */ |
| 2598 | for (; *numberresult; ++i, ++numberresult) |
| 2599 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2600 | /* skip over the separating '\0' */ |
| 2601 | assert(*numberresult == '\0'); |
| 2602 | numberresult++; |
| 2603 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | break; |
| 2605 | case 's': |
| 2606 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2607 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2608 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2609 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2610 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2611 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2612 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2613 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2614 | /* We're done with the unicode()/repr() => forget it */ |
| 2615 | Py_DECREF(*callresult); |
| 2616 | /* switch to next unicode()/repr() result */ |
| 2617 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | } |
| 2620 | case 'U': |
| 2621 | { |
| 2622 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2623 | Py_ssize_t size; |
| 2624 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2625 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2626 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2627 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2628 | break; |
| 2629 | } |
| 2630 | case 'V': |
| 2631 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2632 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2634 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2636 | size = PyUnicode_GET_LENGTH(obj); |
| 2637 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2638 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2640 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2641 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2642 | assert(PyUnicode_KIND(*callresult) <= |
| 2643 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2644 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2645 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2646 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2647 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2648 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2649 | break; |
| 2650 | } |
| 2651 | case 'S': |
| 2652 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2653 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2654 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2655 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2656 | /* unused, since we already have the result */ |
| 2657 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2658 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2659 | copy_characters(string, i, *callresult, 0, size); |
| 2660 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2661 | /* We're done with the unicode()/repr() => forget it */ |
| 2662 | Py_DECREF(*callresult); |
| 2663 | /* switch to next unicode()/repr() result */ |
| 2664 | ++callresult; |
| 2665 | break; |
| 2666 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2667 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2668 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2669 | break; |
| 2670 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2671 | for (; *p; ++p, ++i) |
| 2672 | PyUnicode_WRITE(kind, data, i, *p); |
| 2673 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2674 | goto end; |
| 2675 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2676 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2677 | else { |
| 2678 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2679 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2680 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2681 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2682 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2684 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2685 | if (callresults) |
| 2686 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2687 | if (numberresults) |
| 2688 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2689 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2690 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2691 | if (callresults) { |
| 2692 | PyObject **callresult2 = callresults; |
| 2693 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2694 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2695 | ++callresult2; |
| 2696 | } |
| 2697 | PyObject_Free(callresults); |
| 2698 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2699 | if (numberresults) |
| 2700 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2701 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2704 | PyObject * |
| 2705 | PyUnicode_FromFormat(const char *format, ...) |
| 2706 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | PyObject* ret; |
| 2708 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2709 | |
| 2710 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2711 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2712 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2713 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2714 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2715 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2716 | va_end(vargs); |
| 2717 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | #ifdef HAVE_WCHAR_H |
| 2721 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2722 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2723 | convert a Unicode object to a wide character string. |
| 2724 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2725 | - 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] | 2726 | character) required to convert the unicode object. Ignore size argument. |
| 2727 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2728 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2729 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2730 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2731 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2732 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2733 | wchar_t *w, |
| 2734 | Py_ssize_t size) |
| 2735 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2736 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2737 | const wchar_t *wstr; |
| 2738 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2739 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2740 | if (wstr == NULL) |
| 2741 | return -1; |
| 2742 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2743 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2744 | if (size > res) |
| 2745 | size = res + 1; |
| 2746 | else |
| 2747 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2748 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2749 | return res; |
| 2750 | } |
| 2751 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2756 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2757 | wchar_t *w, |
| 2758 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2759 | { |
| 2760 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2761 | PyErr_BadInternalCall(); |
| 2762 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2763 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2764 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2767 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2768 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2769 | Py_ssize_t *size) |
| 2770 | { |
| 2771 | wchar_t* buffer; |
| 2772 | Py_ssize_t buflen; |
| 2773 | |
| 2774 | if (unicode == NULL) { |
| 2775 | PyErr_BadInternalCall(); |
| 2776 | return NULL; |
| 2777 | } |
| 2778 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2779 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | if (buflen == -1) |
| 2781 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2782 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2783 | PyErr_NoMemory(); |
| 2784 | return NULL; |
| 2785 | } |
| 2786 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2787 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2788 | if (buffer == NULL) { |
| 2789 | PyErr_NoMemory(); |
| 2790 | return NULL; |
| 2791 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2792 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2793 | if (buflen == -1) |
| 2794 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2795 | if (size != NULL) |
| 2796 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2797 | return buffer; |
| 2798 | } |
| 2799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2802 | PyObject * |
| 2803 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2804 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2805 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2806 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2807 | PyErr_SetString(PyExc_ValueError, |
| 2808 | "chr() arg not in range(0x110000)"); |
| 2809 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2810 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2812 | if (ordinal < 256) |
| 2813 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2815 | v = PyUnicode_New(1, ordinal); |
| 2816 | if (v == NULL) |
| 2817 | return NULL; |
| 2818 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2819 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2823 | PyObject * |
| 2824 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2825 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2826 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2827 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2828 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2829 | if (PyUnicode_READY(obj)) |
| 2830 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2831 | Py_INCREF(obj); |
| 2832 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2833 | } |
| 2834 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2835 | /* For a Unicode subtype that's not a Unicode object, |
| 2836 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2837 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2838 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2839 | PyErr_Format(PyExc_TypeError, |
| 2840 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2841 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2842 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2845 | PyObject * |
| 2846 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2847 | const char *encoding, |
| 2848 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2849 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2850 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2851 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2854 | PyErr_BadInternalCall(); |
| 2855 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2856 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2857 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2858 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2859 | if (PyBytes_Check(obj)) { |
| 2860 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2861 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2862 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2863 | } |
| 2864 | else { |
| 2865 | v = PyUnicode_Decode( |
| 2866 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2867 | encoding, errors); |
| 2868 | } |
| 2869 | return v; |
| 2870 | } |
| 2871 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2872 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2873 | PyErr_SetString(PyExc_TypeError, |
| 2874 | "decoding str is not supported"); |
| 2875 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2876 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2877 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2878 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2879 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2880 | PyErr_Format(PyExc_TypeError, |
| 2881 | "coercing to str: need bytes, bytearray " |
| 2882 | "or buffer-like object, %.80s found", |
| 2883 | Py_TYPE(obj)->tp_name); |
| 2884 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2885 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2887 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2889 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2890 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2891 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2892 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2893 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2894 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2895 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2898 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2899 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2900 | 1 on success. */ |
| 2901 | static int |
| 2902 | normalize_encoding(const char *encoding, |
| 2903 | char *lower, |
| 2904 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2906 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2907 | char *l; |
| 2908 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2909 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2910 | if (encoding == NULL) { |
| 2911 | strcpy(lower, "utf-8"); |
| 2912 | return 1; |
| 2913 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2914 | e = encoding; |
| 2915 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2916 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2917 | while (*e) { |
| 2918 | if (l == l_end) |
| 2919 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2920 | if (Py_ISUPPER(*e)) { |
| 2921 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2922 | } |
| 2923 | else if (*e == '_') { |
| 2924 | *l++ = '-'; |
| 2925 | e++; |
| 2926 | } |
| 2927 | else { |
| 2928 | *l++ = *e++; |
| 2929 | } |
| 2930 | } |
| 2931 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2932 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2935 | PyObject * |
| 2936 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2937 | Py_ssize_t size, |
| 2938 | const char *encoding, |
| 2939 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2940 | { |
| 2941 | PyObject *buffer = NULL, *unicode; |
| 2942 | Py_buffer info; |
| 2943 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2944 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2945 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2946 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2947 | if ((strcmp(lower, "utf-8") == 0) || |
| 2948 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2949 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2950 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2951 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2952 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2953 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2954 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2955 | else if (strcmp(lower, "mbcs") == 0) |
| 2956 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2957 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2958 | else if (strcmp(lower, "ascii") == 0) |
| 2959 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2960 | else if (strcmp(lower, "utf-16") == 0) |
| 2961 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2962 | else if (strcmp(lower, "utf-32") == 0) |
| 2963 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2964 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | |
| 2966 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2967 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2968 | 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] | 2969 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2970 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2971 | if (buffer == NULL) |
| 2972 | goto onError; |
| 2973 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2974 | if (unicode == NULL) |
| 2975 | goto onError; |
| 2976 | if (!PyUnicode_Check(unicode)) { |
| 2977 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2978 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2979 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2980 | Py_DECREF(unicode); |
| 2981 | goto onError; |
| 2982 | } |
| 2983 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2984 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2986 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2987 | Py_XDECREF(buffer); |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2991 | PyObject * |
| 2992 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2993 | const char *encoding, |
| 2994 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2995 | { |
| 2996 | PyObject *v; |
| 2997 | |
| 2998 | if (!PyUnicode_Check(unicode)) { |
| 2999 | PyErr_BadArgument(); |
| 3000 | goto onError; |
| 3001 | } |
| 3002 | |
| 3003 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3004 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3005 | |
| 3006 | /* Decode via the codec registry */ |
| 3007 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3008 | if (v == NULL) |
| 3009 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3010 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3011 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3012 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3013 | return NULL; |
| 3014 | } |
| 3015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3016 | PyObject * |
| 3017 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3018 | const char *encoding, |
| 3019 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3020 | { |
| 3021 | PyObject *v; |
| 3022 | |
| 3023 | if (!PyUnicode_Check(unicode)) { |
| 3024 | PyErr_BadArgument(); |
| 3025 | goto onError; |
| 3026 | } |
| 3027 | |
| 3028 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3029 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3030 | |
| 3031 | /* Decode via the codec registry */ |
| 3032 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3033 | if (v == NULL) |
| 3034 | goto onError; |
| 3035 | if (!PyUnicode_Check(v)) { |
| 3036 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3037 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3038 | Py_TYPE(v)->tp_name); |
| 3039 | Py_DECREF(v); |
| 3040 | goto onError; |
| 3041 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3042 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3044 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3045 | return NULL; |
| 3046 | } |
| 3047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3048 | PyObject * |
| 3049 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3050 | Py_ssize_t size, |
| 3051 | const char *encoding, |
| 3052 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | { |
| 3054 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | unicode = PyUnicode_FromUnicode(s, size); |
| 3057 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3058 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3060 | Py_DECREF(unicode); |
| 3061 | return v; |
| 3062 | } |
| 3063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3064 | PyObject * |
| 3065 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3066 | const char *encoding, |
| 3067 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3068 | { |
| 3069 | PyObject *v; |
| 3070 | |
| 3071 | if (!PyUnicode_Check(unicode)) { |
| 3072 | PyErr_BadArgument(); |
| 3073 | goto onError; |
| 3074 | } |
| 3075 | |
| 3076 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3077 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3078 | |
| 3079 | /* Encode via the codec registry */ |
| 3080 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3081 | if (v == NULL) |
| 3082 | goto onError; |
| 3083 | return v; |
| 3084 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3085 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3086 | return NULL; |
| 3087 | } |
| 3088 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3089 | PyObject * |
| 3090 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3091 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3092 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3093 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3094 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3095 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3096 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3097 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3098 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3099 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3100 | the Python codec requires to encode at least its own filename. Use the C |
| 3101 | version of the locale codec until the codec registry is initialized and |
| 3102 | the Python codec is loaded. |
| 3103 | |
| 3104 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3105 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3106 | subinterpreters. */ |
| 3107 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3108 | return PyUnicode_AsEncodedString(unicode, |
| 3109 | Py_FileSystemDefaultEncoding, |
| 3110 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3111 | } |
| 3112 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3113 | /* locale encoding with surrogateescape */ |
| 3114 | wchar_t *wchar; |
| 3115 | char *bytes; |
| 3116 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3117 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3118 | |
| 3119 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3120 | if (wchar == NULL) |
| 3121 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3122 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3123 | if (bytes == NULL) { |
| 3124 | if (error_pos != (size_t)-1) { |
| 3125 | char *errmsg = strerror(errno); |
| 3126 | PyObject *exc = NULL; |
| 3127 | if (errmsg == NULL) |
| 3128 | errmsg = "Py_wchar2char() failed"; |
| 3129 | raise_encode_exception(&exc, |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 3130 | "filesystemencoding", unicode, |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3131 | error_pos, error_pos+1, |
| 3132 | errmsg); |
| 3133 | Py_XDECREF(exc); |
| 3134 | } |
| 3135 | else |
| 3136 | PyErr_NoMemory(); |
| 3137 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3138 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3139 | } |
| 3140 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3141 | |
| 3142 | bytes_obj = PyBytes_FromString(bytes); |
| 3143 | PyMem_Free(bytes); |
| 3144 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3145 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3146 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3149 | PyObject * |
| 3150 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3151 | const char *encoding, |
| 3152 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3153 | { |
| 3154 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3155 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | if (!PyUnicode_Check(unicode)) { |
| 3158 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3159 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3161 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3162 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3163 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3164 | if ((strcmp(lower, "utf-8") == 0) || |
| 3165 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3166 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3167 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3168 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3169 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3170 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3171 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3172 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3173 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3174 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3175 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3176 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3177 | else if (strcmp(lower, "mbcs") == 0) |
| 3178 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3179 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3180 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3181 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3182 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3183 | |
| 3184 | /* Encode via the codec registry */ |
| 3185 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3186 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | |
| 3189 | /* The normal path */ |
| 3190 | if (PyBytes_Check(v)) |
| 3191 | return v; |
| 3192 | |
| 3193 | /* 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] | 3194 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3195 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3196 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3197 | |
| 3198 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3199 | "encoder %s returned bytearray instead of bytes", |
| 3200 | encoding); |
| 3201 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3202 | Py_DECREF(v); |
| 3203 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3204 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3205 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3206 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3207 | Py_DECREF(v); |
| 3208 | return b; |
| 3209 | } |
| 3210 | |
| 3211 | PyErr_Format(PyExc_TypeError, |
| 3212 | "encoder did not return a bytes object (type=%.400s)", |
| 3213 | Py_TYPE(v)->tp_name); |
| 3214 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3215 | return NULL; |
| 3216 | } |
| 3217 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3218 | PyObject * |
| 3219 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3220 | const char *encoding, |
| 3221 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3222 | { |
| 3223 | PyObject *v; |
| 3224 | |
| 3225 | if (!PyUnicode_Check(unicode)) { |
| 3226 | PyErr_BadArgument(); |
| 3227 | goto onError; |
| 3228 | } |
| 3229 | |
| 3230 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3231 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3232 | |
| 3233 | /* Encode via the codec registry */ |
| 3234 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3235 | if (v == NULL) |
| 3236 | goto onError; |
| 3237 | if (!PyUnicode_Check(v)) { |
| 3238 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3239 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3240 | Py_TYPE(v)->tp_name); |
| 3241 | Py_DECREF(v); |
| 3242 | goto onError; |
| 3243 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3244 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3245 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3246 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3247 | return NULL; |
| 3248 | } |
| 3249 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3250 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3251 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3252 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3253 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3254 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3255 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3256 | PyObject* |
| 3257 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3258 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3259 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3260 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3261 | #elif defined(__APPLE__) |
| 3262 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3263 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3264 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3265 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3266 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3267 | the Python codec requires to encode at least its own filename. Use the C |
| 3268 | version of the locale codec until the codec registry is initialized and |
| 3269 | the Python codec is loaded. |
| 3270 | |
| 3271 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3272 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3273 | subinterpreters. */ |
| 3274 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3275 | return PyUnicode_Decode(s, size, |
| 3276 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3277 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3278 | } |
| 3279 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3280 | /* locale encoding with surrogateescape */ |
| 3281 | wchar_t *wchar; |
| 3282 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3283 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3284 | |
| 3285 | if (s[size] != '\0' || size != strlen(s)) { |
| 3286 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3287 | return NULL; |
| 3288 | } |
| 3289 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3290 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3291 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3292 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3293 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3294 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3295 | PyMem_Free(wchar); |
| 3296 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3297 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3298 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3301 | |
| 3302 | int |
| 3303 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3304 | { |
| 3305 | PyObject *output = NULL; |
| 3306 | Py_ssize_t size; |
| 3307 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3308 | if (arg == NULL) { |
| 3309 | Py_DECREF(*(PyObject**)addr); |
| 3310 | return 1; |
| 3311 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3312 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3313 | output = arg; |
| 3314 | Py_INCREF(output); |
| 3315 | } |
| 3316 | else { |
| 3317 | arg = PyUnicode_FromObject(arg); |
| 3318 | if (!arg) |
| 3319 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3320 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3321 | Py_DECREF(arg); |
| 3322 | if (!output) |
| 3323 | return 0; |
| 3324 | if (!PyBytes_Check(output)) { |
| 3325 | Py_DECREF(output); |
| 3326 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3327 | return 0; |
| 3328 | } |
| 3329 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3330 | size = PyBytes_GET_SIZE(output); |
| 3331 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3332 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3333 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3334 | Py_DECREF(output); |
| 3335 | return 0; |
| 3336 | } |
| 3337 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3338 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3342 | int |
| 3343 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3344 | { |
| 3345 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3346 | if (arg == NULL) { |
| 3347 | Py_DECREF(*(PyObject**)addr); |
| 3348 | return 1; |
| 3349 | } |
| 3350 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3351 | if (PyUnicode_READY(arg)) |
| 3352 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3353 | output = arg; |
| 3354 | Py_INCREF(output); |
| 3355 | } |
| 3356 | else { |
| 3357 | arg = PyBytes_FromObject(arg); |
| 3358 | if (!arg) |
| 3359 | return 0; |
| 3360 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3361 | PyBytes_GET_SIZE(arg)); |
| 3362 | Py_DECREF(arg); |
| 3363 | if (!output) |
| 3364 | return 0; |
| 3365 | if (!PyUnicode_Check(output)) { |
| 3366 | Py_DECREF(output); |
| 3367 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3368 | return 0; |
| 3369 | } |
| 3370 | } |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3371 | if (PyUnicode_READY(output) < 0) { |
| 3372 | Py_DECREF(output); |
| 3373 | return 0; |
| 3374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3375 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3376 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3377 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3378 | Py_DECREF(output); |
| 3379 | return 0; |
| 3380 | } |
| 3381 | *(PyObject**)addr = output; |
| 3382 | return Py_CLEANUP_SUPPORTED; |
| 3383 | } |
| 3384 | |
| 3385 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3386 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3387 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3388 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3389 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3390 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3391 | if (!PyUnicode_Check(unicode)) { |
| 3392 | PyErr_BadArgument(); |
| 3393 | return NULL; |
| 3394 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3395 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3396 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3397 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3398 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3399 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3400 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3401 | if (bytes == NULL) |
| 3402 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3403 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3404 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3405 | Py_DECREF(bytes); |
| 3406 | return NULL; |
| 3407 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3408 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3409 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3410 | PyBytes_AS_STRING(bytes), |
| 3411 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3412 | Py_DECREF(bytes); |
| 3413 | } |
| 3414 | |
| 3415 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3416 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3417 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3421 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3422 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3423 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3424 | } |
| 3425 | |
| 3426 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3427 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3428 | #endif |
| 3429 | |
| 3430 | |
| 3431 | Py_UNICODE * |
| 3432 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3434 | const unsigned char *one_byte; |
| 3435 | #if SIZEOF_WCHAR_T == 4 |
| 3436 | const Py_UCS2 *two_bytes; |
| 3437 | #else |
| 3438 | const Py_UCS4 *four_bytes; |
| 3439 | const Py_UCS4 *ucs4_end; |
| 3440 | Py_ssize_t num_surrogates; |
| 3441 | #endif |
| 3442 | wchar_t *w; |
| 3443 | wchar_t *wchar_end; |
| 3444 | |
| 3445 | if (!PyUnicode_Check(unicode)) { |
| 3446 | PyErr_BadArgument(); |
| 3447 | return NULL; |
| 3448 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3449 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3450 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3451 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3452 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3453 | |
| 3454 | #ifdef Py_DEBUG |
| 3455 | ++unicode_as_unicode_calls; |
| 3456 | #endif |
| 3457 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3458 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3459 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3460 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3461 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3462 | num_surrogates = 0; |
| 3463 | |
| 3464 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3465 | if (*four_bytes > 0xFFFF) |
| 3466 | ++num_surrogates; |
| 3467 | } |
| 3468 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3469 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3470 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3471 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3472 | PyErr_NoMemory(); |
| 3473 | return NULL; |
| 3474 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3475 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3476 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3477 | w = _PyUnicode_WSTR(unicode); |
| 3478 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3479 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3480 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3481 | if (*four_bytes > 0xFFFF) { |
| 3482 | /* encode surrogate pair in this case */ |
| 3483 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3484 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3485 | } |
| 3486 | else |
| 3487 | *w = *four_bytes; |
| 3488 | |
| 3489 | if (w > wchar_end) { |
| 3490 | assert(0 && "Miscalculated string end"); |
| 3491 | } |
| 3492 | } |
| 3493 | *w = 0; |
| 3494 | #else |
| 3495 | /* sizeof(wchar_t) == 4 */ |
| 3496 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3497 | "should share memory already."); |
| 3498 | return NULL; |
| 3499 | #endif |
| 3500 | } |
| 3501 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3502 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3503 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3504 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3505 | PyErr_NoMemory(); |
| 3506 | return NULL; |
| 3507 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3508 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3509 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3510 | w = _PyUnicode_WSTR(unicode); |
| 3511 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3512 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3513 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3514 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3515 | for (; w < wchar_end; ++one_byte, ++w) |
| 3516 | *w = *one_byte; |
| 3517 | /* null-terminate the wstr */ |
| 3518 | *w = 0; |
| 3519 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3520 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3521 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3522 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3523 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3524 | *w = *two_bytes; |
| 3525 | /* null-terminate the wstr */ |
| 3526 | *w = 0; |
| 3527 | #else |
| 3528 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3529 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3530 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3531 | Py_FatalError("Impossible unicode object state, wstr " |
| 3532 | "and str should share memory already."); |
| 3533 | return NULL; |
| 3534 | #endif |
| 3535 | } |
| 3536 | else { |
| 3537 | assert(0 && "This should never happen."); |
| 3538 | } |
| 3539 | } |
| 3540 | } |
| 3541 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3542 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3543 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3546 | Py_UNICODE * |
| 3547 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3548 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3549 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3552 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3553 | Py_ssize_t |
| 3554 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3555 | { |
| 3556 | if (!PyUnicode_Check(unicode)) { |
| 3557 | PyErr_BadArgument(); |
| 3558 | goto onError; |
| 3559 | } |
| 3560 | return PyUnicode_GET_SIZE(unicode); |
| 3561 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3562 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3563 | return -1; |
| 3564 | } |
| 3565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3566 | Py_ssize_t |
| 3567 | PyUnicode_GetLength(PyObject *unicode) |
| 3568 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3569 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3570 | PyErr_BadArgument(); |
| 3571 | return -1; |
| 3572 | } |
| 3573 | |
| 3574 | return PyUnicode_GET_LENGTH(unicode); |
| 3575 | } |
| 3576 | |
| 3577 | Py_UCS4 |
| 3578 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3579 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3580 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3581 | PyErr_BadArgument(); |
| 3582 | return (Py_UCS4)-1; |
| 3583 | } |
| 3584 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3585 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3586 | return (Py_UCS4)-1; |
| 3587 | } |
| 3588 | return PyUnicode_READ_CHAR(unicode, index); |
| 3589 | } |
| 3590 | |
| 3591 | int |
| 3592 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3593 | { |
| 3594 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3595 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3596 | return -1; |
| 3597 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3598 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3599 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3600 | return -1; |
| 3601 | } |
| 3602 | if (_PyUnicode_Dirty(unicode)) |
| 3603 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3604 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3605 | index, ch); |
| 3606 | return 0; |
| 3607 | } |
| 3608 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3609 | const char * |
| 3610 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3611 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3612 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3615 | /* create or adjust a UnicodeDecodeError */ |
| 3616 | static void |
| 3617 | make_decode_exception(PyObject **exceptionObject, |
| 3618 | const char *encoding, |
| 3619 | const char *input, Py_ssize_t length, |
| 3620 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3621 | const char *reason) |
| 3622 | { |
| 3623 | if (*exceptionObject == NULL) { |
| 3624 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3625 | encoding, input, length, startpos, endpos, reason); |
| 3626 | } |
| 3627 | else { |
| 3628 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3629 | goto onError; |
| 3630 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3631 | goto onError; |
| 3632 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3633 | goto onError; |
| 3634 | } |
| 3635 | return; |
| 3636 | |
| 3637 | onError: |
| 3638 | Py_DECREF(*exceptionObject); |
| 3639 | *exceptionObject = NULL; |
| 3640 | } |
| 3641 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3642 | /* error handling callback helper: |
| 3643 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3644 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3645 | and adjust various state variables. |
| 3646 | return 0 on success, -1 on error |
| 3647 | */ |
| 3648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3649 | static int |
| 3650 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3651 | const char *encoding, const char *reason, |
| 3652 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3653 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3654 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3656 | 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] | 3657 | |
| 3658 | PyObject *restuple = NULL; |
| 3659 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3660 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3661 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3662 | Py_ssize_t requiredsize; |
| 3663 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3664 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3665 | int res = -1; |
| 3666 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3667 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 3668 | outsize = PyUnicode_GET_LENGTH(*output); |
| 3669 | else |
| 3670 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 3671 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3672 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3673 | *errorHandler = PyCodec_LookupError(errors); |
| 3674 | if (*errorHandler == NULL) |
| 3675 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3678 | make_decode_exception(exceptionObject, |
| 3679 | encoding, |
| 3680 | *input, *inend - *input, |
| 3681 | *startinpos, *endinpos, |
| 3682 | reason); |
| 3683 | if (*exceptionObject == NULL) |
| 3684 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3685 | |
| 3686 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3687 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3688 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3689 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3690 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
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 | } |
| 3693 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3694 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3695 | if (PyUnicode_READY(repunicode) < 0) |
| 3696 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Copy back the bytes variables, which might have been modified by the |
| 3699 | callback */ |
| 3700 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3701 | if (!inputobj) |
| 3702 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3703 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3704 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3705 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3706 | *input = PyBytes_AS_STRING(inputobj); |
| 3707 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3708 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3709 | /* we can DECREF safely, as the exception has another reference, |
| 3710 | so the object won't go away. */ |
| 3711 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3712 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3713 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3714 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3715 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3716 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3717 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3720 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 3721 | /* need more space? (at least enough for what we |
| 3722 | have+the replacement+the rest of the string (starting |
| 3723 | at the new input position), so we won't have to check space |
| 3724 | when there are no errors in the rest of the string) */ |
| 3725 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 3726 | requiredsize = *outpos + replen + insize-newpos; |
| 3727 | if (requiredsize > outsize) { |
| 3728 | if (requiredsize<2*outsize) |
| 3729 | requiredsize = 2*outsize; |
| 3730 | if (unicode_resize(output, requiredsize) < 0) |
| 3731 | goto onError; |
| 3732 | } |
| 3733 | if (unicode_widen(output, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3734 | goto onError; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3735 | copy_characters(*output, *outpos, repunicode, 0, replen); |
| 3736 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3738 | else { |
| 3739 | wchar_t *repwstr; |
| 3740 | Py_ssize_t repwlen; |
| 3741 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3742 | if (repwstr == NULL) |
| 3743 | goto onError; |
| 3744 | /* need more space? (at least enough for what we |
| 3745 | have+the replacement+the rest of the string (starting |
| 3746 | at the new input position), so we won't have to check space |
| 3747 | when there are no errors in the rest of the string) */ |
| 3748 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3749 | if (requiredsize > outsize) { |
| 3750 | if (requiredsize < 2*outsize) |
| 3751 | requiredsize = 2*outsize; |
| 3752 | if (unicode_resize(output, requiredsize) < 0) |
| 3753 | goto onError; |
| 3754 | } |
| 3755 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3756 | *outpos += repwlen; |
| 3757 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3758 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3759 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3760 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3761 | /* we made it! */ |
| 3762 | res = 0; |
| 3763 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3764 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | Py_XDECREF(restuple); |
| 3766 | return res; |
| 3767 | } |
| 3768 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3769 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3770 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3771 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3772 | |
| 3773 | /* Three simple macros defining base-64. */ |
| 3774 | |
| 3775 | /* Is c a base-64 character? */ |
| 3776 | |
| 3777 | #define IS_BASE64(c) \ |
| 3778 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3779 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3780 | ((c) >= '0' && (c) <= '9') || \ |
| 3781 | (c) == '+' || (c) == '/') |
| 3782 | |
| 3783 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3784 | |
| 3785 | #define FROM_BASE64(c) \ |
| 3786 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3787 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3788 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3789 | (c) == '+' ? 62 : 63) |
| 3790 | |
| 3791 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3792 | |
| 3793 | #define TO_BASE64(n) \ |
| 3794 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3795 | |
| 3796 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3797 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3798 | * byte not decoding to itself is the + which begins a base64 |
| 3799 | * string. */ |
| 3800 | |
| 3801 | #define DECODE_DIRECT(c) \ |
| 3802 | ((c) <= 127 && (c) != '+') |
| 3803 | |
| 3804 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3805 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3806 | * the above). See RFC2152. This array identifies these different |
| 3807 | * sets: |
| 3808 | * 0 : "Set D" |
| 3809 | * alphanumeric and '(),-./:? |
| 3810 | * 1 : "Set O" |
| 3811 | * !"#$%&*;<=>@[]^_`{|} |
| 3812 | * 2 : "whitespace" |
| 3813 | * ht nl cr sp |
| 3814 | * 3 : special (must be base64 encoded) |
| 3815 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3816 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3817 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3818 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3819 | char utf7_category[128] = { |
| 3820 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3821 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3822 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3823 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3824 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3825 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3826 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3827 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3828 | /* @ A B C D E F G H I J K L M N O */ |
| 3829 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3830 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3831 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3832 | /* ` a b c d e f g h i j k l m n o */ |
| 3833 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3834 | /* p q r s t u v w x y z { | } ~ del */ |
| 3835 | 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] | 3836 | }; |
| 3837 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3838 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3839 | * answer depends on whether we are encoding set O as itself, and also |
| 3840 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3841 | * clear that the answers to these questions vary between |
| 3842 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3843 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3844 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3845 | ((c) < 128 && (c) > 0 && \ |
| 3846 | ((utf7_category[(c)] == 0) || \ |
| 3847 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3848 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3850 | PyObject * |
| 3851 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3852 | Py_ssize_t size, |
| 3853 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3854 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3855 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3856 | } |
| 3857 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3858 | /* The decoder. The only state we preserve is our read position, |
| 3859 | * i.e. how many characters we have consumed. So if we end in the |
| 3860 | * middle of a shift sequence we have to back off the read position |
| 3861 | * and the output to the beginning of the sequence, otherwise we lose |
| 3862 | * all the shift state (seen bits, number of bits seen, high |
| 3863 | * surrogate). */ |
| 3864 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3865 | PyObject * |
| 3866 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3867 | Py_ssize_t size, |
| 3868 | const char *errors, |
| 3869 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3870 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3871 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3872 | Py_ssize_t startinpos; |
| 3873 | Py_ssize_t endinpos; |
| 3874 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3875 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3876 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3877 | const char *errmsg = ""; |
| 3878 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3879 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3880 | unsigned int base64bits = 0; |
| 3881 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3882 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3883 | PyObject *errorHandler = NULL; |
| 3884 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3885 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3886 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 3887 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3888 | if (!unicode) |
| 3889 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3890 | if (size == 0) { |
| 3891 | if (consumed) |
| 3892 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 3893 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3894 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3895 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3896 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3897 | e = s + size; |
| 3898 | |
| 3899 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3900 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3901 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3902 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3903 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3904 | if (inShift) { /* in a base-64 section */ |
| 3905 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3906 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3907 | base64bits += 6; |
| 3908 | s++; |
| 3909 | if (base64bits >= 16) { |
| 3910 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 3911 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3912 | base64bits -= 16; |
| 3913 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3914 | if (surrogate) { |
| 3915 | /* expecting a second surrogate */ |
| 3916 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3917 | Py_UCS4 ch2 = (((surrogate & 0x3FF)<<10) |
| 3918 | | (outCh & 0x3FF)) + 0x10000; |
| 3919 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 3920 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3921 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3922 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3923 | } |
| 3924 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3925 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3926 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3927 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3928 | } |
| 3929 | } |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3930 | if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3931 | /* first surrogate */ |
| 3932 | surrogate = outCh; |
| 3933 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3934 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3935 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 3936 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3941 | inShift = 0; |
| 3942 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3943 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 3944 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 3945 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 3946 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3947 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3948 | if (base64bits > 0) { /* left-over bits */ |
| 3949 | if (base64bits >= 6) { |
| 3950 | /* We've seen at least one base-64 character */ |
| 3951 | errmsg = "partial character in shift sequence"; |
| 3952 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3953 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3954 | else { |
| 3955 | /* Some bits remain; they should be zero */ |
| 3956 | if (base64buffer != 0) { |
| 3957 | errmsg = "non-zero padding bits in shift sequence"; |
| 3958 | goto utf7Error; |
| 3959 | } |
| 3960 | } |
| 3961 | } |
| 3962 | if (ch != '-') { |
| 3963 | /* '-' is absorbed; other terminating |
| 3964 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3965 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3966 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3967 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3968 | } |
| 3969 | } |
| 3970 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3972 | s++; /* consume '+' */ |
| 3973 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3974 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3975 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 3976 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3977 | } |
| 3978 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3979 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3980 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3981 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3984 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 3985 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 3986 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3987 | s++; |
| 3988 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3989 | else { |
| 3990 | startinpos = s-starts; |
| 3991 | s++; |
| 3992 | errmsg = "unexpected special character"; |
| 3993 | goto utf7Error; |
| 3994 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3995 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3996 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3997 | endinpos = s-starts; |
| 3998 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3999 | errors, &errorHandler, |
| 4000 | "utf7", errmsg, |
| 4001 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4002 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4003 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4006 | /* end of string */ |
| 4007 | |
| 4008 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4009 | /* if we're in an inconsistent state, that's an error */ |
| 4010 | if (surrogate || |
| 4011 | (base64bits >= 6) || |
| 4012 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4013 | endinpos = size; |
| 4014 | if (unicode_decode_call_errorhandler( |
| 4015 | errors, &errorHandler, |
| 4016 | "utf7", "unterminated shift sequence", |
| 4017 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4018 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4019 | goto onError; |
| 4020 | if (s < e) |
| 4021 | goto restart; |
| 4022 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4023 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4024 | |
| 4025 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4026 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4027 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4028 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4029 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4030 | } |
| 4031 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4032 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4033 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4034 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4035 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4036 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4037 | goto onError; |
| 4038 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | Py_XDECREF(errorHandler); |
| 4040 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4041 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4042 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4043 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4044 | Py_XDECREF(errorHandler); |
| 4045 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4046 | Py_DECREF(unicode); |
| 4047 | return NULL; |
| 4048 | } |
| 4049 | |
| 4050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4051 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4052 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4053 | int base64SetO, |
| 4054 | int base64WhiteSpace, |
| 4055 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4056 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4057 | int kind; |
| 4058 | void *data; |
| 4059 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4060 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4061 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4062 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4063 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4064 | unsigned int base64bits = 0; |
| 4065 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4066 | char * out; |
| 4067 | char * start; |
| 4068 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4069 | if (PyUnicode_READY(str) < 0) |
| 4070 | return NULL; |
| 4071 | kind = PyUnicode_KIND(str); |
| 4072 | data = PyUnicode_DATA(str); |
| 4073 | len = PyUnicode_GET_LENGTH(str); |
| 4074 | |
| 4075 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4076 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4077 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4078 | /* It might be possible to tighten this worst case */ |
| 4079 | allocated = 8 * len; |
| 4080 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4081 | return PyErr_NoMemory(); |
| 4082 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4083 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4084 | if (v == NULL) |
| 4085 | return NULL; |
| 4086 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4087 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4088 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4089 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4090 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4091 | if (inShift) { |
| 4092 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4093 | /* shifting out */ |
| 4094 | if (base64bits) { /* output remaining bits */ |
| 4095 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4096 | base64buffer = 0; |
| 4097 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4098 | } |
| 4099 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4100 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4101 | so no '-' is required, except if the character is itself a '-' */ |
| 4102 | if (IS_BASE64(ch) || ch == '-') { |
| 4103 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4104 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4105 | *out++ = (char) ch; |
| 4106 | } |
| 4107 | else { |
| 4108 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4109 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4110 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4111 | else { /* not in a shift sequence */ |
| 4112 | if (ch == '+') { |
| 4113 | *out++ = '+'; |
| 4114 | *out++ = '-'; |
| 4115 | } |
| 4116 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4117 | *out++ = (char) ch; |
| 4118 | } |
| 4119 | else { |
| 4120 | *out++ = '+'; |
| 4121 | inShift = 1; |
| 4122 | goto encode_char; |
| 4123 | } |
| 4124 | } |
| 4125 | continue; |
| 4126 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4127 | if (ch >= 0x10000) { |
| 4128 | /* code first surrogate */ |
| 4129 | base64bits += 16; |
| 4130 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4131 | while (base64bits >= 6) { |
| 4132 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4133 | base64bits -= 6; |
| 4134 | } |
| 4135 | /* prepare second surrogate */ |
| 4136 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4137 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4138 | base64bits += 16; |
| 4139 | base64buffer = (base64buffer << 16) | ch; |
| 4140 | while (base64bits >= 6) { |
| 4141 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4142 | base64bits -= 6; |
| 4143 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4144 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4145 | if (base64bits) |
| 4146 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4147 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4148 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4149 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4150 | return NULL; |
| 4151 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4152 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4153 | PyObject * |
| 4154 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4155 | Py_ssize_t size, |
| 4156 | int base64SetO, |
| 4157 | int base64WhiteSpace, |
| 4158 | const char *errors) |
| 4159 | { |
| 4160 | PyObject *result; |
| 4161 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4162 | if (tmp == NULL) |
| 4163 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4164 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4165 | base64WhiteSpace, errors); |
| 4166 | Py_DECREF(tmp); |
| 4167 | return result; |
| 4168 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4170 | #undef IS_BASE64 |
| 4171 | #undef FROM_BASE64 |
| 4172 | #undef TO_BASE64 |
| 4173 | #undef DECODE_DIRECT |
| 4174 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4176 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4177 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4178 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4179 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4180 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4181 | illegal prefix. See RFC 3629 for details */ |
| 4182 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4183 | 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] | 4184 | 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] | 4185 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4186 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4187 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4188 | 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] | 4189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4190 | 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] | 4191 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4192 | 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] | 4193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4194 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4195 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4196 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4197 | 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] | 4198 | }; |
| 4199 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4200 | PyObject * |
| 4201 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4202 | Py_ssize_t size, |
| 4203 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4204 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4205 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4206 | } |
| 4207 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4208 | #include "stringlib/ucs1lib.h" |
| 4209 | #include "stringlib/codecs.h" |
| 4210 | #include "stringlib/undef.h" |
| 4211 | |
| 4212 | #include "stringlib/ucs2lib.h" |
| 4213 | #include "stringlib/codecs.h" |
| 4214 | #include "stringlib/undef.h" |
| 4215 | |
| 4216 | #include "stringlib/ucs4lib.h" |
| 4217 | #include "stringlib/codecs.h" |
| 4218 | #include "stringlib/undef.h" |
| 4219 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4220 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4221 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4222 | |
| 4223 | /* Mask to quickly check whether a C 'long' contains a |
| 4224 | non-ASCII, UTF8-encoded char. */ |
| 4225 | #if (SIZEOF_LONG == 8) |
| 4226 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4227 | #elif (SIZEOF_LONG == 4) |
| 4228 | # define ASCII_CHAR_MASK 0x80808080L |
| 4229 | #else |
| 4230 | # error C 'long' size should be either 4 or 8! |
| 4231 | #endif |
| 4232 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4233 | /* Scans a UTF-8 string and returns the maximum character to be expected |
| 4234 | and the size of the decoded unicode string. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4235 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4236 | 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] | 4237 | PyUnicode_DecodeUTF8Stateful. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4238 | */ |
| 4239 | static Py_UCS4 |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4240 | utf8_max_char_size_and_char_count(const char *s, Py_ssize_t string_size, |
| 4241 | Py_ssize_t *unicode_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4242 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4243 | Py_ssize_t char_count = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4244 | const unsigned char *p = (const unsigned char *)s; |
| 4245 | const unsigned char *end = p + string_size; |
| 4246 | 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] | 4247 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4248 | assert(unicode_size != NULL); |
| 4249 | |
| 4250 | /* By having a cascade of independent loops which fallback onto each |
| 4251 | other, we minimize the amount of work done in the average loop |
| 4252 | iteration, and we also maximize the CPU's ability to predict |
| 4253 | branches correctly (because a given condition will have always the |
| 4254 | same boolean outcome except perhaps in the last iteration of the |
| 4255 | corresponding loop). |
| 4256 | In the general case this brings us rather close to decoding |
| 4257 | performance pre-PEP 393, despite the two-pass decoding. |
| 4258 | |
| 4259 | Note that the pure ASCII loop is not duplicated once a non-ASCII |
| 4260 | character has been encountered. It is actually a pessimization (by |
| 4261 | a significant factor) to use this loop on text with many non-ASCII |
| 4262 | characters, and it is important to avoid bad performance on valid |
| 4263 | utf-8 data (invalid utf-8 being a different can of worms). |
| 4264 | */ |
| 4265 | |
| 4266 | /* ASCII */ |
| 4267 | for (; p < end; ++p) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4268 | /* Only check value if it's not a ASCII char... */ |
| 4269 | if (*p < 0x80) { |
| 4270 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4271 | an explanation. */ |
| 4272 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4273 | /* Help register allocation */ |
| 4274 | register const unsigned char *_p = p; |
| 4275 | while (_p < aligned_end) { |
| 4276 | unsigned long value = *(unsigned long *) _p; |
| 4277 | if (value & ASCII_CHAR_MASK) |
| 4278 | break; |
| 4279 | _p += SIZEOF_LONG; |
| 4280 | char_count += SIZEOF_LONG; |
| 4281 | } |
| 4282 | p = _p; |
| 4283 | if (p == end) |
| 4284 | break; |
| 4285 | } |
| 4286 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4287 | if (*p < 0x80) |
| 4288 | ++char_count; |
| 4289 | else |
| 4290 | goto _ucs1loop; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4291 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4292 | *unicode_size = char_count; |
| 4293 | return 127; |
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 | _ucs1loop: |
| 4296 | for (; p < end; ++p) { |
| 4297 | if (*p < 0xc4) |
| 4298 | char_count += ((*p & 0xc0) != 0x80); |
| 4299 | else |
| 4300 | goto _ucs2loop; |
| 4301 | } |
| 4302 | *unicode_size = char_count; |
| 4303 | return 255; |
| 4304 | |
| 4305 | _ucs2loop: |
| 4306 | for (; p < end; ++p) { |
| 4307 | if (*p < 0xf0) |
| 4308 | char_count += ((*p & 0xc0) != 0x80); |
| 4309 | else |
| 4310 | goto _ucs4loop; |
| 4311 | } |
| 4312 | *unicode_size = char_count; |
| 4313 | return 65535; |
| 4314 | |
| 4315 | _ucs4loop: |
| 4316 | for (; p < end; ++p) { |
| 4317 | char_count += ((*p & 0xc0) != 0x80); |
| 4318 | } |
| 4319 | *unicode_size = char_count; |
| 4320 | return 65537; |
| 4321 | } |
| 4322 | |
| 4323 | /* Called when we encountered some error that wasn't detected in the original |
| 4324 | scan, e.g. an encoded surrogate character. The original maxchar computation |
| 4325 | may have been incorrect, so redo it. */ |
| 4326 | static int |
| 4327 | refit_partial_string(PyObject **unicode, int kind, void *data, Py_ssize_t n) |
| 4328 | { |
| 4329 | PyObject *tmp; |
| 4330 | Py_ssize_t k, maxchar; |
| 4331 | for (k = 0, maxchar = 0; k < n; k++) |
| 4332 | maxchar = Py_MAX(maxchar, PyUnicode_READ(kind, data, k)); |
| 4333 | tmp = PyUnicode_New(PyUnicode_GET_LENGTH(*unicode), maxchar); |
| 4334 | if (tmp == NULL) |
| 4335 | return -1; |
| 4336 | PyUnicode_CopyCharacters(tmp, 0, *unicode, 0, n); |
| 4337 | Py_DECREF(*unicode); |
| 4338 | *unicode = tmp; |
| 4339 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4342 | /* Similar to PyUnicode_WRITE but may attempt to widen and resize the string |
| 4343 | in case of errors. Implicit parameters: unicode, kind, data, has_errors, |
| 4344 | onError. Potential resizing overallocates, so the result needs to shrink |
| 4345 | at the end. |
| 4346 | */ |
| 4347 | #define WRITE_MAYBE_FAIL(index, value) \ |
| 4348 | do { \ |
| 4349 | if (has_errors) { \ |
| 4350 | Py_ssize_t pos = index; \ |
| 4351 | if (pos > PyUnicode_GET_LENGTH(unicode) && \ |
| 4352 | unicode_resize(&unicode, pos + pos/8) < 0) \ |
| 4353 | goto onError; \ |
| 4354 | if (unicode_putchar(&unicode, &pos, value) < 0) \ |
| 4355 | goto onError; \ |
| 4356 | } \ |
| 4357 | else \ |
| 4358 | PyUnicode_WRITE(kind, data, index, value); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4359 | } while (0) |
| 4360 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4361 | PyObject * |
| 4362 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4363 | Py_ssize_t size, |
| 4364 | const char *errors, |
| 4365 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4366 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4367 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4368 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4369 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4370 | Py_ssize_t startinpos; |
| 4371 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4372 | const char *e, *aligned_end; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4373 | PyObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4374 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | PyObject *errorHandler = NULL; |
| 4376 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4377 | Py_UCS4 maxchar = 0; |
| 4378 | Py_ssize_t unicode_size; |
| 4379 | Py_ssize_t i; |
| 4380 | int kind; |
| 4381 | void *data; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4382 | int has_errors = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4383 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4384 | if (size == 0) { |
| 4385 | if (consumed) |
| 4386 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4387 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4388 | } |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4389 | maxchar = utf8_max_char_size_and_char_count(s, size, &unicode_size); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4390 | /* When the string is ASCII only, just use memcpy and return. |
| 4391 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4392 | sequence at the end of the ASCII block. */ |
| 4393 | if (maxchar < 128 && size == unicode_size) { |
Victor Stinner | 4288520 | 2011-11-22 01:23:02 +0100 | [diff] [blame^] | 4394 | if (consumed) |
| 4395 | *consumed = size; |
| 4396 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4397 | if (size == 1) |
| 4398 | return get_latin1_char((unsigned char)s[0]); |
| 4399 | |
| 4400 | unicode = PyUnicode_New(unicode_size, maxchar); |
| 4401 | if (!unicode) |
| 4402 | return NULL; |
| 4403 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4404 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4405 | return unicode; |
| 4406 | } |
| 4407 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4408 | /* In case of errors, maxchar and size computation might be incorrect; |
| 4409 | code below refits and resizes as necessary. */ |
| 4410 | unicode = PyUnicode_New(unicode_size, maxchar); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4411 | if (!unicode) |
| 4412 | return NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4413 | kind = PyUnicode_KIND(unicode); |
| 4414 | data = PyUnicode_DATA(unicode); |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4416 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4417 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4418 | e = s + size; |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4419 | switch (kind) { |
| 4420 | case PyUnicode_1BYTE_KIND: |
| 4421 | has_errors = ucs1lib_utf8_try_decode(s, e, (Py_UCS1 *) data, &s, &i); |
| 4422 | break; |
| 4423 | case PyUnicode_2BYTE_KIND: |
| 4424 | has_errors = ucs2lib_utf8_try_decode(s, e, (Py_UCS2 *) data, &s, &i); |
| 4425 | break; |
| 4426 | case PyUnicode_4BYTE_KIND: |
| 4427 | has_errors = ucs4lib_utf8_try_decode(s, e, (Py_UCS4 *) data, &s, &i); |
| 4428 | break; |
| 4429 | } |
| 4430 | if (!has_errors) { |
| 4431 | /* Ensure the unicode size calculation was correct */ |
| 4432 | assert(i == unicode_size); |
| 4433 | assert(s == e); |
| 4434 | if (consumed) |
| 4435 | *consumed = s-starts; |
| 4436 | return unicode; |
| 4437 | } |
| 4438 | /* Fall through to the generic decoding loop for the rest of |
| 4439 | the string */ |
| 4440 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
| 4441 | goto onError; |
| 4442 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4443 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4444 | |
| 4445 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4446 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4447 | |
| 4448 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4449 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4450 | input will consist of an overwhelming majority of ASCII |
| 4451 | characters, we try to optimize for this case by checking |
| 4452 | as many characters as a C 'long' can contain. |
| 4453 | First, check if we can do an aligned read, as most CPUs have |
| 4454 | a penalty for unaligned reads. |
| 4455 | */ |
| 4456 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4457 | /* Help register allocation */ |
| 4458 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4459 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4460 | while (_s < aligned_end) { |
| 4461 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4462 | and do a fast unrolled copy if it only contains ASCII |
| 4463 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4464 | unsigned long value = *(unsigned long *) _s; |
| 4465 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4466 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4467 | WRITE_MAYBE_FAIL(_i+0, _s[0]); |
| 4468 | WRITE_MAYBE_FAIL(_i+1, _s[1]); |
| 4469 | WRITE_MAYBE_FAIL(_i+2, _s[2]); |
| 4470 | WRITE_MAYBE_FAIL(_i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4471 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4472 | WRITE_MAYBE_FAIL(_i+4, _s[4]); |
| 4473 | WRITE_MAYBE_FAIL(_i+5, _s[5]); |
| 4474 | WRITE_MAYBE_FAIL(_i+6, _s[6]); |
| 4475 | WRITE_MAYBE_FAIL(_i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4476 | #endif |
| 4477 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4478 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4479 | } |
| 4480 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4481 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4482 | if (s == e) |
| 4483 | break; |
| 4484 | ch = (unsigned char)*s; |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | if (ch < 0x80) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4489 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | s++; |
| 4491 | continue; |
| 4492 | } |
| 4493 | |
| 4494 | n = utf8_code_length[ch]; |
| 4495 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4496 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4497 | if (consumed) |
| 4498 | break; |
| 4499 | else { |
| 4500 | errmsg = "unexpected end of data"; |
| 4501 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4502 | endinpos = startinpos+1; |
| 4503 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4504 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4505 | goto utf8Error; |
| 4506 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4507 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | |
| 4509 | switch (n) { |
| 4510 | |
| 4511 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4512 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4513 | startinpos = s-starts; |
| 4514 | endinpos = startinpos+1; |
| 4515 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | |
| 4517 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4518 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4519 | startinpos = s-starts; |
| 4520 | endinpos = startinpos+1; |
| 4521 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | |
| 4523 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4524 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4525 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4526 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4527 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4528 | goto utf8Error; |
| 4529 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4530 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4531 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4532 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4533 | break; |
| 4534 | |
| 4535 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4536 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4537 | will result in surrogates in range d800-dfff. Surrogates are |
| 4538 | not valid UTF-8 so they are rejected. |
| 4539 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4540 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4541 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4542 | (s[2] & 0xc0) != 0x80 || |
| 4543 | ((unsigned char)s[0] == 0xE0 && |
| 4544 | (unsigned char)s[1] < 0xA0) || |
| 4545 | ((unsigned char)s[0] == 0xED && |
| 4546 | (unsigned char)s[1] > 0x9F)) { |
| 4547 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4548 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4549 | endinpos = startinpos + 1; |
| 4550 | |
| 4551 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4552 | continuation byte is s[2], so increment endinpos by 1, |
| 4553 | if not, s[1] is invalid and endinpos doesn't need to |
| 4554 | be incremented. */ |
| 4555 | if ((s[1] & 0xC0) == 0x80) |
| 4556 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4557 | goto utf8Error; |
| 4558 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4560 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4561 | WRITE_MAYBE_FAIL(i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4562 | break; |
| 4563 | |
| 4564 | case 4: |
| 4565 | if ((s[1] & 0xc0) != 0x80 || |
| 4566 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4567 | (s[3] & 0xc0) != 0x80 || |
| 4568 | ((unsigned char)s[0] == 0xF0 && |
| 4569 | (unsigned char)s[1] < 0x90) || |
| 4570 | ((unsigned char)s[0] == 0xF4 && |
| 4571 | (unsigned char)s[1] > 0x8F)) { |
| 4572 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4573 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4574 | endinpos = startinpos + 1; |
| 4575 | if ((s[1] & 0xC0) == 0x80) { |
| 4576 | endinpos++; |
| 4577 | if ((s[2] & 0xC0) == 0x80) |
| 4578 | endinpos++; |
| 4579 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4580 | goto utf8Error; |
| 4581 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4582 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4583 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4584 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4585 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4586 | WRITE_MAYBE_FAIL(i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4587 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | } |
| 4589 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4591 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4592 | utf8Error: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4593 | if (!has_errors) { |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4594 | if (refit_partial_string(&unicode, kind, data, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4595 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4596 | has_errors = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4597 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | if (unicode_decode_call_errorhandler( |
| 4599 | errors, &errorHandler, |
| 4600 | "utf8", errmsg, |
| 4601 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4602 | &unicode, &i)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4603 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4604 | /* Update data because unicode_decode_call_errorhandler might have |
| 4605 | re-created or resized the unicode object. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4606 | data = PyUnicode_DATA(unicode); |
| 4607 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4608 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4610 | /* Ensure the unicode_size calculation above was correct: */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4611 | assert(has_errors || i == unicode_size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4612 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4613 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4614 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4616 | /* Adjust length and ready string when it contained errors and |
| 4617 | is of the old resizable kind. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4618 | if (has_errors) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4619 | if (PyUnicode_Resize(&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4620 | goto onError; |
| 4621 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | Py_XDECREF(errorHandler); |
| 4624 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4625 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4626 | return unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4628 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4629 | Py_XDECREF(errorHandler); |
| 4630 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | Py_DECREF(unicode); |
| 4632 | return NULL; |
| 4633 | } |
| 4634 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4635 | #undef WRITE_MAYBE_FAIL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4637 | #ifdef __APPLE__ |
| 4638 | |
| 4639 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4640 | used to decode the command line arguments on Mac OS X. */ |
| 4641 | |
| 4642 | wchar_t* |
| 4643 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4644 | { |
| 4645 | int n; |
| 4646 | const char *e; |
| 4647 | wchar_t *unicode, *p; |
| 4648 | |
| 4649 | /* Note: size will always be longer than the resulting Unicode |
| 4650 | character count */ |
| 4651 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4652 | PyErr_NoMemory(); |
| 4653 | return NULL; |
| 4654 | } |
| 4655 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4656 | if (!unicode) |
| 4657 | return NULL; |
| 4658 | |
| 4659 | /* Unpack UTF-8 encoded data */ |
| 4660 | p = unicode; |
| 4661 | e = s + size; |
| 4662 | while (s < e) { |
| 4663 | Py_UCS4 ch = (unsigned char)*s; |
| 4664 | |
| 4665 | if (ch < 0x80) { |
| 4666 | *p++ = (wchar_t)ch; |
| 4667 | s++; |
| 4668 | continue; |
| 4669 | } |
| 4670 | |
| 4671 | n = utf8_code_length[ch]; |
| 4672 | if (s + n > e) { |
| 4673 | goto surrogateescape; |
| 4674 | } |
| 4675 | |
| 4676 | switch (n) { |
| 4677 | case 0: |
| 4678 | case 1: |
| 4679 | goto surrogateescape; |
| 4680 | |
| 4681 | case 2: |
| 4682 | if ((s[1] & 0xc0) != 0x80) |
| 4683 | goto surrogateescape; |
| 4684 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4685 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4686 | *p++ = (wchar_t)ch; |
| 4687 | break; |
| 4688 | |
| 4689 | case 3: |
| 4690 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4691 | will result in surrogates in range d800-dfff. Surrogates are |
| 4692 | not valid UTF-8 so they are rejected. |
| 4693 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4694 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4695 | if ((s[1] & 0xc0) != 0x80 || |
| 4696 | (s[2] & 0xc0) != 0x80 || |
| 4697 | ((unsigned char)s[0] == 0xE0 && |
| 4698 | (unsigned char)s[1] < 0xA0) || |
| 4699 | ((unsigned char)s[0] == 0xED && |
| 4700 | (unsigned char)s[1] > 0x9F)) { |
| 4701 | |
| 4702 | goto surrogateescape; |
| 4703 | } |
| 4704 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4705 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4706 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4707 | break; |
| 4708 | |
| 4709 | case 4: |
| 4710 | if ((s[1] & 0xc0) != 0x80 || |
| 4711 | (s[2] & 0xc0) != 0x80 || |
| 4712 | (s[3] & 0xc0) != 0x80 || |
| 4713 | ((unsigned char)s[0] == 0xF0 && |
| 4714 | (unsigned char)s[1] < 0x90) || |
| 4715 | ((unsigned char)s[0] == 0xF4 && |
| 4716 | (unsigned char)s[1] > 0x8F)) { |
| 4717 | goto surrogateescape; |
| 4718 | } |
| 4719 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4720 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4721 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4722 | |
| 4723 | #if SIZEOF_WCHAR_T == 4 |
| 4724 | *p++ = (wchar_t)ch; |
| 4725 | #else |
| 4726 | /* compute and append the two surrogates: */ |
| 4727 | |
| 4728 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4729 | ch -= 0x10000; |
| 4730 | |
| 4731 | /* high surrogate = top 10 bits added to D800 */ |
| 4732 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4733 | |
| 4734 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4735 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4736 | #endif |
| 4737 | break; |
| 4738 | } |
| 4739 | s += n; |
| 4740 | continue; |
| 4741 | |
| 4742 | surrogateescape: |
| 4743 | *p++ = 0xDC00 + ch; |
| 4744 | s++; |
| 4745 | } |
| 4746 | *p = L'\0'; |
| 4747 | return unicode; |
| 4748 | } |
| 4749 | |
| 4750 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4752 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4753 | |
| 4754 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4755 | and allocate exactly as much space needed at the end. Else allocate the |
| 4756 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4757 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4758 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4759 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4760 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4761 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4762 | #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] | 4763 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4764 | Py_ssize_t i; /* index into s of next input byte */ |
| 4765 | PyObject *result; /* result string object */ |
| 4766 | char *p; /* next free byte in output buffer */ |
| 4767 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4768 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4769 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4770 | PyObject *errorHandler = NULL; |
| 4771 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4772 | int kind; |
| 4773 | void *data; |
| 4774 | Py_ssize_t size; |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4775 | PyObject *rep = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4777 | if (!PyUnicode_Check(unicode)) { |
| 4778 | PyErr_BadArgument(); |
| 4779 | return NULL; |
| 4780 | } |
| 4781 | |
| 4782 | if (PyUnicode_READY(unicode) == -1) |
| 4783 | return NULL; |
| 4784 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4785 | if (PyUnicode_UTF8(unicode)) |
| 4786 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4787 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4788 | |
| 4789 | kind = PyUnicode_KIND(unicode); |
| 4790 | data = PyUnicode_DATA(unicode); |
| 4791 | size = PyUnicode_GET_LENGTH(unicode); |
| 4792 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4793 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4795 | if (size <= MAX_SHORT_UNICHARS) { |
| 4796 | /* Write into the stack buffer; nallocated can't overflow. |
| 4797 | * At the end, we'll allocate exactly as much heap space as it |
| 4798 | * turns out we need. |
| 4799 | */ |
| 4800 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4801 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4802 | p = stackbuf; |
| 4803 | } |
| 4804 | else { |
| 4805 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4806 | nallocated = size * 4; |
| 4807 | if (nallocated / 4 != size) /* overflow! */ |
| 4808 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4809 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4810 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4811 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4812 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4813 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4814 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4815 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4816 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4817 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4818 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4819 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4820 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4823 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4824 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4825 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4826 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4827 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4828 | Py_ssize_t repsize, k, startpos; |
| 4829 | startpos = i-1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4830 | rep = unicode_encode_call_errorhandler( |
| 4831 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4832 | unicode, &exc, startpos, startpos+1, &newpos); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4833 | if (!rep) |
| 4834 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4835 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4836 | if (PyBytes_Check(rep)) |
| 4837 | repsize = PyBytes_GET_SIZE(rep); |
| 4838 | else |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 4839 | repsize = PyUnicode_GET_LENGTH(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4840 | |
| 4841 | if (repsize > 4) { |
| 4842 | Py_ssize_t offset; |
| 4843 | |
| 4844 | if (result == NULL) |
| 4845 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4846 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4847 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4848 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4849 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4850 | /* integer overflow */ |
| 4851 | PyErr_NoMemory(); |
| 4852 | goto error; |
| 4853 | } |
| 4854 | nallocated += repsize - 4; |
| 4855 | if (result != NULL) { |
| 4856 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4857 | goto error; |
| 4858 | } else { |
| 4859 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4860 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4861 | goto error; |
| 4862 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4863 | } |
| 4864 | p = PyBytes_AS_STRING(result) + offset; |
| 4865 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4866 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4867 | if (PyBytes_Check(rep)) { |
| 4868 | char *prep = PyBytes_AS_STRING(rep); |
| 4869 | for(k = repsize; k > 0; k--) |
| 4870 | *p++ = *prep++; |
| 4871 | } else /* rep is unicode */ { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4872 | enum PyUnicode_Kind repkind; |
| 4873 | void *repdata; |
| 4874 | |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4875 | if (PyUnicode_READY(rep) < 0) |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4876 | goto error; |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4877 | repkind = PyUnicode_KIND(rep); |
| 4878 | repdata = PyUnicode_DATA(rep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4879 | |
| 4880 | for(k=0; k<repsize; k++) { |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4881 | Py_UCS4 c = PyUnicode_READ(repkind, repdata, k); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4882 | if (0x80 <= c) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 4883 | raise_encode_exception(&exc, "utf-8", |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4884 | unicode, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 4885 | i-1, i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4886 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4887 | goto error; |
| 4888 | } |
Victor Stinner | a98b28c | 2011-11-10 20:21:49 +0100 | [diff] [blame] | 4889 | *p++ = (char)c; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4890 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4891 | } |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4892 | Py_CLEAR(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4893 | } else if (ch < 0x10000) { |
| 4894 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4895 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4896 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4897 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4898 | /* Encode UCS4 Unicode ordinals */ |
| 4899 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4900 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4901 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4902 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4903 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4905 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4906 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4907 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4908 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4909 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4910 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4911 | } |
| 4912 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4913 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4914 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4915 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4916 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4917 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4918 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4919 | Py_XDECREF(errorHandler); |
| 4920 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4921 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4922 | error: |
Antoine Pitrou | 31b92a5 | 2011-11-12 18:35:19 +0100 | [diff] [blame] | 4923 | Py_XDECREF(rep); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4924 | Py_XDECREF(errorHandler); |
| 4925 | Py_XDECREF(exc); |
| 4926 | Py_XDECREF(result); |
| 4927 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4928 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4929 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4932 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4933 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4934 | Py_ssize_t size, |
| 4935 | const char *errors) |
| 4936 | { |
| 4937 | PyObject *v, *unicode; |
| 4938 | |
| 4939 | unicode = PyUnicode_FromUnicode(s, size); |
| 4940 | if (unicode == NULL) |
| 4941 | return NULL; |
| 4942 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4943 | Py_DECREF(unicode); |
| 4944 | return v; |
| 4945 | } |
| 4946 | |
| 4947 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4948 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4949 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4950 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4954 | |
| 4955 | PyObject * |
| 4956 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4957 | Py_ssize_t size, |
| 4958 | const char *errors, |
| 4959 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4960 | { |
| 4961 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4962 | } |
| 4963 | |
| 4964 | PyObject * |
| 4965 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4966 | Py_ssize_t size, |
| 4967 | const char *errors, |
| 4968 | int *byteorder, |
| 4969 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4970 | { |
| 4971 | const char *starts = s; |
| 4972 | Py_ssize_t startinpos; |
| 4973 | Py_ssize_t endinpos; |
| 4974 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4975 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4976 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | int bo = 0; /* assume native ordering by default */ |
| 4978 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4979 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4980 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4981 | int iorder[] = {0, 1, 2, 3}; |
| 4982 | #else |
| 4983 | int iorder[] = {3, 2, 1, 0}; |
| 4984 | #endif |
| 4985 | PyObject *errorHandler = NULL; |
| 4986 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4987 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4988 | q = (unsigned char *)s; |
| 4989 | e = q + size; |
| 4990 | |
| 4991 | if (byteorder) |
| 4992 | bo = *byteorder; |
| 4993 | |
| 4994 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4995 | byte order setting accordingly. In native mode, the leading BOM |
| 4996 | mark is skipped, in all other modes, it is copied to the output |
| 4997 | stream as-is (giving a ZWNBSP character). */ |
| 4998 | if (bo == 0) { |
| 4999 | if (size >= 4) { |
| 5000 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5001 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5003 | if (bom == 0x0000FEFF) { |
| 5004 | q += 4; |
| 5005 | bo = -1; |
| 5006 | } |
| 5007 | else if (bom == 0xFFFE0000) { |
| 5008 | q += 4; |
| 5009 | bo = 1; |
| 5010 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5011 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5012 | if (bom == 0x0000FEFF) { |
| 5013 | q += 4; |
| 5014 | bo = 1; |
| 5015 | } |
| 5016 | else if (bom == 0xFFFE0000) { |
| 5017 | q += 4; |
| 5018 | bo = -1; |
| 5019 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5020 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | if (bo == -1) { |
| 5025 | /* force LE */ |
| 5026 | iorder[0] = 0; |
| 5027 | iorder[1] = 1; |
| 5028 | iorder[2] = 2; |
| 5029 | iorder[3] = 3; |
| 5030 | } |
| 5031 | else if (bo == 1) { |
| 5032 | /* force BE */ |
| 5033 | iorder[0] = 3; |
| 5034 | iorder[1] = 2; |
| 5035 | iorder[2] = 1; |
| 5036 | iorder[3] = 0; |
| 5037 | } |
| 5038 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5039 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5040 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5041 | if (!unicode) |
| 5042 | return NULL; |
| 5043 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5044 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5045 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5046 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5047 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | Py_UCS4 ch; |
| 5049 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5050 | if (e-q<4) { |
| 5051 | if (consumed) |
| 5052 | break; |
| 5053 | errmsg = "truncated data"; |
| 5054 | startinpos = ((const char *)q)-starts; |
| 5055 | endinpos = ((const char *)e)-starts; |
| 5056 | goto utf32Error; |
| 5057 | /* The remaining input chars are ignored if the callback |
| 5058 | chooses to skip the input */ |
| 5059 | } |
| 5060 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5061 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | if (ch >= 0x110000) |
| 5064 | { |
| 5065 | errmsg = "codepoint not in range(0x110000)"; |
| 5066 | startinpos = ((const char *)q)-starts; |
| 5067 | endinpos = startinpos+4; |
| 5068 | goto utf32Error; |
| 5069 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5070 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5071 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5072 | q += 4; |
| 5073 | continue; |
| 5074 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | if (unicode_decode_call_errorhandler( |
| 5076 | errors, &errorHandler, |
| 5077 | "utf32", errmsg, |
| 5078 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5079 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | if (byteorder) |
| 5084 | *byteorder = bo; |
| 5085 | |
| 5086 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5088 | |
| 5089 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5090 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | goto onError; |
| 5092 | |
| 5093 | Py_XDECREF(errorHandler); |
| 5094 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5095 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5097 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5098 | Py_DECREF(unicode); |
| 5099 | Py_XDECREF(errorHandler); |
| 5100 | Py_XDECREF(exc); |
| 5101 | return NULL; |
| 5102 | } |
| 5103 | |
| 5104 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5105 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5106 | const char *errors, |
| 5107 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5108 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5109 | int kind; |
| 5110 | void *data; |
| 5111 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5112 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5114 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5115 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5116 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5117 | int iorder[] = {0, 1, 2, 3}; |
| 5118 | #else |
| 5119 | int iorder[] = {3, 2, 1, 0}; |
| 5120 | #endif |
| 5121 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5122 | #define STORECHAR(CH) \ |
| 5123 | do { \ |
| 5124 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5125 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5126 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5127 | p[iorder[0]] = (CH) & 0xff; \ |
| 5128 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5129 | } while(0) |
| 5130 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5131 | if (!PyUnicode_Check(str)) { |
| 5132 | PyErr_BadArgument(); |
| 5133 | return NULL; |
| 5134 | } |
| 5135 | if (PyUnicode_READY(str) < 0) |
| 5136 | return NULL; |
| 5137 | kind = PyUnicode_KIND(str); |
| 5138 | data = PyUnicode_DATA(str); |
| 5139 | len = PyUnicode_GET_LENGTH(str); |
| 5140 | |
| 5141 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5142 | bytesize = nsize * 4; |
| 5143 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5144 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5145 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5146 | if (v == NULL) |
| 5147 | return NULL; |
| 5148 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5149 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5150 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5151 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5152 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5153 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5154 | |
| 5155 | if (byteorder == -1) { |
| 5156 | /* force LE */ |
| 5157 | iorder[0] = 0; |
| 5158 | iorder[1] = 1; |
| 5159 | iorder[2] = 2; |
| 5160 | iorder[3] = 3; |
| 5161 | } |
| 5162 | else if (byteorder == 1) { |
| 5163 | /* force BE */ |
| 5164 | iorder[0] = 3; |
| 5165 | iorder[1] = 2; |
| 5166 | iorder[2] = 1; |
| 5167 | iorder[3] = 0; |
| 5168 | } |
| 5169 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5170 | for (i = 0; i < len; i++) |
| 5171 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5172 | |
| 5173 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5174 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5175 | #undef STORECHAR |
| 5176 | } |
| 5177 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5178 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5179 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5180 | Py_ssize_t size, |
| 5181 | const char *errors, |
| 5182 | int byteorder) |
| 5183 | { |
| 5184 | PyObject *result; |
| 5185 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5186 | if (tmp == NULL) |
| 5187 | return NULL; |
| 5188 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5189 | Py_DECREF(tmp); |
| 5190 | return result; |
| 5191 | } |
| 5192 | |
| 5193 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5194 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5195 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5196 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5200 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5201 | PyObject * |
| 5202 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 | Py_ssize_t size, |
| 5204 | const char *errors, |
| 5205 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5206 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5207 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5208 | } |
| 5209 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5210 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5211 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5212 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5213 | rare in most input. |
| 5214 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5215 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5216 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5217 | #if (SIZEOF_LONG == 8) |
| 5218 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5219 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5220 | #elif (SIZEOF_LONG == 4) |
| 5221 | # define FAST_CHAR_MASK 0x80008000L |
| 5222 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5223 | #else |
| 5224 | # error C 'long' size should be either 4 or 8! |
| 5225 | #endif |
| 5226 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5227 | PyObject * |
| 5228 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5229 | Py_ssize_t size, |
| 5230 | const char *errors, |
| 5231 | int *byteorder, |
| 5232 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5233 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5234 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5235 | Py_ssize_t startinpos; |
| 5236 | Py_ssize_t endinpos; |
| 5237 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5238 | PyObject *unicode; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5239 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5240 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5241 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5242 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5243 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5244 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5245 | int ihi = 1, ilo = 0; |
| 5246 | #else |
| 5247 | int ihi = 0, ilo = 1; |
| 5248 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5249 | PyObject *errorHandler = NULL; |
| 5250 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* Note: size will always be longer than the resulting Unicode |
| 5253 | character count */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5254 | unicode = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | if (!unicode) |
| 5256 | return NULL; |
| 5257 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5258 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5259 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5261 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5262 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5263 | |
| 5264 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5265 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5267 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5268 | byte order setting accordingly. In native mode, the leading BOM |
| 5269 | mark is skipped, in all other modes, it is copied to the output |
| 5270 | stream as-is (giving a ZWNBSP character). */ |
| 5271 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5272 | if (size >= 2) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5273 | const Py_UCS4 bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5274 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5275 | if (bom == 0xFEFF) { |
| 5276 | q += 2; |
| 5277 | bo = -1; |
| 5278 | } |
| 5279 | else if (bom == 0xFFFE) { |
| 5280 | q += 2; |
| 5281 | bo = 1; |
| 5282 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5283 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | if (bom == 0xFEFF) { |
| 5285 | q += 2; |
| 5286 | bo = 1; |
| 5287 | } |
| 5288 | else if (bom == 0xFFFE) { |
| 5289 | q += 2; |
| 5290 | bo = -1; |
| 5291 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5292 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5293 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5294 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5295 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5296 | if (bo == -1) { |
| 5297 | /* force LE */ |
| 5298 | ihi = 1; |
| 5299 | ilo = 0; |
| 5300 | } |
| 5301 | else if (bo == 1) { |
| 5302 | /* force BE */ |
| 5303 | ihi = 0; |
| 5304 | ilo = 1; |
| 5305 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5306 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5307 | native_ordering = ilo < ihi; |
| 5308 | #else |
| 5309 | native_ordering = ilo > ihi; |
| 5310 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5311 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5312 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5313 | while (q < e) { |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5314 | Py_UCS4 ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5315 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5316 | reads are more expensive, better to defer to another iteration. */ |
| 5317 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5318 | /* Fast path for runs of non-surrogate chars. */ |
| 5319 | register const unsigned char *_q = q; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5320 | int kind = PyUnicode_KIND(unicode); |
| 5321 | void *data = PyUnicode_DATA(unicode); |
| 5322 | while (_q < aligned_end) { |
| 5323 | unsigned long block = * (unsigned long *) _q; |
| 5324 | unsigned short *pblock = (unsigned short*)█ |
| 5325 | Py_UCS4 maxch; |
| 5326 | if (native_ordering) { |
| 5327 | /* Can use buffer directly */ |
| 5328 | if (block & FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5329 | break; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5330 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5331 | else { |
| 5332 | /* Need to byte-swap */ |
| 5333 | unsigned char *_p = (unsigned char*)pblock; |
| 5334 | if (block & SWAPPED_FAST_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5335 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5336 | _p[0] = _q[1]; |
| 5337 | _p[1] = _q[0]; |
| 5338 | _p[2] = _q[3]; |
| 5339 | _p[3] = _q[2]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5340 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5341 | _p[4] = _q[5]; |
| 5342 | _p[5] = _q[4]; |
| 5343 | _p[6] = _q[7]; |
| 5344 | _p[7] = _q[6]; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5345 | #endif |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5346 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5347 | maxch = Py_MAX(pblock[0], pblock[1]); |
| 5348 | #if SIZEOF_LONG == 8 |
| 5349 | maxch = Py_MAX(maxch, Py_MAX(pblock[2], pblock[3])); |
| 5350 | #endif |
| 5351 | if (maxch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 5352 | if (unicode_widen(&unicode, maxch) < 0) |
| 5353 | goto onError; |
| 5354 | kind = PyUnicode_KIND(unicode); |
| 5355 | data = PyUnicode_DATA(unicode); |
| 5356 | } |
| 5357 | PyUnicode_WRITE(kind, data, outpos++, pblock[0]); |
| 5358 | PyUnicode_WRITE(kind, data, outpos++, pblock[1]); |
| 5359 | #if SIZEOF_LONG == 8 |
| 5360 | PyUnicode_WRITE(kind, data, outpos++, pblock[2]); |
| 5361 | PyUnicode_WRITE(kind, data, outpos++, pblock[3]); |
| 5362 | #endif |
| 5363 | _q += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5364 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5365 | q = _q; |
| 5366 | if (q >= e) |
| 5367 | break; |
| 5368 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5369 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5371 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | |
| 5373 | if (ch < 0xD800 || ch > 0xDFFF) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5374 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5375 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | continue; |
| 5377 | } |
| 5378 | |
| 5379 | /* UTF-16 code pair: */ |
| 5380 | if (q > e) { |
| 5381 | errmsg = "unexpected end of data"; |
| 5382 | startinpos = (((const char *)q) - 2) - starts; |
| 5383 | endinpos = ((const char *)e) + 1 - starts; |
| 5384 | goto utf16Error; |
| 5385 | } |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5386 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { |
| 5387 | Py_UCS4 ch2 = (q[ihi] << 8) | q[ilo]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5388 | q += 2; |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5389 | if (Py_UNICODE_IS_LOW_SURROGATE(ch2)) { |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 5390 | if (unicode_putchar(&unicode, &outpos, |
Victor Stinner | 2e9cfad | 2011-11-20 18:40:27 +0100 | [diff] [blame] | 5391 | Py_UNICODE_JOIN_SURROGATES(ch, ch2)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5392 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5393 | continue; |
| 5394 | } |
| 5395 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5396 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5397 | startinpos = (((const char *)q)-4)-starts; |
| 5398 | endinpos = startinpos+2; |
| 5399 | goto utf16Error; |
| 5400 | } |
| 5401 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5402 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5403 | errmsg = "illegal encoding"; |
| 5404 | startinpos = (((const char *)q)-2)-starts; |
| 5405 | endinpos = startinpos+2; |
| 5406 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5407 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | utf16Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5409 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5410 | errors, |
| 5411 | &errorHandler, |
| 5412 | "utf16", errmsg, |
| 5413 | &starts, |
| 5414 | (const char **)&e, |
| 5415 | &startinpos, |
| 5416 | &endinpos, |
| 5417 | &exc, |
| 5418 | (const char **)&q, |
| 5419 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5420 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5423 | /* remaining byte at the end? (size should be even) */ |
| 5424 | if (e == q) { |
| 5425 | if (!consumed) { |
| 5426 | errmsg = "truncated data"; |
| 5427 | startinpos = ((const char *)q) - starts; |
| 5428 | endinpos = ((const char *)e) + 1 - starts; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5429 | if (unicode_decode_call_errorhandler( |
| 5430 | errors, |
| 5431 | &errorHandler, |
| 5432 | "utf16", errmsg, |
| 5433 | &starts, |
| 5434 | (const char **)&e, |
| 5435 | &startinpos, |
| 5436 | &endinpos, |
| 5437 | &exc, |
| 5438 | (const char **)&q, |
| 5439 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5440 | &outpos)) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5441 | goto onError; |
| 5442 | /* The remaining input chars are ignored if the callback |
| 5443 | chooses to skip the input */ |
| 5444 | } |
| 5445 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | |
| 5447 | if (byteorder) |
| 5448 | *byteorder = bo; |
| 5449 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5450 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | /* Adjust length */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5454 | if (PyUnicode_Resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5455 | goto onError; |
| 5456 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5457 | Py_XDECREF(errorHandler); |
| 5458 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5459 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5462 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5463 | Py_XDECREF(errorHandler); |
| 5464 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | return NULL; |
| 5466 | } |
| 5467 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5468 | #undef FAST_CHAR_MASK |
| 5469 | #undef SWAPPED_FAST_CHAR_MASK |
| 5470 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5471 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5472 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5473 | const char *errors, |
| 5474 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5475 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5476 | int kind; |
| 5477 | void *data; |
| 5478 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5479 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5480 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5481 | Py_ssize_t nsize, bytesize; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5482 | Py_ssize_t i, pairs; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5483 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5484 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5485 | int ihi = 1, ilo = 0; |
| 5486 | #else |
| 5487 | int ihi = 0, ilo = 1; |
| 5488 | #endif |
| 5489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5490 | #define STORECHAR(CH) \ |
| 5491 | do { \ |
| 5492 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5493 | p[ilo] = (CH) & 0xff; \ |
| 5494 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5495 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5497 | if (!PyUnicode_Check(str)) { |
| 5498 | PyErr_BadArgument(); |
| 5499 | return NULL; |
| 5500 | } |
| 5501 | if (PyUnicode_READY(str) < 0) |
| 5502 | return NULL; |
| 5503 | kind = PyUnicode_KIND(str); |
| 5504 | data = PyUnicode_DATA(str); |
| 5505 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5506 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5507 | pairs = 0; |
| 5508 | if (kind == PyUnicode_4BYTE_KIND) |
| 5509 | for (i = 0; i < len; i++) |
| 5510 | if (PyUnicode_READ(kind, data, i) >= 0x10000) |
| 5511 | pairs++; |
| 5512 | /* 2 * (len + pairs + (byteorder == 0)) */ |
| 5513 | if (len > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | return PyErr_NoMemory(); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5515 | nsize = len + pairs + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5516 | bytesize = nsize * 2; |
| 5517 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5518 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5519 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | if (v == NULL) |
| 5521 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5523 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5526 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5527 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5528 | |
| 5529 | if (byteorder == -1) { |
| 5530 | /* force LE */ |
| 5531 | ihi = 1; |
| 5532 | ilo = 0; |
| 5533 | } |
| 5534 | else if (byteorder == 1) { |
| 5535 | /* force BE */ |
| 5536 | ihi = 0; |
| 5537 | ilo = 1; |
| 5538 | } |
| 5539 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5540 | for (i = 0; i < len; i++) { |
| 5541 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5542 | Py_UCS4 ch2 = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5543 | if (ch >= 0x10000) { |
| 5544 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5545 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5546 | } |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5547 | STORECHAR(ch); |
| 5548 | if (ch2) |
| 5549 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5550 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5551 | |
| 5552 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5553 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5554 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5557 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5558 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5559 | Py_ssize_t size, |
| 5560 | const char *errors, |
| 5561 | int byteorder) |
| 5562 | { |
| 5563 | PyObject *result; |
| 5564 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5565 | if (tmp == NULL) |
| 5566 | return NULL; |
| 5567 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5568 | Py_DECREF(tmp); |
| 5569 | return result; |
| 5570 | } |
| 5571 | |
| 5572 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5573 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5575 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5579 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5580 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5581 | if all the escapes in the string make it still a valid ASCII string. |
| 5582 | Returns -1 if any escapes were found which cause the string to |
| 5583 | pop out of ASCII range. Otherwise returns the length of the |
| 5584 | required buffer to hold the string. |
| 5585 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5586 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5587 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5588 | { |
| 5589 | const unsigned char *p = (const unsigned char *)s; |
| 5590 | const unsigned char *end = p + size; |
| 5591 | Py_ssize_t length = 0; |
| 5592 | |
| 5593 | if (size < 0) |
| 5594 | return -1; |
| 5595 | |
| 5596 | for (; p < end; ++p) { |
| 5597 | if (*p > 127) { |
| 5598 | /* Non-ASCII */ |
| 5599 | return -1; |
| 5600 | } |
| 5601 | else if (*p != '\\') { |
| 5602 | /* Normal character */ |
| 5603 | ++length; |
| 5604 | } |
| 5605 | else { |
| 5606 | /* Backslash-escape, check next char */ |
| 5607 | ++p; |
| 5608 | /* Escape sequence reaches till end of string or |
| 5609 | non-ASCII follow-up. */ |
| 5610 | if (p >= end || *p > 127) |
| 5611 | return -1; |
| 5612 | switch (*p) { |
| 5613 | case '\n': |
| 5614 | /* backslash + \n result in zero characters */ |
| 5615 | break; |
| 5616 | case '\\': case '\'': case '\"': |
| 5617 | case 'b': case 'f': case 't': |
| 5618 | case 'n': case 'r': case 'v': case 'a': |
| 5619 | ++length; |
| 5620 | break; |
| 5621 | case '0': case '1': case '2': case '3': |
| 5622 | case '4': case '5': case '6': case '7': |
| 5623 | case 'x': case 'u': case 'U': case 'N': |
| 5624 | /* these do not guarantee ASCII characters */ |
| 5625 | return -1; |
| 5626 | default: |
| 5627 | /* count the backslash + the other character */ |
| 5628 | length += 2; |
| 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | return length; |
| 5633 | } |
| 5634 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5635 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5636 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5637 | PyObject * |
| 5638 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5639 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5640 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5642 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5643 | Py_ssize_t startinpos; |
| 5644 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5645 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5646 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5648 | char* message; |
| 5649 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5650 | PyObject *errorHandler = NULL; |
| 5651 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5652 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5653 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5654 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5655 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5656 | |
| 5657 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5658 | either the string is pure ASCII with named escapes like \n, etc. |
| 5659 | and we determined it's exact size (common case) |
| 5660 | or it contains \x, \u, ... escape sequences. then we create a |
| 5661 | 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] | 5662 | if (len >= 0) { |
| 5663 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5664 | if (!v) |
| 5665 | goto onError; |
| 5666 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5667 | } |
| 5668 | else { |
| 5669 | /* Escaped strings will always be longer than the resulting |
| 5670 | Unicode string, so we start with size here and then reduce the |
| 5671 | length after conversion to the true value. |
| 5672 | (but if the error callback returns a long replacement string |
| 5673 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5674 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5675 | if (!v) |
| 5676 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5677 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5678 | } |
| 5679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5681 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5682 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5685 | while (s < end) { |
| 5686 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5687 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5688 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5690 | /* The only case in which i == ascii_length is a backslash |
| 5691 | followed by a newline. */ |
| 5692 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5695 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5696 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5697 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | continue; |
| 5699 | } |
| 5700 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5701 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | /* \ - Escapes */ |
| 5703 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5704 | c = *s++; |
| 5705 | if (s > end) |
| 5706 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5707 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5708 | /* The only case in which i == ascii_length is a backslash |
| 5709 | followed by a newline. */ |
| 5710 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5711 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5712 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5715 | #define WRITECHAR(ch) \ |
| 5716 | do { \ |
| 5717 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5718 | goto onError; \ |
| 5719 | }while(0) |
| 5720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5722 | case '\\': WRITECHAR('\\'); break; |
| 5723 | case '\'': WRITECHAR('\''); break; |
| 5724 | case '\"': WRITECHAR('\"'); break; |
| 5725 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5726 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5727 | case 'f': WRITECHAR('\014'); break; |
| 5728 | case 't': WRITECHAR('\t'); break; |
| 5729 | case 'n': WRITECHAR('\n'); break; |
| 5730 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5731 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5732 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5733 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5734 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | case '0': case '1': case '2': case '3': |
| 5738 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5739 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5740 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5741 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5742 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5743 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5745 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | break; |
| 5747 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | /* hex escapes */ |
| 5749 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5751 | digits = 2; |
| 5752 | message = "truncated \\xXX escape"; |
| 5753 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5757 | digits = 4; |
| 5758 | message = "truncated \\uXXXX escape"; |
| 5759 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5761 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5762 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5763 | digits = 8; |
| 5764 | message = "truncated \\UXXXXXXXX escape"; |
| 5765 | hexescape: |
| 5766 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5767 | if (s+digits>end) { |
| 5768 | endinpos = size; |
| 5769 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | errors, &errorHandler, |
| 5771 | "unicodeescape", "end of string in escape sequence", |
| 5772 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5773 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5774 | goto onError; |
| 5775 | goto nextByte; |
| 5776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5777 | for (j = 0; j < digits; ++j) { |
| 5778 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5779 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5780 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5781 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | errors, &errorHandler, |
| 5783 | "unicodeescape", message, |
| 5784 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5785 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5786 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5787 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5788 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5789 | } |
| 5790 | chr = (chr<<4) & ~0xF; |
| 5791 | if (c >= '0' && c <= '9') |
| 5792 | chr += c - '0'; |
| 5793 | else if (c >= 'a' && c <= 'f') |
| 5794 | chr += 10 + c - 'a'; |
| 5795 | else |
| 5796 | chr += 10 + c - 'A'; |
| 5797 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5798 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5799 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5800 | /* _decoding_error will have already written into the |
| 5801 | target buffer. */ |
| 5802 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5803 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5804 | /* 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] | 5805 | if (chr <= 0x10ffff) { |
| 5806 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5807 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5808 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5809 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5810 | errors, &errorHandler, |
| 5811 | "unicodeescape", "illegal Unicode character", |
| 5812 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5813 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5814 | goto onError; |
| 5815 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5816 | break; |
| 5817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5819 | case 'N': |
| 5820 | message = "malformed \\N character escape"; |
| 5821 | if (ucnhash_CAPI == NULL) { |
| 5822 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5823 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5824 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5825 | if (ucnhash_CAPI == NULL) |
| 5826 | goto ucnhashError; |
| 5827 | } |
| 5828 | if (*s == '{') { |
| 5829 | const char *start = s+1; |
| 5830 | /* look for the closing brace */ |
| 5831 | while (*s != '}' && s < end) |
| 5832 | s++; |
| 5833 | if (s > start && s < end && *s == '}') { |
| 5834 | /* found a name. look it up in the unicode database */ |
| 5835 | message = "unknown Unicode character name"; |
| 5836 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5837 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5838 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5839 | goto store; |
| 5840 | } |
| 5841 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5842 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5843 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | errors, &errorHandler, |
| 5845 | "unicodeescape", message, |
| 5846 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5847 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5848 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5849 | break; |
| 5850 | |
| 5851 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5852 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | message = "\\ at end of string"; |
| 5854 | s--; |
| 5855 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5857 | errors, &errorHandler, |
| 5858 | "unicodeescape", message, |
| 5859 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5860 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5861 | goto onError; |
| 5862 | } |
| 5863 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5864 | WRITECHAR('\\'); |
| 5865 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5866 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5867 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5869 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5870 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5872 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5873 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5874 | if (PyUnicode_Resize(&v, i) < 0) |
| 5875 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5876 | Py_XDECREF(errorHandler); |
| 5877 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5878 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5879 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5881 | PyErr_SetString( |
| 5882 | PyExc_UnicodeError, |
| 5883 | "\\N escapes not supported (can't load unicodedata module)" |
| 5884 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5885 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5886 | Py_XDECREF(errorHandler); |
| 5887 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5888 | return NULL; |
| 5889 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5890 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5892 | Py_XDECREF(errorHandler); |
| 5893 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | return NULL; |
| 5895 | } |
| 5896 | |
| 5897 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5898 | |
| 5899 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5900 | appropriate. |
| 5901 | |
| 5902 | */ |
| 5903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5904 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5905 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5906 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5907 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5908 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5910 | int kind; |
| 5911 | void *data; |
| 5912 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5914 | /* Initial allocation is based on the longest-possible unichr |
| 5915 | escape. |
| 5916 | |
| 5917 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5918 | unichr, so in this case it's the longest unichr escape. In |
| 5919 | narrow (UTF-16) builds this is five chars per source unichr |
| 5920 | since there are two unichrs in the surrogate pair, so in narrow |
| 5921 | (UTF-16) builds it's not the longest unichr escape. |
| 5922 | |
| 5923 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5924 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5925 | escape. |
| 5926 | */ |
| 5927 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5928 | if (!PyUnicode_Check(unicode)) { |
| 5929 | PyErr_BadArgument(); |
| 5930 | return NULL; |
| 5931 | } |
| 5932 | if (PyUnicode_READY(unicode) < 0) |
| 5933 | return NULL; |
| 5934 | len = PyUnicode_GET_LENGTH(unicode); |
| 5935 | kind = PyUnicode_KIND(unicode); |
| 5936 | data = PyUnicode_DATA(unicode); |
| 5937 | switch(kind) { |
| 5938 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5939 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5940 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5941 | } |
| 5942 | |
| 5943 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5944 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5945 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5946 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5948 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5949 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5950 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5951 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | if (repr == NULL) |
| 5954 | return NULL; |
| 5955 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5956 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5958 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5959 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5960 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5961 | /* Escape backslashes */ |
| 5962 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | *p++ = '\\'; |
| 5964 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5965 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5966 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5967 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5968 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5969 | else if (ch >= 0x10000) { |
| 5970 | *p++ = '\\'; |
| 5971 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5972 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5973 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5974 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5975 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5976 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5977 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5978 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5979 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5980 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5981 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5984 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | *p++ = '\\'; |
| 5986 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5987 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5988 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5989 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5990 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5992 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5993 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5994 | else if (ch == '\t') { |
| 5995 | *p++ = '\\'; |
| 5996 | *p++ = 't'; |
| 5997 | } |
| 5998 | else if (ch == '\n') { |
| 5999 | *p++ = '\\'; |
| 6000 | *p++ = 'n'; |
| 6001 | } |
| 6002 | else if (ch == '\r') { |
| 6003 | *p++ = '\\'; |
| 6004 | *p++ = 'r'; |
| 6005 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6006 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6007 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6008 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6010 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6011 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6012 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6013 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | /* Copy everything else as-is */ |
| 6016 | else |
| 6017 | *p++ = (char) ch; |
| 6018 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6020 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6021 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6022 | return NULL; |
| 6023 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6026 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6027 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6028 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6030 | PyObject *result; |
| 6031 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6032 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6034 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6035 | Py_DECREF(tmp); |
| 6036 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6041 | PyObject * |
| 6042 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6043 | Py_ssize_t size, |
| 6044 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6046 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6047 | Py_ssize_t startinpos; |
| 6048 | Py_ssize_t endinpos; |
| 6049 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6050 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | const char *end; |
| 6052 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6053 | PyObject *errorHandler = NULL; |
| 6054 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | /* Escaped strings will always be longer than the resulting |
| 6057 | 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] | 6058 | length after conversion to the true value. (But decoding error |
| 6059 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6060 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6062 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6064 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6065 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | end = s + size; |
| 6067 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6068 | unsigned char c; |
| 6069 | Py_UCS4 x; |
| 6070 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6071 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6074 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6075 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6076 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6078 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | startinpos = s-starts; |
| 6080 | |
| 6081 | /* \u-escapes are only interpreted iff the number of leading |
| 6082 | backslashes if odd */ |
| 6083 | bs = s; |
| 6084 | for (;s < end;) { |
| 6085 | if (*s != '\\') |
| 6086 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6087 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 6088 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | } |
| 6090 | if (((s - bs) & 1) == 0 || |
| 6091 | s >= end || |
| 6092 | (*s != 'u' && *s != 'U')) { |
| 6093 | continue; |
| 6094 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6095 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | count = *s=='u' ? 4 : 8; |
| 6097 | s++; |
| 6098 | |
| 6099 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6100 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6101 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6102 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | endinpos = s-starts; |
| 6104 | if (unicode_decode_call_errorhandler( |
| 6105 | errors, &errorHandler, |
| 6106 | "rawunicodeescape", "truncated \\uXXXX", |
| 6107 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6108 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | goto onError; |
| 6110 | goto nextByte; |
| 6111 | } |
| 6112 | x = (x<<4) & ~0xF; |
| 6113 | if (c >= '0' && c <= '9') |
| 6114 | x += c - '0'; |
| 6115 | else if (c >= 'a' && c <= 'f') |
| 6116 | x += 10 + c - 'a'; |
| 6117 | else |
| 6118 | x += 10 + c - 'A'; |
| 6119 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6120 | if (x <= 0x10ffff) { |
| 6121 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6122 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6123 | } else { |
| 6124 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6125 | if (unicode_decode_call_errorhandler( |
| 6126 | errors, &errorHandler, |
| 6127 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6129 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6131 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | nextByte: |
| 6133 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6135 | if (PyUnicode_Resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6137 | Py_XDECREF(errorHandler); |
| 6138 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6139 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6140 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6141 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | Py_XDECREF(errorHandler); |
| 6144 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | return NULL; |
| 6146 | } |
| 6147 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6149 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6150 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6152 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | char *p; |
| 6154 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6155 | Py_ssize_t expandsize, pos; |
| 6156 | int kind; |
| 6157 | void *data; |
| 6158 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6160 | if (!PyUnicode_Check(unicode)) { |
| 6161 | PyErr_BadArgument(); |
| 6162 | return NULL; |
| 6163 | } |
| 6164 | if (PyUnicode_READY(unicode) < 0) |
| 6165 | return NULL; |
| 6166 | kind = PyUnicode_KIND(unicode); |
| 6167 | data = PyUnicode_DATA(unicode); |
| 6168 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6169 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6170 | switch(kind) { |
| 6171 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6172 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6173 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6174 | } |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6175 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6176 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6177 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6178 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6179 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | if (repr == NULL) |
| 6181 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6182 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6183 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6185 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6186 | for (pos = 0; pos < len; pos++) { |
| 6187 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6188 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6189 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6190 | *p++ = '\\'; |
| 6191 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6192 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6193 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6194 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6195 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6196 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6197 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6198 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6199 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6200 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6202 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | *p++ = '\\'; |
| 6204 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6205 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6206 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6207 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6208 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6210 | /* Copy everything else as-is */ |
| 6211 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | *p++ = (char) ch; |
| 6213 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6214 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6215 | assert(p > q); |
| 6216 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6217 | return NULL; |
| 6218 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6221 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6222 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6223 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6225 | PyObject *result; |
| 6226 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6227 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6228 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6229 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6230 | Py_DECREF(tmp); |
| 6231 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6234 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6235 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6236 | PyObject * |
| 6237 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6238 | Py_ssize_t size, |
| 6239 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6240 | { |
| 6241 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6242 | Py_ssize_t startinpos; |
| 6243 | Py_ssize_t endinpos; |
| 6244 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6245 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6246 | const char *end; |
| 6247 | const char *reason; |
| 6248 | PyObject *errorHandler = NULL; |
| 6249 | PyObject *exc = NULL; |
| 6250 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6251 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6252 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6253 | 1)) |
| 6254 | return NULL; |
| 6255 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6256 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6257 | 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] | 6258 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6260 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6261 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6262 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6263 | end = s + size; |
| 6264 | |
| 6265 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6266 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6267 | Py_UCS4 ch; |
| 6268 | /* We copy the raw representation one byte at a time because the |
| 6269 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6270 | ((char *) &uch)[0] = s[0]; |
| 6271 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6272 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6273 | ((char *) &uch)[2] = s[2]; |
| 6274 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6275 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6276 | ch = uch; |
| 6277 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6278 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6279 | some malformed UCS-4 data. */ |
| 6280 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6282 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6284 | end-s < Py_UNICODE_SIZE |
| 6285 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6287 | startinpos = s - starts; |
| 6288 | if (end-s < Py_UNICODE_SIZE) { |
| 6289 | endinpos = end-starts; |
| 6290 | reason = "truncated input"; |
| 6291 | } |
| 6292 | else { |
| 6293 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6294 | reason = "illegal code point (> 0x10FFFF)"; |
| 6295 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6296 | if (unicode_decode_call_errorhandler( |
| 6297 | errors, &errorHandler, |
| 6298 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6299 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6300 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6301 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6302 | continue; |
| 6303 | } |
| 6304 | |
| 6305 | s += Py_UNICODE_SIZE; |
| 6306 | #ifndef Py_UNICODE_WIDE |
| 6307 | if (ch >= 0xD800 && ch <= 0xDBFF && s < end) |
| 6308 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6309 | Py_UNICODE uch2; |
| 6310 | ((char *) &uch2)[0] = s[0]; |
| 6311 | ((char *) &uch2)[1] = s[1]; |
| 6312 | if (uch2 >= 0xDC00 && uch2 <= 0xDFFF) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6313 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6314 | ch = (((uch & 0x3FF)<<10) | (uch2 & 0x3FF)) + 0x10000; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6315 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6316 | } |
| 6317 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6318 | #endif |
| 6319 | |
| 6320 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6321 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6324 | if (PyUnicode_Resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6325 | goto onError; |
| 6326 | Py_XDECREF(errorHandler); |
| 6327 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6328 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6330 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6331 | Py_XDECREF(v); |
| 6332 | Py_XDECREF(errorHandler); |
| 6333 | Py_XDECREF(exc); |
| 6334 | return NULL; |
| 6335 | } |
| 6336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6337 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6338 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6339 | PyObject * |
| 6340 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6341 | Py_ssize_t size, |
| 6342 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6343 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6345 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6348 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6349 | static void |
| 6350 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6351 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6352 | PyObject *unicode, |
| 6353 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6354 | const char *reason) |
| 6355 | { |
| 6356 | if (*exceptionObject == NULL) { |
| 6357 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6358 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6359 | encoding, unicode, startpos, endpos, reason); |
| 6360 | } |
| 6361 | else { |
| 6362 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6363 | goto onError; |
| 6364 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6365 | goto onError; |
| 6366 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6367 | goto onError; |
| 6368 | return; |
| 6369 | onError: |
| 6370 | Py_DECREF(*exceptionObject); |
| 6371 | *exceptionObject = NULL; |
| 6372 | } |
| 6373 | } |
| 6374 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6375 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6376 | static void |
| 6377 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6378 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6379 | PyObject *unicode, |
| 6380 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6381 | const char *reason) |
| 6382 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6383 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6384 | encoding, unicode, startpos, endpos, reason); |
| 6385 | if (*exceptionObject != NULL) |
| 6386 | PyCodec_StrictErrors(*exceptionObject); |
| 6387 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6388 | |
| 6389 | /* error handling callback helper: |
| 6390 | build arguments, call the callback and check the arguments, |
| 6391 | put the result into newpos and return the replacement string, which |
| 6392 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6393 | static PyObject * |
| 6394 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6395 | PyObject **errorHandler, |
| 6396 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6397 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6398 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6399 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6400 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6401 | 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] | 6402 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6403 | PyObject *restuple; |
| 6404 | PyObject *resunicode; |
| 6405 | |
| 6406 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6412 | if (PyUnicode_READY(unicode) < 0) |
| 6413 | return NULL; |
| 6414 | len = PyUnicode_GET_LENGTH(unicode); |
| 6415 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6416 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6417 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6418 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6419 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6420 | |
| 6421 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6422 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6423 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6426 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | Py_DECREF(restuple); |
| 6428 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6429 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6430 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | &resunicode, newpos)) { |
| 6432 | Py_DECREF(restuple); |
| 6433 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6434 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6435 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6436 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6437 | Py_DECREF(restuple); |
| 6438 | return NULL; |
| 6439 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6440 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6441 | *newpos = len + *newpos; |
| 6442 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6444 | Py_DECREF(restuple); |
| 6445 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6446 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6447 | Py_INCREF(resunicode); |
| 6448 | Py_DECREF(restuple); |
| 6449 | return resunicode; |
| 6450 | } |
| 6451 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6452 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6453 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6454 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6455 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6456 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6457 | /* input state */ |
| 6458 | Py_ssize_t pos=0, size; |
| 6459 | int kind; |
| 6460 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6461 | /* output object */ |
| 6462 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6463 | /* pointer into the output */ |
| 6464 | char *str; |
| 6465 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6466 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6467 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6468 | 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] | 6469 | PyObject *errorHandler = NULL; |
| 6470 | PyObject *exc = NULL; |
| 6471 | /* the following variable is used for caching string comparisons |
| 6472 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6473 | int known_errorHandler = -1; |
| 6474 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6475 | if (PyUnicode_READY(unicode) < 0) |
| 6476 | return NULL; |
| 6477 | size = PyUnicode_GET_LENGTH(unicode); |
| 6478 | kind = PyUnicode_KIND(unicode); |
| 6479 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6480 | /* allocate enough for a simple encoding without |
| 6481 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6482 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6483 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6484 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6485 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6486 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6487 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6488 | ressize = size; |
| 6489 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6490 | while (pos < size) { |
| 6491 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6492 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6493 | /* can we encode this? */ |
| 6494 | if (c<limit) { |
| 6495 | /* no overflow check, because we know that the space is enough */ |
| 6496 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6497 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6498 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6500 | Py_ssize_t requiredsize; |
| 6501 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6502 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6503 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6504 | Py_ssize_t collstart = pos; |
| 6505 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6506 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6507 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6508 | ++collend; |
| 6509 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6510 | if (known_errorHandler==-1) { |
| 6511 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6512 | known_errorHandler = 1; |
| 6513 | else if (!strcmp(errors, "replace")) |
| 6514 | known_errorHandler = 2; |
| 6515 | else if (!strcmp(errors, "ignore")) |
| 6516 | known_errorHandler = 3; |
| 6517 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6518 | known_errorHandler = 4; |
| 6519 | else |
| 6520 | known_errorHandler = 0; |
| 6521 | } |
| 6522 | switch (known_errorHandler) { |
| 6523 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6524 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | goto onError; |
| 6526 | case 2: /* replace */ |
| 6527 | while (collstart++<collend) |
| 6528 | *str++ = '?'; /* fall through */ |
| 6529 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6530 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6531 | break; |
| 6532 | case 4: /* xmlcharrefreplace */ |
| 6533 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | /* determine replacement size */ |
| 6535 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6536 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6537 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6541 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6543 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6545 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | else |
| 6547 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6548 | #else |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6549 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6551 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | repsize += 2+6+1; |
| 6553 | else |
| 6554 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6555 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6557 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 | if (requiredsize > ressize) { |
| 6559 | if (requiredsize<2*ressize) |
| 6560 | requiredsize = 2*ressize; |
| 6561 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6562 | goto onError; |
| 6563 | str = PyBytes_AS_STRING(res) + respos; |
| 6564 | ressize = requiredsize; |
| 6565 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6566 | /* generate replacement */ |
| 6567 | for (i = collstart; i < collend; ++i) { |
| 6568 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6570 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6571 | break; |
| 6572 | default: |
| 6573 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6574 | encoding, reason, unicode, &exc, |
| 6575 | collstart, collend, &newpos); |
| 6576 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
| 6577 | PyUnicode_READY(repunicode) < 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6578 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6579 | if (PyBytes_Check(repunicode)) { |
| 6580 | /* Directly copy bytes result to output. */ |
| 6581 | repsize = PyBytes_Size(repunicode); |
| 6582 | if (repsize > 1) { |
| 6583 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6584 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6585 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6586 | Py_DECREF(repunicode); |
| 6587 | goto onError; |
| 6588 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6589 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6590 | ressize += repsize-1; |
| 6591 | } |
| 6592 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6593 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6594 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6595 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6596 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6597 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | /* need more space? (at least enough for what we |
| 6599 | have+the replacement+the rest of the string, so |
| 6600 | we won't have to check space for encodable characters) */ |
| 6601 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6602 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6603 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | if (requiredsize > ressize) { |
| 6605 | if (requiredsize<2*ressize) |
| 6606 | requiredsize = 2*ressize; |
| 6607 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6608 | Py_DECREF(repunicode); |
| 6609 | goto onError; |
| 6610 | } |
| 6611 | str = PyBytes_AS_STRING(res) + respos; |
| 6612 | ressize = requiredsize; |
| 6613 | } |
| 6614 | /* check if there is anything unencodable in the replacement |
| 6615 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6616 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6617 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6618 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6619 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6621 | Py_DECREF(repunicode); |
| 6622 | goto onError; |
| 6623 | } |
| 6624 | *str = (char)c; |
| 6625 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6626 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6627 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6628 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6629 | } |
| 6630 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6631 | /* Resize if we allocated to much */ |
| 6632 | size = str - PyBytes_AS_STRING(res); |
| 6633 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6634 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6635 | if (_PyBytes_Resize(&res, size) < 0) |
| 6636 | goto onError; |
| 6637 | } |
| 6638 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6639 | Py_XDECREF(errorHandler); |
| 6640 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6641 | return res; |
| 6642 | |
| 6643 | onError: |
| 6644 | Py_XDECREF(res); |
| 6645 | Py_XDECREF(errorHandler); |
| 6646 | Py_XDECREF(exc); |
| 6647 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6650 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6651 | PyObject * |
| 6652 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6653 | Py_ssize_t size, |
| 6654 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6656 | PyObject *result; |
| 6657 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6658 | if (unicode == NULL) |
| 6659 | return NULL; |
| 6660 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6661 | Py_DECREF(unicode); |
| 6662 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | } |
| 6664 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6665 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6666 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | { |
| 6668 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | PyErr_BadArgument(); |
| 6670 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6672 | if (PyUnicode_READY(unicode) == -1) |
| 6673 | return NULL; |
| 6674 | /* Fast path: if it is a one-byte string, construct |
| 6675 | bytes object directly. */ |
| 6676 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6677 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6678 | PyUnicode_GET_LENGTH(unicode)); |
| 6679 | /* Non-Latin-1 characters present. Defer to above function to |
| 6680 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6681 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | PyObject* |
| 6685 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6686 | { |
| 6687 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6691 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6692 | PyObject * |
| 6693 | PyUnicode_DecodeASCII(const char *s, |
| 6694 | Py_ssize_t size, |
| 6695 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | const char *starts = s; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6698 | PyObject *v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6699 | int kind; |
| 6700 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6701 | Py_ssize_t startinpos; |
| 6702 | Py_ssize_t endinpos; |
| 6703 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6704 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6705 | int has_error; |
| 6706 | const unsigned char *p = (const unsigned char *)s; |
| 6707 | const unsigned char *end = p + size; |
| 6708 | 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] | 6709 | PyObject *errorHandler = NULL; |
| 6710 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6711 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6712 | if (size == 0) { |
| 6713 | Py_INCREF(unicode_empty); |
| 6714 | return unicode_empty; |
| 6715 | } |
| 6716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6718 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6719 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6720 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6721 | has_error = 0; |
| 6722 | while (p < end && !has_error) { |
| 6723 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6724 | an explanation. */ |
| 6725 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6726 | /* Help register allocation */ |
| 6727 | register const unsigned char *_p = p; |
| 6728 | while (_p < aligned_end) { |
| 6729 | unsigned long value = *(unsigned long *) _p; |
| 6730 | if (value & ASCII_CHAR_MASK) { |
| 6731 | has_error = 1; |
| 6732 | break; |
| 6733 | } |
| 6734 | _p += SIZEOF_LONG; |
| 6735 | } |
| 6736 | if (_p == end) |
| 6737 | break; |
| 6738 | if (has_error) |
| 6739 | break; |
| 6740 | p = _p; |
| 6741 | } |
| 6742 | if (*p & 0x80) { |
| 6743 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6744 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6745 | } |
| 6746 | else { |
| 6747 | ++p; |
| 6748 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6749 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6750 | if (!has_error) |
| 6751 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6752 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6753 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6757 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6758 | kind = PyUnicode_KIND(v); |
| 6759 | data = PyUnicode_DATA(v); |
| 6760 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6761 | e = s + size; |
| 6762 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | register unsigned char c = (unsigned char)*s; |
| 6764 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6765 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6766 | ++s; |
| 6767 | } |
| 6768 | else { |
| 6769 | startinpos = s-starts; |
| 6770 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6771 | if (unicode_decode_call_errorhandler( |
| 6772 | errors, &errorHandler, |
| 6773 | "ascii", "ordinal not in range(128)", |
| 6774 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6775 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6777 | kind = PyUnicode_KIND(v); |
| 6778 | data = PyUnicode_DATA(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6781 | if (PyUnicode_Resize(&v, outpos) < 0) |
| 6782 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6783 | Py_XDECREF(errorHandler); |
| 6784 | Py_XDECREF(exc); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6785 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6786 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6787 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6788 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6790 | Py_XDECREF(errorHandler); |
| 6791 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | return NULL; |
| 6793 | } |
| 6794 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6795 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6796 | PyObject * |
| 6797 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6798 | Py_ssize_t size, |
| 6799 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6801 | PyObject *result; |
| 6802 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6803 | if (unicode == NULL) |
| 6804 | return NULL; |
| 6805 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6806 | Py_DECREF(unicode); |
| 6807 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6810 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6811 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | { |
| 6813 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6814 | PyErr_BadArgument(); |
| 6815 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6817 | if (PyUnicode_READY(unicode) == -1) |
| 6818 | return NULL; |
| 6819 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6820 | directly. Else defer to above function to raise the exception. */ |
| 6821 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6822 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6823 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6824 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6825 | } |
| 6826 | |
| 6827 | PyObject * |
| 6828 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6829 | { |
| 6830 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6833 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6834 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6835 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6836 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6837 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6838 | #define NEED_RETRY |
| 6839 | #endif |
| 6840 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6841 | #ifndef WC_ERR_INVALID_CHARS |
| 6842 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6843 | #endif |
| 6844 | |
| 6845 | static char* |
| 6846 | code_page_name(UINT code_page, PyObject **obj) |
| 6847 | { |
| 6848 | *obj = NULL; |
| 6849 | if (code_page == CP_ACP) |
| 6850 | return "mbcs"; |
| 6851 | if (code_page == CP_UTF7) |
| 6852 | return "CP_UTF7"; |
| 6853 | if (code_page == CP_UTF8) |
| 6854 | return "CP_UTF8"; |
| 6855 | |
| 6856 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6857 | if (*obj == NULL) |
| 6858 | return NULL; |
| 6859 | return PyBytes_AS_STRING(*obj); |
| 6860 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6862 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6863 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6864 | { |
| 6865 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6866 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6867 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6868 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6869 | return 0; |
| 6870 | |
| 6871 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6872 | if (prev == curr) |
| 6873 | return 1; |
| 6874 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6875 | as it assumes an incomplete character consists of a single |
| 6876 | byte. */ |
| 6877 | if (curr - prev == 2) |
| 6878 | return 1; |
| 6879 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6880 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6881 | return 0; |
| 6882 | } |
| 6883 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6884 | static DWORD |
| 6885 | decode_code_page_flags(UINT code_page) |
| 6886 | { |
| 6887 | if (code_page == CP_UTF7) { |
| 6888 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6889 | return 0; |
| 6890 | } |
| 6891 | else |
| 6892 | return MB_ERR_INVALID_CHARS; |
| 6893 | } |
| 6894 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6896 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6897 | * mode. |
| 6898 | * |
| 6899 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6900 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6902 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6903 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6904 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6905 | const char *in, |
| 6906 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6907 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6909 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | |
| 6912 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | assert(insize > 0); |
| 6914 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6915 | if (outsize <= 0) |
| 6916 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6917 | |
| 6918 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6919 | /* Create unicode object */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6920 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6921 | if (*v == NULL) |
| 6922 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6923 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | } |
| 6925 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6926 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6927 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6928 | if (PyUnicode_Resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6929 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6930 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6934 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6935 | if (outsize <= 0) |
| 6936 | goto error; |
| 6937 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6938 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6939 | error: |
| 6940 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6941 | return -2; |
| 6942 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6943 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6946 | /* |
| 6947 | * Decode a byte string from a code page into unicode object with an error |
| 6948 | * handler. |
| 6949 | * |
| 6950 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6951 | * UnicodeDecodeError exception and returns -1 on error. |
| 6952 | */ |
| 6953 | static int |
| 6954 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6955 | PyObject **v, |
| 6956 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6957 | const char *errors) |
| 6958 | { |
| 6959 | const char *startin = in; |
| 6960 | const char *endin = in + size; |
| 6961 | const DWORD flags = decode_code_page_flags(code_page); |
| 6962 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6963 | 2000 English version of the message. */ |
| 6964 | const char *reason = "No mapping for the Unicode character exists " |
| 6965 | "in the target code page."; |
| 6966 | /* each step cannot decode more than 1 character, but a character can be |
| 6967 | represented as a surrogate pair */ |
| 6968 | wchar_t buffer[2], *startout, *out; |
| 6969 | int insize, outsize; |
| 6970 | PyObject *errorHandler = NULL; |
| 6971 | PyObject *exc = NULL; |
| 6972 | PyObject *encoding_obj = NULL; |
| 6973 | char *encoding; |
| 6974 | DWORD err; |
| 6975 | int ret = -1; |
| 6976 | |
| 6977 | assert(size > 0); |
| 6978 | |
| 6979 | encoding = code_page_name(code_page, &encoding_obj); |
| 6980 | if (encoding == NULL) |
| 6981 | return -1; |
| 6982 | |
| 6983 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6984 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6985 | UnicodeDecodeError. */ |
| 6986 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6987 | if (exc != NULL) { |
| 6988 | PyCodec_StrictErrors(exc); |
| 6989 | Py_CLEAR(exc); |
| 6990 | } |
| 6991 | goto error; |
| 6992 | } |
| 6993 | |
| 6994 | if (*v == NULL) { |
| 6995 | /* Create unicode object */ |
| 6996 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6997 | PyErr_NoMemory(); |
| 6998 | goto error; |
| 6999 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7000 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7001 | if (*v == NULL) |
| 7002 | goto error; |
| 7003 | startout = PyUnicode_AS_UNICODE(*v); |
| 7004 | } |
| 7005 | else { |
| 7006 | /* Extend unicode object */ |
| 7007 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7008 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7009 | PyErr_NoMemory(); |
| 7010 | goto error; |
| 7011 | } |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7012 | if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7013 | goto error; |
| 7014 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7015 | } |
| 7016 | |
| 7017 | /* Decode the byte string character per character */ |
| 7018 | out = startout; |
| 7019 | while (in < endin) |
| 7020 | { |
| 7021 | /* Decode a character */ |
| 7022 | insize = 1; |
| 7023 | do |
| 7024 | { |
| 7025 | outsize = MultiByteToWideChar(code_page, flags, |
| 7026 | in, insize, |
| 7027 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7028 | if (outsize > 0) |
| 7029 | break; |
| 7030 | err = GetLastError(); |
| 7031 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7032 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7033 | { |
| 7034 | PyErr_SetFromWindowsErr(0); |
| 7035 | goto error; |
| 7036 | } |
| 7037 | insize++; |
| 7038 | } |
| 7039 | /* 4=maximum length of a UTF-8 sequence */ |
| 7040 | while (insize <= 4 && (in + insize) <= endin); |
| 7041 | |
| 7042 | if (outsize <= 0) { |
| 7043 | Py_ssize_t startinpos, endinpos, outpos; |
| 7044 | |
| 7045 | startinpos = in - startin; |
| 7046 | endinpos = startinpos + 1; |
| 7047 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 7048 | if (unicode_decode_call_errorhandler( |
| 7049 | errors, &errorHandler, |
| 7050 | encoding, reason, |
| 7051 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7052 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7053 | { |
| 7054 | goto error; |
| 7055 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7056 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7057 | } |
| 7058 | else { |
| 7059 | in += insize; |
| 7060 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7061 | out += outsize; |
| 7062 | } |
| 7063 | } |
| 7064 | |
| 7065 | /* write a NUL character at the end */ |
| 7066 | *out = 0; |
| 7067 | |
| 7068 | /* Extend unicode object */ |
| 7069 | outsize = out - startout; |
| 7070 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7071 | if (PyUnicode_Resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7072 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7073 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7074 | |
| 7075 | error: |
| 7076 | Py_XDECREF(encoding_obj); |
| 7077 | Py_XDECREF(errorHandler); |
| 7078 | Py_XDECREF(exc); |
| 7079 | return ret; |
| 7080 | } |
| 7081 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7082 | static PyObject * |
| 7083 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7084 | const char *s, Py_ssize_t size, |
| 7085 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7086 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7087 | PyObject *v = NULL; |
| 7088 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7089 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7090 | if (code_page < 0) { |
| 7091 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7092 | return NULL; |
| 7093 | } |
| 7094 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7095 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7096 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7097 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7098 | do |
| 7099 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7100 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7101 | if (size > INT_MAX) { |
| 7102 | chunk_size = INT_MAX; |
| 7103 | final = 0; |
| 7104 | done = 0; |
| 7105 | } |
| 7106 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7107 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7108 | { |
| 7109 | chunk_size = (int)size; |
| 7110 | final = (consumed == NULL); |
| 7111 | done = 1; |
| 7112 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7113 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7114 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7115 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7116 | --chunk_size; |
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 | if (chunk_size == 0 && done) { |
| 7119 | if (v != NULL) |
| 7120 | break; |
| 7121 | Py_INCREF(unicode_empty); |
| 7122 | return unicode_empty; |
| 7123 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7124 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7125 | |
| 7126 | converted = decode_code_page_strict(code_page, &v, |
| 7127 | s, chunk_size); |
| 7128 | if (converted == -2) |
| 7129 | converted = decode_code_page_errors(code_page, &v, |
| 7130 | s, chunk_size, |
| 7131 | errors); |
| 7132 | assert(converted != 0); |
| 7133 | |
| 7134 | if (converted < 0) { |
| 7135 | Py_XDECREF(v); |
| 7136 | return NULL; |
| 7137 | } |
| 7138 | |
| 7139 | if (consumed) |
| 7140 | *consumed += converted; |
| 7141 | |
| 7142 | s += converted; |
| 7143 | size -= converted; |
| 7144 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7145 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7146 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7149 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7150 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7151 | const char *s, |
| 7152 | Py_ssize_t size, |
| 7153 | const char *errors, |
| 7154 | Py_ssize_t *consumed) |
| 7155 | { |
| 7156 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7157 | } |
| 7158 | |
| 7159 | PyObject * |
| 7160 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7161 | Py_ssize_t size, |
| 7162 | const char *errors, |
| 7163 | Py_ssize_t *consumed) |
| 7164 | { |
| 7165 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7166 | } |
| 7167 | |
| 7168 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7169 | PyUnicode_DecodeMBCS(const char *s, |
| 7170 | Py_ssize_t size, |
| 7171 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7172 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7173 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7174 | } |
| 7175 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | static DWORD |
| 7177 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7178 | { |
| 7179 | if (code_page == CP_UTF8) { |
| 7180 | if (winver.dwMajorVersion >= 6) |
| 7181 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7182 | and later */ |
| 7183 | return WC_ERR_INVALID_CHARS; |
| 7184 | else |
| 7185 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7186 | return 0; |
| 7187 | } |
| 7188 | else if (code_page == CP_UTF7) { |
| 7189 | /* CP_UTF7 only supports flags=0 */ |
| 7190 | return 0; |
| 7191 | } |
| 7192 | else { |
| 7193 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7194 | return 0; |
| 7195 | else |
| 7196 | return WC_NO_BEST_FIT_CHARS; |
| 7197 | } |
| 7198 | } |
| 7199 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7200 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7201 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7202 | * mode. |
| 7203 | * |
| 7204 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7205 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7206 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7207 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7208 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7209 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7210 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7211 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7212 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7213 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7214 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7215 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7216 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7217 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7218 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7219 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7220 | /* Create a substring so that we can get the UTF-16 representation |
| 7221 | of just the slice under consideration. */ |
| 7222 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7223 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7224 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7225 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7226 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7227 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7229 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7230 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7231 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7232 | if (substring == NULL) |
| 7233 | return -1; |
| 7234 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7235 | if (p == NULL) { |
| 7236 | Py_DECREF(substring); |
| 7237 | return -1; |
| 7238 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7239 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7240 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7241 | outsize = WideCharToMultiByte(code_page, flags, |
| 7242 | p, size, |
| 7243 | NULL, 0, |
| 7244 | NULL, pusedDefaultChar); |
| 7245 | if (outsize <= 0) |
| 7246 | goto error; |
| 7247 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7248 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7249 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7251 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7252 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7256 | if (*outbytes == NULL) { |
| 7257 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7258 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7259 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7260 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7261 | } |
| 7262 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7263 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7264 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7265 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7266 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7267 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7268 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7269 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7270 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7271 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7272 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7273 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7274 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | outsize = WideCharToMultiByte(code_page, flags, |
| 7279 | p, size, |
| 7280 | out, outsize, |
| 7281 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7282 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7283 | if (outsize <= 0) |
| 7284 | goto error; |
| 7285 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7286 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7287 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7288 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7289 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7290 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7292 | return -2; |
| 7293 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7294 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7297 | /* |
| 7298 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7299 | * error handler. |
| 7300 | * |
| 7301 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7302 | * -1 on other error. |
| 7303 | */ |
| 7304 | static int |
| 7305 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7306 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7307 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7308 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7309 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7310 | Py_ssize_t pos = unicode_offset; |
| 7311 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7312 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7313 | 2000 English version of the message. */ |
| 7314 | const char *reason = "invalid character"; |
| 7315 | /* 4=maximum length of a UTF-8 sequence */ |
| 7316 | char buffer[4]; |
| 7317 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7318 | Py_ssize_t outsize; |
| 7319 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7320 | PyObject *errorHandler = NULL; |
| 7321 | PyObject *exc = NULL; |
| 7322 | PyObject *encoding_obj = NULL; |
| 7323 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7324 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | PyObject *rep; |
| 7326 | int ret = -1; |
| 7327 | |
| 7328 | assert(insize > 0); |
| 7329 | |
| 7330 | encoding = code_page_name(code_page, &encoding_obj); |
| 7331 | if (encoding == NULL) |
| 7332 | return -1; |
| 7333 | |
| 7334 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7335 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7336 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7337 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7338 | if (exc != NULL) { |
| 7339 | PyCodec_StrictErrors(exc); |
| 7340 | Py_DECREF(exc); |
| 7341 | } |
| 7342 | Py_XDECREF(encoding_obj); |
| 7343 | return -1; |
| 7344 | } |
| 7345 | |
| 7346 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7347 | pusedDefaultChar = &usedDefaultChar; |
| 7348 | else |
| 7349 | pusedDefaultChar = NULL; |
| 7350 | |
| 7351 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7352 | PyErr_NoMemory(); |
| 7353 | goto error; |
| 7354 | } |
| 7355 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7356 | |
| 7357 | if (*outbytes == NULL) { |
| 7358 | /* Create string object */ |
| 7359 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7360 | if (*outbytes == NULL) |
| 7361 | goto error; |
| 7362 | out = PyBytes_AS_STRING(*outbytes); |
| 7363 | } |
| 7364 | else { |
| 7365 | /* Extend string object */ |
| 7366 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7367 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7368 | PyErr_NoMemory(); |
| 7369 | goto error; |
| 7370 | } |
| 7371 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7372 | goto error; |
| 7373 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7374 | } |
| 7375 | |
| 7376 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7377 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7379 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7380 | wchar_t chars[2]; |
| 7381 | int charsize; |
| 7382 | if (ch < 0x10000) { |
| 7383 | chars[0] = (wchar_t)ch; |
| 7384 | charsize = 1; |
| 7385 | } |
| 7386 | else { |
| 7387 | ch -= 0x10000; |
| 7388 | chars[0] = 0xd800 + (ch >> 10); |
| 7389 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7390 | charsize = 2; |
| 7391 | } |
| 7392 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7393 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7394 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7395 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7396 | NULL, pusedDefaultChar); |
| 7397 | if (outsize > 0) { |
| 7398 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7399 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7400 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7401 | memcpy(out, buffer, outsize); |
| 7402 | out += outsize; |
| 7403 | continue; |
| 7404 | } |
| 7405 | } |
| 7406 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7407 | PyErr_SetFromWindowsErr(0); |
| 7408 | goto error; |
| 7409 | } |
| 7410 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7411 | rep = unicode_encode_call_errorhandler( |
| 7412 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7413 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7414 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7415 | if (rep == NULL) |
| 7416 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7417 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7418 | |
| 7419 | if (PyBytes_Check(rep)) { |
| 7420 | outsize = PyBytes_GET_SIZE(rep); |
| 7421 | if (outsize != 1) { |
| 7422 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7423 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7424 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7425 | Py_DECREF(rep); |
| 7426 | goto error; |
| 7427 | } |
| 7428 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7429 | } |
| 7430 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7431 | out += outsize; |
| 7432 | } |
| 7433 | else { |
| 7434 | Py_ssize_t i; |
| 7435 | enum PyUnicode_Kind kind; |
| 7436 | void *data; |
| 7437 | |
| 7438 | if (PyUnicode_READY(rep) < 0) { |
| 7439 | Py_DECREF(rep); |
| 7440 | goto error; |
| 7441 | } |
| 7442 | |
| 7443 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7444 | if (outsize != 1) { |
| 7445 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7446 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7447 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7448 | Py_DECREF(rep); |
| 7449 | goto error; |
| 7450 | } |
| 7451 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7452 | } |
| 7453 | kind = PyUnicode_KIND(rep); |
| 7454 | data = PyUnicode_DATA(rep); |
| 7455 | for (i=0; i < outsize; i++) { |
| 7456 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7457 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7458 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7459 | encoding, unicode, |
| 7460 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7461 | "unable to encode error handler result to ASCII"); |
| 7462 | Py_DECREF(rep); |
| 7463 | goto error; |
| 7464 | } |
| 7465 | *out = (unsigned char)ch; |
| 7466 | out++; |
| 7467 | } |
| 7468 | } |
| 7469 | Py_DECREF(rep); |
| 7470 | } |
| 7471 | /* write a NUL byte */ |
| 7472 | *out = 0; |
| 7473 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7474 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7475 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7476 | goto error; |
| 7477 | ret = 0; |
| 7478 | |
| 7479 | error: |
| 7480 | Py_XDECREF(encoding_obj); |
| 7481 | Py_XDECREF(errorHandler); |
| 7482 | Py_XDECREF(exc); |
| 7483 | return ret; |
| 7484 | } |
| 7485 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7486 | static PyObject * |
| 7487 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7488 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7489 | const char *errors) |
| 7490 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7491 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7492 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7493 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7494 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7495 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7496 | if (PyUnicode_READY(unicode) < 0) |
| 7497 | return NULL; |
| 7498 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7499 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7500 | if (code_page < 0) { |
| 7501 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7502 | return NULL; |
| 7503 | } |
| 7504 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7505 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7506 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7507 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7508 | offset = 0; |
| 7509 | do |
| 7510 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7511 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7512 | /* 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] | 7513 | chunks. */ |
| 7514 | if (len > INT_MAX/2) { |
| 7515 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7516 | done = 0; |
| 7517 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7518 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7519 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7520 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7521 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7522 | done = 1; |
| 7523 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7524 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7525 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7526 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7527 | errors); |
| 7528 | if (ret == -2) |
| 7529 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7530 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7531 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7532 | if (ret < 0) { |
| 7533 | Py_XDECREF(outbytes); |
| 7534 | return NULL; |
| 7535 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7536 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7537 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7538 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7539 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7540 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7541 | return outbytes; |
| 7542 | } |
| 7543 | |
| 7544 | PyObject * |
| 7545 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7546 | Py_ssize_t size, |
| 7547 | const char *errors) |
| 7548 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7549 | PyObject *unicode, *res; |
| 7550 | unicode = PyUnicode_FromUnicode(p, size); |
| 7551 | if (unicode == NULL) |
| 7552 | return NULL; |
| 7553 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7554 | Py_DECREF(unicode); |
| 7555 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7556 | } |
| 7557 | |
| 7558 | PyObject * |
| 7559 | PyUnicode_EncodeCodePage(int code_page, |
| 7560 | PyObject *unicode, |
| 7561 | const char *errors) |
| 7562 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7563 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7564 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7566 | PyObject * |
| 7567 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7568 | { |
| 7569 | if (!PyUnicode_Check(unicode)) { |
| 7570 | PyErr_BadArgument(); |
| 7571 | return NULL; |
| 7572 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7573 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7574 | } |
| 7575 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7576 | #undef NEED_RETRY |
| 7577 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7578 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7581 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7582 | PyObject * |
| 7583 | PyUnicode_DecodeCharmap(const char *s, |
| 7584 | Py_ssize_t size, |
| 7585 | PyObject *mapping, |
| 7586 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7588 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7589 | Py_ssize_t startinpos; |
| 7590 | Py_ssize_t endinpos; |
| 7591 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7592 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7593 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7594 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7595 | PyObject *errorHandler = NULL; |
| 7596 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | /* Default to Latin-1 */ |
| 7599 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7600 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7601 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7602 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7604 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7605 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7606 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7607 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7608 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7609 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7610 | Py_ssize_t maplen; |
| 7611 | enum PyUnicode_Kind kind; |
| 7612 | void *data; |
| 7613 | Py_UCS4 x; |
| 7614 | |
| 7615 | if (PyUnicode_READY(mapping) < 0) |
| 7616 | return NULL; |
| 7617 | |
| 7618 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7619 | data = PyUnicode_DATA(mapping); |
| 7620 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7621 | while (s < e) { |
| 7622 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7623 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7624 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7625 | x = PyUnicode_READ(kind, data, ch); |
| 7626 | else |
| 7627 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7629 | if (x == 0xfffe) |
| 7630 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7631 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7632 | startinpos = s-starts; |
| 7633 | endinpos = startinpos+1; |
| 7634 | if (unicode_decode_call_errorhandler( |
| 7635 | errors, &errorHandler, |
| 7636 | "charmap", "character maps to <undefined>", |
| 7637 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7638 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | goto onError; |
| 7640 | } |
| 7641 | continue; |
| 7642 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7643 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7644 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7645 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7647 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7648 | } |
| 7649 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | while (s < e) { |
| 7651 | unsigned char ch = *s; |
| 7652 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7653 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7654 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7655 | w = PyLong_FromLong((long)ch); |
| 7656 | if (w == NULL) |
| 7657 | goto onError; |
| 7658 | x = PyObject_GetItem(mapping, w); |
| 7659 | Py_DECREF(w); |
| 7660 | if (x == NULL) { |
| 7661 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7662 | /* No mapping found means: mapping is undefined. */ |
| 7663 | PyErr_Clear(); |
| 7664 | x = Py_None; |
| 7665 | Py_INCREF(x); |
| 7666 | } else |
| 7667 | goto onError; |
| 7668 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7670 | /* Apply mapping */ |
| 7671 | if (PyLong_Check(x)) { |
| 7672 | long value = PyLong_AS_LONG(x); |
| 7673 | if (value < 0 || value > 65535) { |
| 7674 | PyErr_SetString(PyExc_TypeError, |
| 7675 | "character mapping must be in range(65536)"); |
| 7676 | Py_DECREF(x); |
| 7677 | goto onError; |
| 7678 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7679 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7680 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7681 | } |
| 7682 | else if (x == Py_None) { |
| 7683 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7684 | startinpos = s-starts; |
| 7685 | endinpos = startinpos+1; |
| 7686 | if (unicode_decode_call_errorhandler( |
| 7687 | errors, &errorHandler, |
| 7688 | "charmap", "character maps to <undefined>", |
| 7689 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7690 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7691 | Py_DECREF(x); |
| 7692 | goto onError; |
| 7693 | } |
| 7694 | Py_DECREF(x); |
| 7695 | continue; |
| 7696 | } |
| 7697 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7698 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7699 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7700 | if (PyUnicode_READY(x) < 0) |
| 7701 | goto onError; |
| 7702 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7703 | |
| 7704 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7705 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7706 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7707 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7708 | goto onError; |
| 7709 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7710 | else if (targetsize > 1) { |
| 7711 | /* 1-n mapping */ |
| 7712 | if (targetsize > extrachars) { |
| 7713 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7715 | (targetsize << 2); |
| 7716 | extrachars += needed; |
| 7717 | /* XXX overflow detection missing */ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7718 | if (PyUnicode_Resize(&v, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7719 | PyUnicode_GET_LENGTH(v) + needed) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7720 | Py_DECREF(x); |
| 7721 | goto onError; |
| 7722 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7723 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7724 | if (unicode_widen(&v, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
| 7725 | goto onError; |
| 7726 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7727 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7728 | extrachars -= targetsize; |
| 7729 | } |
| 7730 | /* 1-0 mapping: skip the character */ |
| 7731 | } |
| 7732 | else { |
| 7733 | /* wrong return value */ |
| 7734 | PyErr_SetString(PyExc_TypeError, |
| 7735 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7736 | Py_DECREF(x); |
| 7737 | goto onError; |
| 7738 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | Py_DECREF(x); |
| 7740 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7741 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7743 | if (PyUnicode_Resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7744 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7745 | Py_XDECREF(errorHandler); |
| 7746 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7747 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | Py_XDECREF(errorHandler); |
| 7751 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | Py_XDECREF(v); |
| 7753 | return NULL; |
| 7754 | } |
| 7755 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7756 | /* Charmap encoding: the lookup table */ |
| 7757 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7758 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | PyObject_HEAD |
| 7760 | unsigned char level1[32]; |
| 7761 | int count2, count3; |
| 7762 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7763 | }; |
| 7764 | |
| 7765 | static PyObject* |
| 7766 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7767 | { |
| 7768 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7769 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7771 | } |
| 7772 | |
| 7773 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7774 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7776 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7777 | }; |
| 7778 | |
| 7779 | static void |
| 7780 | encoding_map_dealloc(PyObject* o) |
| 7781 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7782 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7786 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | "EncodingMap", /*tp_name*/ |
| 7788 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7789 | 0, /*tp_itemsize*/ |
| 7790 | /* methods */ |
| 7791 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7792 | 0, /*tp_print*/ |
| 7793 | 0, /*tp_getattr*/ |
| 7794 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7795 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7796 | 0, /*tp_repr*/ |
| 7797 | 0, /*tp_as_number*/ |
| 7798 | 0, /*tp_as_sequence*/ |
| 7799 | 0, /*tp_as_mapping*/ |
| 7800 | 0, /*tp_hash*/ |
| 7801 | 0, /*tp_call*/ |
| 7802 | 0, /*tp_str*/ |
| 7803 | 0, /*tp_getattro*/ |
| 7804 | 0, /*tp_setattro*/ |
| 7805 | 0, /*tp_as_buffer*/ |
| 7806 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7807 | 0, /*tp_doc*/ |
| 7808 | 0, /*tp_traverse*/ |
| 7809 | 0, /*tp_clear*/ |
| 7810 | 0, /*tp_richcompare*/ |
| 7811 | 0, /*tp_weaklistoffset*/ |
| 7812 | 0, /*tp_iter*/ |
| 7813 | 0, /*tp_iternext*/ |
| 7814 | encoding_map_methods, /*tp_methods*/ |
| 7815 | 0, /*tp_members*/ |
| 7816 | 0, /*tp_getset*/ |
| 7817 | 0, /*tp_base*/ |
| 7818 | 0, /*tp_dict*/ |
| 7819 | 0, /*tp_descr_get*/ |
| 7820 | 0, /*tp_descr_set*/ |
| 7821 | 0, /*tp_dictoffset*/ |
| 7822 | 0, /*tp_init*/ |
| 7823 | 0, /*tp_alloc*/ |
| 7824 | 0, /*tp_new*/ |
| 7825 | 0, /*tp_free*/ |
| 7826 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7827 | }; |
| 7828 | |
| 7829 | PyObject* |
| 7830 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7831 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7832 | PyObject *result; |
| 7833 | struct encoding_map *mresult; |
| 7834 | int i; |
| 7835 | int need_dict = 0; |
| 7836 | unsigned char level1[32]; |
| 7837 | unsigned char level2[512]; |
| 7838 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7839 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7840 | int kind; |
| 7841 | void *data; |
| 7842 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7843 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7845 | PyErr_BadArgument(); |
| 7846 | return NULL; |
| 7847 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | kind = PyUnicode_KIND(string); |
| 7849 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7850 | memset(level1, 0xFF, sizeof level1); |
| 7851 | memset(level2, 0xFF, sizeof level2); |
| 7852 | |
| 7853 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7854 | or if there are non-BMP characters, we need to use |
| 7855 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7856 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7857 | need_dict = 1; |
| 7858 | for (i = 1; i < 256; i++) { |
| 7859 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | ch = PyUnicode_READ(kind, data, i); |
| 7861 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7862 | need_dict = 1; |
| 7863 | break; |
| 7864 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7865 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7866 | /* unmapped character */ |
| 7867 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7868 | l1 = ch >> 11; |
| 7869 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7870 | if (level1[l1] == 0xFF) |
| 7871 | level1[l1] = count2++; |
| 7872 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7873 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7877 | need_dict = 1; |
| 7878 | |
| 7879 | if (need_dict) { |
| 7880 | PyObject *result = PyDict_New(); |
| 7881 | PyObject *key, *value; |
| 7882 | if (!result) |
| 7883 | return NULL; |
| 7884 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7885 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7886 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7887 | if (!key || !value) |
| 7888 | goto failed1; |
| 7889 | if (PyDict_SetItem(result, key, value) == -1) |
| 7890 | goto failed1; |
| 7891 | Py_DECREF(key); |
| 7892 | Py_DECREF(value); |
| 7893 | } |
| 7894 | return result; |
| 7895 | failed1: |
| 7896 | Py_XDECREF(key); |
| 7897 | Py_XDECREF(value); |
| 7898 | Py_DECREF(result); |
| 7899 | return NULL; |
| 7900 | } |
| 7901 | |
| 7902 | /* Create a three-level trie */ |
| 7903 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7904 | 16*count2 + 128*count3 - 1); |
| 7905 | if (!result) |
| 7906 | return PyErr_NoMemory(); |
| 7907 | PyObject_Init(result, &EncodingMapType); |
| 7908 | mresult = (struct encoding_map*)result; |
| 7909 | mresult->count2 = count2; |
| 7910 | mresult->count3 = count3; |
| 7911 | mlevel1 = mresult->level1; |
| 7912 | mlevel2 = mresult->level23; |
| 7913 | mlevel3 = mresult->level23 + 16*count2; |
| 7914 | memcpy(mlevel1, level1, 32); |
| 7915 | memset(mlevel2, 0xFF, 16*count2); |
| 7916 | memset(mlevel3, 0, 128*count3); |
| 7917 | count3 = 0; |
| 7918 | for (i = 1; i < 256; i++) { |
| 7919 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7920 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7921 | /* unmapped character */ |
| 7922 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7923 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7924 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | i2 = 16*mlevel1[o1] + o2; |
| 7926 | if (mlevel2[i2] == 0xFF) |
| 7927 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7928 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7929 | i3 = 128*mlevel2[i2] + o3; |
| 7930 | mlevel3[i3] = i; |
| 7931 | } |
| 7932 | return result; |
| 7933 | } |
| 7934 | |
| 7935 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7936 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7937 | { |
| 7938 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7939 | int l1 = c>>11; |
| 7940 | int l2 = (c>>7) & 0xF; |
| 7941 | int l3 = c & 0x7F; |
| 7942 | int i; |
| 7943 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7944 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7945 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7946 | if (c == 0) |
| 7947 | return 0; |
| 7948 | /* level 1*/ |
| 7949 | i = map->level1[l1]; |
| 7950 | if (i == 0xFF) { |
| 7951 | return -1; |
| 7952 | } |
| 7953 | /* level 2*/ |
| 7954 | i = map->level23[16*i+l2]; |
| 7955 | if (i == 0xFF) { |
| 7956 | return -1; |
| 7957 | } |
| 7958 | /* level 3 */ |
| 7959 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7960 | if (i == 0) { |
| 7961 | return -1; |
| 7962 | } |
| 7963 | return i; |
| 7964 | } |
| 7965 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7966 | /* Lookup the character ch in the mapping. If the character |
| 7967 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7968 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7969 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7970 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7971 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7972 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7973 | PyObject *x; |
| 7974 | |
| 7975 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7977 | x = PyObject_GetItem(mapping, w); |
| 7978 | Py_DECREF(w); |
| 7979 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7981 | /* No mapping found means: mapping is undefined. */ |
| 7982 | PyErr_Clear(); |
| 7983 | x = Py_None; |
| 7984 | Py_INCREF(x); |
| 7985 | return x; |
| 7986 | } else |
| 7987 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7988 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7989 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7990 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7991 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7992 | long value = PyLong_AS_LONG(x); |
| 7993 | if (value < 0 || value > 255) { |
| 7994 | PyErr_SetString(PyExc_TypeError, |
| 7995 | "character mapping must be in range(256)"); |
| 7996 | Py_DECREF(x); |
| 7997 | return NULL; |
| 7998 | } |
| 7999 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8001 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8002 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8003 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8004 | /* wrong return value */ |
| 8005 | PyErr_Format(PyExc_TypeError, |
| 8006 | "character mapping must return integer, bytes or None, not %.400s", |
| 8007 | x->ob_type->tp_name); |
| 8008 | Py_DECREF(x); |
| 8009 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | } |
| 8011 | } |
| 8012 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8013 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8014 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8015 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8016 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8017 | /* exponentially overallocate to minimize reallocations */ |
| 8018 | if (requiredsize < 2*outsize) |
| 8019 | requiredsize = 2*outsize; |
| 8020 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8021 | return -1; |
| 8022 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8023 | } |
| 8024 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8025 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8027 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8028 | /* 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] | 8029 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8030 | space is available. Return a new reference to the object that |
| 8031 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8032 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8033 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8034 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8035 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8036 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8037 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | PyObject *rep; |
| 8039 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8040 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8041 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8042 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8043 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8045 | if (res == -1) |
| 8046 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | if (outsize<requiredsize) |
| 8048 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8049 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8050 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | outstart[(*outpos)++] = (char)res; |
| 8052 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | } |
| 8054 | |
| 8055 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8056 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8057 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8058 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | Py_DECREF(rep); |
| 8060 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8061 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | if (PyLong_Check(rep)) { |
| 8063 | Py_ssize_t requiredsize = *outpos+1; |
| 8064 | if (outsize<requiredsize) |
| 8065 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8066 | Py_DECREF(rep); |
| 8067 | return enc_EXCEPTION; |
| 8068 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8069 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8070 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8071 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | else { |
| 8073 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8074 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8075 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8076 | if (outsize<requiredsize) |
| 8077 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8078 | Py_DECREF(rep); |
| 8079 | return enc_EXCEPTION; |
| 8080 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8081 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8082 | memcpy(outstart + *outpos, repchars, repsize); |
| 8083 | *outpos += repsize; |
| 8084 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8085 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8086 | Py_DECREF(rep); |
| 8087 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | /* handle an error in PyUnicode_EncodeCharmap |
| 8091 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8092 | static int |
| 8093 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8094 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8095 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8096 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8097 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8098 | { |
| 8099 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8100 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8101 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8102 | enum PyUnicode_Kind kind; |
| 8103 | void *data; |
| 8104 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8105 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8106 | Py_ssize_t collstartpos = *inpos; |
| 8107 | Py_ssize_t collendpos = *inpos+1; |
| 8108 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8109 | char *encoding = "charmap"; |
| 8110 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8111 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8112 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8113 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8114 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8115 | if (PyUnicode_READY(unicode) < 0) |
| 8116 | return -1; |
| 8117 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | /* find all unencodable characters */ |
| 8119 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8120 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8121 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8122 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8123 | val = encoding_map_lookup(ch, mapping); |
| 8124 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8125 | break; |
| 8126 | ++collendpos; |
| 8127 | continue; |
| 8128 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8129 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8130 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8131 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | if (rep==NULL) |
| 8133 | return -1; |
| 8134 | else if (rep!=Py_None) { |
| 8135 | Py_DECREF(rep); |
| 8136 | break; |
| 8137 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8138 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | } |
| 8141 | /* cache callback name lookup |
| 8142 | * (if not done yet, i.e. it's the first error) */ |
| 8143 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8145 | *known_errorHandler = 1; |
| 8146 | else if (!strcmp(errors, "replace")) |
| 8147 | *known_errorHandler = 2; |
| 8148 | else if (!strcmp(errors, "ignore")) |
| 8149 | *known_errorHandler = 3; |
| 8150 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8151 | *known_errorHandler = 4; |
| 8152 | else |
| 8153 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8154 | } |
| 8155 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8156 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8157 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8158 | return -1; |
| 8159 | case 2: /* replace */ |
| 8160 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8161 | x = charmapencode_output('?', mapping, res, respos); |
| 8162 | if (x==enc_EXCEPTION) { |
| 8163 | return -1; |
| 8164 | } |
| 8165 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8166 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | return -1; |
| 8168 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8169 | } |
| 8170 | /* fall through */ |
| 8171 | case 3: /* ignore */ |
| 8172 | *inpos = collendpos; |
| 8173 | break; |
| 8174 | case 4: /* xmlcharrefreplace */ |
| 8175 | /* generate replacement (temporarily (mis)uses p) */ |
| 8176 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8177 | char buffer[2+29+1+1]; |
| 8178 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8179 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | for (cp = buffer; *cp; ++cp) { |
| 8181 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8182 | if (x==enc_EXCEPTION) |
| 8183 | return -1; |
| 8184 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8185 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8186 | return -1; |
| 8187 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8188 | } |
| 8189 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8190 | *inpos = collendpos; |
| 8191 | break; |
| 8192 | default: |
| 8193 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8194 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8195 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8196 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8197 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8198 | if (PyBytes_Check(repunicode)) { |
| 8199 | /* Directly copy bytes result to output. */ |
| 8200 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8201 | Py_ssize_t requiredsize; |
| 8202 | repsize = PyBytes_Size(repunicode); |
| 8203 | requiredsize = *respos + repsize; |
| 8204 | if (requiredsize > outsize) |
| 8205 | /* Make room for all additional bytes. */ |
| 8206 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8207 | Py_DECREF(repunicode); |
| 8208 | return -1; |
| 8209 | } |
| 8210 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8211 | PyBytes_AsString(repunicode), repsize); |
| 8212 | *respos += repsize; |
| 8213 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8214 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8215 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8216 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8217 | /* generate replacement */ |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8218 | if (PyUnicode_READY(repunicode) < 0) { |
| 8219 | Py_DECREF(repunicode); |
| 8220 | return -1; |
| 8221 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8222 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8223 | data = PyUnicode_DATA(repunicode); |
| 8224 | kind = PyUnicode_KIND(repunicode); |
| 8225 | for (index = 0; index < repsize; index++) { |
| 8226 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8227 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8228 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8229 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | return -1; |
| 8231 | } |
| 8232 | else if (x==enc_FAILED) { |
| 8233 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8234 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | return -1; |
| 8236 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8237 | } |
| 8238 | *inpos = newpos; |
| 8239 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8240 | } |
| 8241 | return 0; |
| 8242 | } |
| 8243 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8244 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8245 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8246 | PyObject *mapping, |
| 8247 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8248 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | /* output object */ |
| 8250 | PyObject *res = NULL; |
| 8251 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8252 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8253 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8254 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8255 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8256 | PyObject *errorHandler = NULL; |
| 8257 | PyObject *exc = NULL; |
| 8258 | /* the following variable is used for caching string comparisons |
| 8259 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8260 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8261 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8262 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8263 | if (PyUnicode_READY(unicode) < 0) |
| 8264 | return NULL; |
| 8265 | size = PyUnicode_GET_LENGTH(unicode); |
| 8266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | /* Default to Latin-1 */ |
| 8268 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8269 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8270 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | /* allocate enough for a simple encoding without |
| 8272 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8273 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8274 | if (res == NULL) |
| 8275 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8276 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8279 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8280 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8281 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8282 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | if (x==enc_EXCEPTION) /* error */ |
| 8284 | goto onError; |
| 8285 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8286 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | &exc, |
| 8288 | &known_errorHandler, &errorHandler, errors, |
| 8289 | &res, &respos)) { |
| 8290 | goto onError; |
| 8291 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8292 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | else |
| 8294 | /* done with this character => adjust input position */ |
| 8295 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8297 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8298 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8299 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8300 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8301 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8302 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8303 | Py_XDECREF(exc); |
| 8304 | Py_XDECREF(errorHandler); |
| 8305 | return res; |
| 8306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8308 | Py_XDECREF(res); |
| 8309 | Py_XDECREF(exc); |
| 8310 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | return NULL; |
| 8312 | } |
| 8313 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8314 | /* Deprecated */ |
| 8315 | PyObject * |
| 8316 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8317 | Py_ssize_t size, |
| 8318 | PyObject *mapping, |
| 8319 | const char *errors) |
| 8320 | { |
| 8321 | PyObject *result; |
| 8322 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8323 | if (unicode == NULL) |
| 8324 | return NULL; |
| 8325 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8326 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8327 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8328 | } |
| 8329 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8330 | PyObject * |
| 8331 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8332 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 | { |
| 8334 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | PyErr_BadArgument(); |
| 8336 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8338 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8341 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8342 | static void |
| 8343 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8345 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8346 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8347 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8349 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8350 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | } |
| 8352 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8353 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8354 | goto onError; |
| 8355 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8356 | goto onError; |
| 8357 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8358 | goto onError; |
| 8359 | return; |
| 8360 | onError: |
| 8361 | Py_DECREF(*exceptionObject); |
| 8362 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8363 | } |
| 8364 | } |
| 8365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8366 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8367 | static void |
| 8368 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8369 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8370 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8371 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8372 | { |
| 8373 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8374 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8375 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8376 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8377 | } |
| 8378 | |
| 8379 | /* error handling callback helper: |
| 8380 | build arguments, call the callback and check the arguments, |
| 8381 | put the result into newpos and return the replacement string, which |
| 8382 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8383 | static PyObject * |
| 8384 | unicode_translate_call_errorhandler(const char *errors, |
| 8385 | PyObject **errorHandler, |
| 8386 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8388 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8389 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8391 | 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] | 8392 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8393 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | PyObject *restuple; |
| 8395 | PyObject *resunicode; |
| 8396 | |
| 8397 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8398 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8399 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8401 | } |
| 8402 | |
| 8403 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8407 | |
| 8408 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8409 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8410 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8411 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8412 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8413 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | Py_DECREF(restuple); |
| 8415 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | } |
| 8417 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8418 | &resunicode, &i_newpos)) { |
| 8419 | Py_DECREF(restuple); |
| 8420 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8421 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8422 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8424 | else |
| 8425 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8426 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8427 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8428 | Py_DECREF(restuple); |
| 8429 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8430 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | Py_INCREF(resunicode); |
| 8432 | Py_DECREF(restuple); |
| 8433 | return resunicode; |
| 8434 | } |
| 8435 | |
| 8436 | /* Lookup the character ch in the mapping and put the result in result, |
| 8437 | which must be decrefed by the caller. |
| 8438 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8439 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8442 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8443 | PyObject *x; |
| 8444 | |
| 8445 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8447 | x = PyObject_GetItem(mapping, w); |
| 8448 | Py_DECREF(w); |
| 8449 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8451 | /* No mapping found means: use 1:1 mapping. */ |
| 8452 | PyErr_Clear(); |
| 8453 | *result = NULL; |
| 8454 | return 0; |
| 8455 | } else |
| 8456 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | } |
| 8458 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8459 | *result = x; |
| 8460 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8461 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8462 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | long value = PyLong_AS_LONG(x); |
| 8464 | long max = PyUnicode_GetMax(); |
| 8465 | if (value < 0 || value > max) { |
| 8466 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8467 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8468 | Py_DECREF(x); |
| 8469 | return -1; |
| 8470 | } |
| 8471 | *result = x; |
| 8472 | return 0; |
| 8473 | } |
| 8474 | else if (PyUnicode_Check(x)) { |
| 8475 | *result = x; |
| 8476 | return 0; |
| 8477 | } |
| 8478 | else { |
| 8479 | /* wrong return value */ |
| 8480 | PyErr_SetString(PyExc_TypeError, |
| 8481 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8482 | Py_DECREF(x); |
| 8483 | return -1; |
| 8484 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8485 | } |
| 8486 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | if not reallocate and adjust various state variables. |
| 8488 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8489 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8490 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8492 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8493 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8494 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8495 | /* exponentially overallocate to minimize reallocations */ |
| 8496 | if (requiredsize < 2 * oldsize) |
| 8497 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8498 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8499 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8501 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8502 | } |
| 8503 | return 0; |
| 8504 | } |
| 8505 | /* lookup the character, put the result in the output string and adjust |
| 8506 | various state variables. Return a new reference to the object that |
| 8507 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8508 | undefined (in which case no character was written). |
| 8509 | The called must decref result. |
| 8510 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8511 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8512 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8513 | PyObject *mapping, Py_UCS4 **output, |
| 8514 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8515 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8516 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8518 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8520 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8521 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8522 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8523 | } |
| 8524 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8526 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | /* 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] | 8528 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8529 | } |
| 8530 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | Py_ssize_t repsize; |
| 8532 | if (PyUnicode_READY(*res) == -1) |
| 8533 | return -1; |
| 8534 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | if (repsize==1) { |
| 8536 | /* 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] | 8537 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8538 | } |
| 8539 | else if (repsize!=0) { |
| 8540 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | Py_ssize_t requiredsize = *opos + |
| 8542 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8543 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8544 | Py_ssize_t i; |
| 8545 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8546 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | for(i = 0; i < repsize; i++) |
| 8548 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8550 | } |
| 8551 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8552 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8553 | return 0; |
| 8554 | } |
| 8555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8556 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8558 | PyObject *mapping, |
| 8559 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8560 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8561 | /* input object */ |
| 8562 | char *idata; |
| 8563 | Py_ssize_t size, i; |
| 8564 | int kind; |
| 8565 | /* output buffer */ |
| 8566 | Py_UCS4 *output = NULL; |
| 8567 | Py_ssize_t osize; |
| 8568 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8569 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8570 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8571 | char *reason = "character maps to <undefined>"; |
| 8572 | PyObject *errorHandler = NULL; |
| 8573 | PyObject *exc = NULL; |
| 8574 | /* the following variable is used for caching string comparisons |
| 8575 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8576 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8577 | int known_errorHandler = -1; |
| 8578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8580 | PyErr_BadArgument(); |
| 8581 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8582 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8584 | if (PyUnicode_READY(input) == -1) |
| 8585 | return NULL; |
| 8586 | idata = (char*)PyUnicode_DATA(input); |
| 8587 | kind = PyUnicode_KIND(input); |
| 8588 | size = PyUnicode_GET_LENGTH(input); |
| 8589 | i = 0; |
| 8590 | |
| 8591 | if (size == 0) { |
| 8592 | Py_INCREF(input); |
| 8593 | return input; |
| 8594 | } |
| 8595 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8596 | /* allocate enough for a simple 1:1 translation without |
| 8597 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8598 | osize = size; |
| 8599 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8600 | opos = 0; |
| 8601 | if (output == NULL) { |
| 8602 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8603 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8606 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | /* try to encode it */ |
| 8608 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | if (charmaptranslate_output(input, i, mapping, |
| 8610 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8611 | Py_XDECREF(x); |
| 8612 | goto onError; |
| 8613 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8614 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8615 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | else { /* untranslatable character */ |
| 8618 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8619 | Py_ssize_t repsize; |
| 8620 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8621 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8622 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | Py_ssize_t collstart = i; |
| 8624 | Py_ssize_t collend = i+1; |
| 8625 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8627 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8628 | while (collend < size) { |
| 8629 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8630 | goto onError; |
| 8631 | Py_XDECREF(x); |
| 8632 | if (x!=Py_None) |
| 8633 | break; |
| 8634 | ++collend; |
| 8635 | } |
| 8636 | /* cache callback name lookup |
| 8637 | * (if not done yet, i.e. it's the first error) */ |
| 8638 | if (known_errorHandler==-1) { |
| 8639 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8640 | known_errorHandler = 1; |
| 8641 | else if (!strcmp(errors, "replace")) |
| 8642 | known_errorHandler = 2; |
| 8643 | else if (!strcmp(errors, "ignore")) |
| 8644 | known_errorHandler = 3; |
| 8645 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8646 | known_errorHandler = 4; |
| 8647 | else |
| 8648 | known_errorHandler = 0; |
| 8649 | } |
| 8650 | switch (known_errorHandler) { |
| 8651 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | raise_translate_exception(&exc, input, collstart, |
| 8653 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8654 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8655 | case 2: /* replace */ |
| 8656 | /* 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] | 8657 | for (coll = collstart; coll<collend; coll++) |
| 8658 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | /* fall through */ |
| 8660 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | break; |
| 8663 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | /* generate replacement (temporarily (mis)uses i) */ |
| 8665 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8666 | char buffer[2+29+1+1]; |
| 8667 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8668 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8669 | if (charmaptranslate_makespace(&output, &osize, |
| 8670 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8671 | goto onError; |
| 8672 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8673 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8674 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8675 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8676 | break; |
| 8677 | default: |
| 8678 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8679 | reason, input, &exc, |
| 8680 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8681 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8683 | if (PyUnicode_READY(repunicode) < 0) { |
| 8684 | Py_DECREF(repunicode); |
| 8685 | goto onError; |
| 8686 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8687 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8688 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8689 | if (charmaptranslate_makespace(&output, &osize, |
| 8690 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8691 | Py_DECREF(repunicode); |
| 8692 | goto onError; |
| 8693 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8695 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8696 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8698 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8699 | } |
| 8700 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8702 | if (!res) |
| 8703 | goto onError; |
| 8704 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8705 | Py_XDECREF(exc); |
| 8706 | Py_XDECREF(errorHandler); |
| 8707 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8708 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8709 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8710 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8711 | Py_XDECREF(exc); |
| 8712 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8713 | return NULL; |
| 8714 | } |
| 8715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8716 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8717 | PyObject * |
| 8718 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8719 | Py_ssize_t size, |
| 8720 | PyObject *mapping, |
| 8721 | const char *errors) |
| 8722 | { |
| 8723 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8724 | if (!unicode) |
| 8725 | return NULL; |
| 8726 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8727 | } |
| 8728 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8729 | PyObject * |
| 8730 | PyUnicode_Translate(PyObject *str, |
| 8731 | PyObject *mapping, |
| 8732 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8733 | { |
| 8734 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | str = PyUnicode_FromObject(str); |
| 8737 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8738 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | Py_DECREF(str); |
| 8741 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8744 | Py_XDECREF(str); |
| 8745 | return NULL; |
| 8746 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8749 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8750 | { |
| 8751 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8752 | called as a callback from fixup() which does it already. */ |
| 8753 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8754 | const int kind = PyUnicode_KIND(self); |
| 8755 | void *data = PyUnicode_DATA(self); |
| 8756 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8757 | Py_ssize_t i; |
| 8758 | |
| 8759 | for (i = 0; i < len; ++i) { |
| 8760 | ch = PyUnicode_READ(kind, data, i); |
| 8761 | fixed = 0; |
| 8762 | if (ch > 127) { |
| 8763 | if (Py_UNICODE_ISSPACE(ch)) |
| 8764 | fixed = ' '; |
| 8765 | else { |
| 8766 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8767 | if (decimal >= 0) |
| 8768 | fixed = '0' + decimal; |
| 8769 | } |
| 8770 | if (fixed != 0) { |
| 8771 | if (fixed > maxchar) |
| 8772 | maxchar = fixed; |
| 8773 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8774 | } |
| 8775 | else if (ch > maxchar) |
| 8776 | maxchar = ch; |
| 8777 | } |
| 8778 | else if (ch > maxchar) |
| 8779 | maxchar = ch; |
| 8780 | } |
| 8781 | |
| 8782 | return maxchar; |
| 8783 | } |
| 8784 | |
| 8785 | PyObject * |
| 8786 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8787 | { |
| 8788 | if (!PyUnicode_Check(unicode)) { |
| 8789 | PyErr_BadInternalCall(); |
| 8790 | return NULL; |
| 8791 | } |
| 8792 | if (PyUnicode_READY(unicode) == -1) |
| 8793 | return NULL; |
| 8794 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8795 | /* If the string is already ASCII, just return the same string */ |
| 8796 | Py_INCREF(unicode); |
| 8797 | return unicode; |
| 8798 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8799 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | } |
| 8801 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8802 | PyObject * |
| 8803 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8804 | Py_ssize_t length) |
| 8805 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8806 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8807 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8808 | Py_UCS4 maxchar; |
| 8809 | enum PyUnicode_Kind kind; |
| 8810 | void *data; |
| 8811 | |
| 8812 | maxchar = 0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8813 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8814 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8815 | if (ch > 127) { |
| 8816 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8817 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8818 | ch = '0' + decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8819 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8820 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8821 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8822 | |
| 8823 | /* Copy to a new string */ |
| 8824 | decimal = PyUnicode_New(length, maxchar); |
| 8825 | if (decimal == NULL) |
| 8826 | return decimal; |
| 8827 | kind = PyUnicode_KIND(decimal); |
| 8828 | data = PyUnicode_DATA(decimal); |
| 8829 | /* Iterate over code points */ |
| 8830 | for (i = 0; i < length; i++) { |
| 8831 | Py_UNICODE ch = s[i]; |
| 8832 | if (ch > 127) { |
| 8833 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8834 | if (decimal >= 0) |
| 8835 | ch = '0' + decimal; |
| 8836 | } |
| 8837 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8838 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8839 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8840 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8841 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8842 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8843 | int |
| 8844 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8845 | Py_ssize_t length, |
| 8846 | char *output, |
| 8847 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8848 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8849 | PyObject *errorHandler = NULL; |
| 8850 | PyObject *exc = NULL; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8851 | PyObject *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8852 | const char *encoding = "decimal"; |
| 8853 | const char *reason = "invalid decimal Unicode string"; |
| 8854 | /* the following variable is used for caching string comparisons |
| 8855 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8856 | int known_errorHandler = -1; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8857 | Py_ssize_t i, j; |
| 8858 | enum PyUnicode_Kind kind; |
| 8859 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8860 | |
| 8861 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8862 | PyErr_BadArgument(); |
| 8863 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8864 | } |
| 8865 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8866 | unicode = PyUnicode_FromUnicode(s, length); |
| 8867 | if (unicode == NULL) |
| 8868 | return -1; |
| 8869 | |
| 8870 | if (PyUnicode_READY(unicode) < 0) |
| 8871 | goto onError; |
| 8872 | kind = PyUnicode_KIND(unicode); |
| 8873 | data = PyUnicode_DATA(unicode); |
| 8874 | |
| 8875 | for (i=0; i < length; i++) { |
| 8876 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8877 | int decimal; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8878 | Py_ssize_t startpos, endpos; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8879 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8880 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8881 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8882 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8883 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8884 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8885 | if (decimal >= 0) { |
| 8886 | *output++ = '0' + decimal; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | continue; |
| 8888 | } |
| 8889 | if (0 < ch && ch < 256) { |
| 8890 | *output++ = (char)ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8891 | continue; |
| 8892 | } |
| 8893 | /* All other characters are considered unencodable */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8894 | startpos = i; |
| 8895 | endpos = i+1; |
| 8896 | for (; endpos < length; endpos++) { |
| 8897 | ch = PyUnicode_READ(kind, data, endpos); |
| 8898 | if ((0 < ch && ch < 256) || |
| 8899 | !Py_UNICODE_ISSPACE(ch) || |
| 8900 | Py_UNICODE_TODECIMAL(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | break; |
| 8902 | } |
| 8903 | /* cache callback name lookup |
| 8904 | * (if not done yet, i.e. it's the first error) */ |
| 8905 | if (known_errorHandler==-1) { |
| 8906 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8907 | known_errorHandler = 1; |
| 8908 | else if (!strcmp(errors, "replace")) |
| 8909 | known_errorHandler = 2; |
| 8910 | else if (!strcmp(errors, "ignore")) |
| 8911 | known_errorHandler = 3; |
| 8912 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8913 | known_errorHandler = 4; |
| 8914 | else |
| 8915 | known_errorHandler = 0; |
| 8916 | } |
| 8917 | switch (known_errorHandler) { |
| 8918 | case 1: /* strict */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8919 | raise_encode_exception(&exc, encoding, unicode, startpos, endpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8920 | goto onError; |
| 8921 | case 2: /* replace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8922 | for (j=startpos; j < endpos; j++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8923 | *output++ = '?'; |
| 8924 | /* fall through */ |
| 8925 | case 3: /* ignore */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8926 | i = endpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8927 | break; |
| 8928 | case 4: /* xmlcharrefreplace */ |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8929 | /* generate replacement */ |
| 8930 | for (j=startpos; j < endpos; j++) { |
| 8931 | ch = PyUnicode_READ(kind, data, i); |
| 8932 | output += sprintf(output, "&#%d;", (int)ch); |
| 8933 | i++; |
| 8934 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | break; |
| 8936 | default: |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8937 | { |
| 8938 | PyObject *repunicode; |
| 8939 | Py_ssize_t repsize, newpos, k; |
| 8940 | enum PyUnicode_Kind repkind; |
| 8941 | void *repdata; |
| 8942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8943 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8944 | encoding, reason, unicode, &exc, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8945 | startpos, endpos, &newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | if (repunicode == NULL) |
| 8947 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8948 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8949 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8950 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8951 | Py_DECREF(repunicode); |
| 8952 | goto onError; |
| 8953 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8954 | if (PyUnicode_READY(repunicode) < 0) { |
| 8955 | Py_DECREF(repunicode); |
| 8956 | goto onError; |
| 8957 | } |
| 8958 | repkind = PyUnicode_KIND(repunicode); |
| 8959 | repdata = PyUnicode_DATA(repunicode); |
| 8960 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8961 | /* generate replacement */ |
| 8962 | repsize = PyUnicode_GET_SIZE(repunicode); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8963 | for (k=0; k<repsize; k++) { |
| 8964 | ch = PyUnicode_READ(repkind, repdata, k); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8965 | if (Py_UNICODE_ISSPACE(ch)) |
| 8966 | *output++ = ' '; |
| 8967 | else { |
| 8968 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8969 | if (decimal >= 0) |
| 8970 | *output++ = '0' + decimal; |
| 8971 | else if (0 < ch && ch < 256) |
| 8972 | *output++ = (char)ch; |
| 8973 | else { |
| 8974 | Py_DECREF(repunicode); |
| 8975 | raise_encode_exception(&exc, encoding, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8976 | unicode, startpos, endpos, |
| 8977 | reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8978 | goto onError; |
| 8979 | } |
| 8980 | } |
| 8981 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8982 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8983 | Py_DECREF(repunicode); |
| 8984 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8985 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8986 | } |
| 8987 | /* 0-terminate the output string */ |
| 8988 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8989 | Py_XDECREF(exc); |
| 8990 | Py_XDECREF(errorHandler); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8991 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8992 | return 0; |
| 8993 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8994 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8995 | Py_XDECREF(exc); |
| 8996 | Py_XDECREF(errorHandler); |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8997 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8998 | return -1; |
| 8999 | } |
| 9000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9001 | /* --- Helpers ------------------------------------------------------------ */ |
| 9002 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9004 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | Py_ssize_t start, |
| 9006 | Py_ssize_t end) |
| 9007 | { |
| 9008 | int kind1, kind2, kind; |
| 9009 | void *buf1, *buf2; |
| 9010 | Py_ssize_t len1, len2, result; |
| 9011 | |
| 9012 | kind1 = PyUnicode_KIND(s1); |
| 9013 | kind2 = PyUnicode_KIND(s2); |
| 9014 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9015 | buf1 = PyUnicode_DATA(s1); |
| 9016 | buf2 = PyUnicode_DATA(s2); |
| 9017 | if (kind1 != kind) |
| 9018 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 9019 | if (!buf1) |
| 9020 | return -2; |
| 9021 | if (kind2 != kind) |
| 9022 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9023 | if (!buf2) { |
| 9024 | if (kind1 != kind) PyMem_Free(buf1); |
| 9025 | return -2; |
| 9026 | } |
| 9027 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9028 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9029 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9030 | if (direction > 0) { |
| 9031 | switch(kind) { |
| 9032 | case PyUnicode_1BYTE_KIND: |
| 9033 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9034 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9035 | else |
| 9036 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9037 | break; |
| 9038 | case PyUnicode_2BYTE_KIND: |
| 9039 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9040 | break; |
| 9041 | case PyUnicode_4BYTE_KIND: |
| 9042 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9043 | break; |
| 9044 | default: |
| 9045 | assert(0); result = -2; |
| 9046 | } |
| 9047 | } |
| 9048 | else { |
| 9049 | switch(kind) { |
| 9050 | case PyUnicode_1BYTE_KIND: |
| 9051 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9052 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9053 | else |
| 9054 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9055 | break; |
| 9056 | case PyUnicode_2BYTE_KIND: |
| 9057 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9058 | break; |
| 9059 | case PyUnicode_4BYTE_KIND: |
| 9060 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9061 | break; |
| 9062 | default: |
| 9063 | assert(0); result = -2; |
| 9064 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9065 | } |
| 9066 | |
| 9067 | if (kind1 != kind) |
| 9068 | PyMem_Free(buf1); |
| 9069 | if (kind2 != kind) |
| 9070 | PyMem_Free(buf2); |
| 9071 | |
| 9072 | return result; |
| 9073 | } |
| 9074 | |
| 9075 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9076 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | Py_ssize_t n_buffer, |
| 9078 | void *digits, Py_ssize_t n_digits, |
| 9079 | Py_ssize_t min_width, |
| 9080 | const char *grouping, |
| 9081 | const char *thousands_sep) |
| 9082 | { |
| 9083 | switch(kind) { |
| 9084 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9085 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 9086 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 9087 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9088 | min_width, grouping, thousands_sep); |
| 9089 | else |
| 9090 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 9091 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 9092 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | case PyUnicode_2BYTE_KIND: |
| 9094 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 9095 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 9096 | min_width, grouping, thousands_sep); |
| 9097 | case PyUnicode_4BYTE_KIND: |
| 9098 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 9099 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 9100 | min_width, grouping, thousands_sep); |
| 9101 | } |
| 9102 | assert(0); |
| 9103 | return -1; |
| 9104 | } |
| 9105 | |
| 9106 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9107 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9108 | #define ADJUST_INDICES(start, end, len) \ |
| 9109 | if (end > len) \ |
| 9110 | end = len; \ |
| 9111 | else if (end < 0) { \ |
| 9112 | end += len; \ |
| 9113 | if (end < 0) \ |
| 9114 | end = 0; \ |
| 9115 | } \ |
| 9116 | if (start < 0) { \ |
| 9117 | start += len; \ |
| 9118 | if (start < 0) \ |
| 9119 | start = 0; \ |
| 9120 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9121 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9122 | Py_ssize_t |
| 9123 | PyUnicode_Count(PyObject *str, |
| 9124 | PyObject *substr, |
| 9125 | Py_ssize_t start, |
| 9126 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9128 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9129 | PyObject* str_obj; |
| 9130 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9131 | int kind1, kind2, kind; |
| 9132 | void *buf1 = NULL, *buf2 = NULL; |
| 9133 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9134 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9135 | str_obj = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9136 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9137 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9138 | sub_obj = PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9139 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9140 | Py_DECREF(str_obj); |
| 9141 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9143 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9144 | kind1 = PyUnicode_KIND(str_obj); |
| 9145 | kind2 = PyUnicode_KIND(sub_obj); |
| 9146 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9147 | buf1 = PyUnicode_DATA(str_obj); |
| 9148 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9149 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9150 | if (!buf1) |
| 9151 | goto onError; |
| 9152 | buf2 = PyUnicode_DATA(sub_obj); |
| 9153 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9154 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9155 | if (!buf2) |
| 9156 | goto onError; |
| 9157 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9158 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9159 | |
| 9160 | ADJUST_INDICES(start, end, len1); |
| 9161 | switch(kind) { |
| 9162 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9163 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9164 | result = asciilib_count( |
| 9165 | ((Py_UCS1*)buf1) + start, end - start, |
| 9166 | buf2, len2, PY_SSIZE_T_MAX |
| 9167 | ); |
| 9168 | else |
| 9169 | result = ucs1lib_count( |
| 9170 | ((Py_UCS1*)buf1) + start, end - start, |
| 9171 | buf2, len2, PY_SSIZE_T_MAX |
| 9172 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9173 | break; |
| 9174 | case PyUnicode_2BYTE_KIND: |
| 9175 | result = ucs2lib_count( |
| 9176 | ((Py_UCS2*)buf1) + start, end - start, |
| 9177 | buf2, len2, PY_SSIZE_T_MAX |
| 9178 | ); |
| 9179 | break; |
| 9180 | case PyUnicode_4BYTE_KIND: |
| 9181 | result = ucs4lib_count( |
| 9182 | ((Py_UCS4*)buf1) + start, end - start, |
| 9183 | buf2, len2, PY_SSIZE_T_MAX |
| 9184 | ); |
| 9185 | break; |
| 9186 | default: |
| 9187 | assert(0); result = 0; |
| 9188 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9189 | |
| 9190 | Py_DECREF(sub_obj); |
| 9191 | Py_DECREF(str_obj); |
| 9192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9193 | if (kind1 != kind) |
| 9194 | PyMem_Free(buf1); |
| 9195 | if (kind2 != kind) |
| 9196 | PyMem_Free(buf2); |
| 9197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9198 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9199 | onError: |
| 9200 | Py_DECREF(sub_obj); |
| 9201 | Py_DECREF(str_obj); |
| 9202 | if (kind1 != kind && buf1) |
| 9203 | PyMem_Free(buf1); |
| 9204 | if (kind2 != kind && buf2) |
| 9205 | PyMem_Free(buf2); |
| 9206 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | } |
| 9208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9209 | Py_ssize_t |
| 9210 | PyUnicode_Find(PyObject *str, |
| 9211 | PyObject *sub, |
| 9212 | Py_ssize_t start, |
| 9213 | Py_ssize_t end, |
| 9214 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9216 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9218 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9220 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9221 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9222 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9223 | Py_DECREF(str); |
| 9224 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9226 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9227 | result = any_find_slice(direction, |
| 9228 | str, sub, start, end |
| 9229 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9231 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9232 | Py_DECREF(sub); |
| 9233 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9234 | return result; |
| 9235 | } |
| 9236 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | Py_ssize_t |
| 9238 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9239 | Py_ssize_t start, Py_ssize_t end, |
| 9240 | int direction) |
| 9241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9243 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | if (PyUnicode_READY(str) == -1) |
| 9245 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9246 | if (start < 0 || end < 0) { |
| 9247 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9248 | return -2; |
| 9249 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9250 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9251 | end = PyUnicode_GET_LENGTH(str); |
| 9252 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9253 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9254 | kind, end-start, ch, direction); |
| 9255 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9257 | else |
| 9258 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9259 | } |
| 9260 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9261 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9262 | tailmatch(PyObject *self, |
| 9263 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9264 | Py_ssize_t start, |
| 9265 | Py_ssize_t end, |
| 9266 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9267 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9268 | int kind_self; |
| 9269 | int kind_sub; |
| 9270 | void *data_self; |
| 9271 | void *data_sub; |
| 9272 | Py_ssize_t offset; |
| 9273 | Py_ssize_t i; |
| 9274 | Py_ssize_t end_sub; |
| 9275 | |
| 9276 | if (PyUnicode_READY(self) == -1 || |
| 9277 | PyUnicode_READY(substring) == -1) |
| 9278 | return 0; |
| 9279 | |
| 9280 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9281 | return 1; |
| 9282 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9283 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9284 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9285 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9286 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9288 | kind_self = PyUnicode_KIND(self); |
| 9289 | data_self = PyUnicode_DATA(self); |
| 9290 | kind_sub = PyUnicode_KIND(substring); |
| 9291 | data_sub = PyUnicode_DATA(substring); |
| 9292 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9293 | |
| 9294 | if (direction > 0) |
| 9295 | offset = end; |
| 9296 | else |
| 9297 | offset = start; |
| 9298 | |
| 9299 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9300 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9301 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9302 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9303 | /* If both are of the same kind, memcmp is sufficient */ |
| 9304 | if (kind_self == kind_sub) { |
| 9305 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9306 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9307 | data_sub, |
| 9308 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9309 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9310 | } |
| 9311 | /* otherwise we have to compare each character by first accesing it */ |
| 9312 | else { |
| 9313 | /* We do not need to compare 0 and len(substring)-1 because |
| 9314 | the if statement above ensured already that they are equal |
| 9315 | when we end up here. */ |
| 9316 | // TODO: honor direction and do a forward or backwards search |
| 9317 | for (i = 1; i < end_sub; ++i) { |
| 9318 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9319 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9320 | return 0; |
| 9321 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9322 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9324 | } |
| 9325 | |
| 9326 | return 0; |
| 9327 | } |
| 9328 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9329 | Py_ssize_t |
| 9330 | PyUnicode_Tailmatch(PyObject *str, |
| 9331 | PyObject *substr, |
| 9332 | Py_ssize_t start, |
| 9333 | Py_ssize_t end, |
| 9334 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9335 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9336 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9338 | str = PyUnicode_FromObject(str); |
| 9339 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9340 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9341 | substr = PyUnicode_FromObject(substr); |
| 9342 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9343 | Py_DECREF(str); |
| 9344 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9345 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9346 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9347 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9348 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9349 | Py_DECREF(str); |
| 9350 | Py_DECREF(substr); |
| 9351 | return result; |
| 9352 | } |
| 9353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | /* Apply fixfct filter to the Unicode object self and return a |
| 9355 | reference to the modified object */ |
| 9356 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9357 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9358 | fixup(PyObject *self, |
| 9359 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9361 | PyObject *u; |
| 9362 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9364 | u = PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9366 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9367 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9369 | /* fix functions return the new maximum character in a string, |
| 9370 | if the kind of the resulting unicode object does not change, |
| 9371 | everything is fine. Otherwise we need to change the string kind |
| 9372 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9373 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9374 | if (maxchar_new == 0) |
| 9375 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 9376 | else if (maxchar_new <= 127) |
| 9377 | maxchar_new = 127; |
| 9378 | else if (maxchar_new <= 255) |
| 9379 | maxchar_new = 255; |
| 9380 | else if (maxchar_new <= 65535) |
| 9381 | maxchar_new = 65535; |
| 9382 | else |
| 9383 | maxchar_new = 1114111; /* 0x10ffff */ |
| 9384 | |
| 9385 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9386 | /* fixfct should return TRUE if it modified the buffer. If |
| 9387 | FALSE, return a reference to the original buffer instead |
| 9388 | (to save space, not time) */ |
| 9389 | Py_INCREF(self); |
| 9390 | Py_DECREF(u); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9391 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9393 | else if (maxchar_new == maxchar_old) { |
| 9394 | return u; |
| 9395 | } |
| 9396 | else { |
| 9397 | /* In case the maximum character changed, we need to |
| 9398 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9399 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | if (v == NULL) { |
| 9401 | Py_DECREF(u); |
| 9402 | return NULL; |
| 9403 | } |
| 9404 | if (maxchar_new > maxchar_old) { |
| 9405 | /* If the maxchar increased so that the kind changed, not all |
| 9406 | characters are representable anymore and we need to fix the |
| 9407 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9408 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9409 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 9411 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9412 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9413 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9414 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | |
| 9416 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9417 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | return v; |
| 9419 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9420 | } |
| 9421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9422 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9423 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | /* No need to call PyUnicode_READY(self) because this function is only |
| 9426 | called as a callback from fixup() which does it already. */ |
| 9427 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9428 | const int kind = PyUnicode_KIND(self); |
| 9429 | void *data = PyUnicode_DATA(self); |
| 9430 | int touched = 0; |
| 9431 | Py_UCS4 maxchar = 0; |
| 9432 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9433 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9434 | for (i = 0; i < len; ++i) { |
| 9435 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9436 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 9437 | if (up != ch) { |
| 9438 | if (up > maxchar) |
| 9439 | maxchar = up; |
| 9440 | PyUnicode_WRITE(kind, data, i, up); |
| 9441 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | else if (ch > maxchar) |
| 9444 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | if (touched) |
| 9448 | return maxchar; |
| 9449 | else |
| 9450 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9451 | } |
| 9452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9454 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9456 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9457 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9458 | const int kind = PyUnicode_KIND(self); |
| 9459 | void *data = PyUnicode_DATA(self); |
| 9460 | int touched = 0; |
| 9461 | Py_UCS4 maxchar = 0; |
| 9462 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | for(i = 0; i < len; ++i) { |
| 9465 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9466 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9467 | if (lo != ch) { |
| 9468 | if (lo > maxchar) |
| 9469 | maxchar = lo; |
| 9470 | PyUnicode_WRITE(kind, data, i, lo); |
| 9471 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9472 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9473 | else if (ch > maxchar) |
| 9474 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9477 | if (touched) |
| 9478 | return maxchar; |
| 9479 | else |
| 9480 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | } |
| 9482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9484 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9486 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9487 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9488 | const int kind = PyUnicode_KIND(self); |
| 9489 | void *data = PyUnicode_DATA(self); |
| 9490 | int touched = 0; |
| 9491 | Py_UCS4 maxchar = 0; |
| 9492 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | for(i = 0; i < len; ++i) { |
| 9495 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9496 | Py_UCS4 nu = 0; |
| 9497 | |
| 9498 | if (Py_UNICODE_ISUPPER(ch)) |
| 9499 | nu = Py_UNICODE_TOLOWER(ch); |
| 9500 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9501 | nu = Py_UNICODE_TOUPPER(ch); |
| 9502 | |
| 9503 | if (nu != 0) { |
| 9504 | if (nu > maxchar) |
| 9505 | maxchar = nu; |
| 9506 | PyUnicode_WRITE(kind, data, i, nu); |
| 9507 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9509 | else if (ch > maxchar) |
| 9510 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | } |
| 9512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9513 | if (touched) |
| 9514 | return maxchar; |
| 9515 | else |
| 9516 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9517 | } |
| 9518 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9519 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9520 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9521 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9522 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9523 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9524 | const int kind = PyUnicode_KIND(self); |
| 9525 | void *data = PyUnicode_DATA(self); |
| 9526 | int touched = 0; |
| 9527 | Py_UCS4 maxchar = 0; |
| 9528 | Py_ssize_t i = 0; |
| 9529 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9530 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9531 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9532 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | |
| 9534 | ch = PyUnicode_READ(kind, data, i); |
| 9535 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9536 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9537 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9538 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9540 | ++i; |
| 9541 | for(; i < len; ++i) { |
| 9542 | ch = PyUnicode_READ(kind, data, i); |
| 9543 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9544 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9545 | if (lo > maxchar) |
| 9546 | maxchar = lo; |
| 9547 | PyUnicode_WRITE(kind, data, i, lo); |
| 9548 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9549 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9550 | else if (ch > maxchar) |
| 9551 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9552 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9553 | |
| 9554 | if (touched) |
| 9555 | return maxchar; |
| 9556 | else |
| 9557 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | } |
| 9559 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9560 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9561 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9562 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9564 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9565 | const int kind = PyUnicode_KIND(self); |
| 9566 | void *data = PyUnicode_DATA(self); |
| 9567 | Py_UCS4 maxchar = 0; |
| 9568 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | int previous_is_cased; |
| 9570 | |
| 9571 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9572 | if (len == 1) { |
| 9573 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9574 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9575 | if (ti != ch) { |
| 9576 | PyUnicode_WRITE(kind, data, i, ti); |
| 9577 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9578 | } |
| 9579 | else |
| 9580 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9583 | for(; i < len; ++i) { |
| 9584 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9585 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9587 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9588 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9589 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9590 | nu = Py_UNICODE_TOTITLE(ch); |
| 9591 | |
| 9592 | if (nu > maxchar) |
| 9593 | maxchar = nu; |
| 9594 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9596 | if (Py_UNICODE_ISLOWER(ch) || |
| 9597 | Py_UNICODE_ISUPPER(ch) || |
| 9598 | Py_UNICODE_ISTITLE(ch)) |
| 9599 | previous_is_cased = 1; |
| 9600 | else |
| 9601 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9602 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9603 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9606 | PyObject * |
| 9607 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9609 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9610 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9611 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9612 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9613 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9614 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9615 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9617 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9618 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9619 | int use_memcpy; |
| 9620 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9621 | PyObject *last_obj; |
| 9622 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9624 | fseq = PySequence_Fast(seq, ""); |
| 9625 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9626 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9627 | } |
| 9628 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9629 | /* NOTE: the following code can't call back into Python code, |
| 9630 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9631 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9632 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9633 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9634 | /* If empty sequence, return u"". */ |
| 9635 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9636 | Py_DECREF(fseq); |
| 9637 | Py_INCREF(unicode_empty); |
| 9638 | res = unicode_empty; |
| 9639 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9640 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9641 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9642 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9643 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9644 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9645 | if (seqlen == 1) { |
| 9646 | if (PyUnicode_CheckExact(items[0])) { |
| 9647 | res = items[0]; |
| 9648 | Py_INCREF(res); |
| 9649 | Py_DECREF(fseq); |
| 9650 | return res; |
| 9651 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9652 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9653 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9654 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9655 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9656 | /* Set up sep and seplen */ |
| 9657 | if (separator == NULL) { |
| 9658 | /* fall back to a blank space separator */ |
| 9659 | sep = PyUnicode_FromOrdinal(' '); |
| 9660 | if (!sep) |
| 9661 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9662 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9663 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9664 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9665 | else { |
| 9666 | if (!PyUnicode_Check(separator)) { |
| 9667 | PyErr_Format(PyExc_TypeError, |
| 9668 | "separator: expected str instance," |
| 9669 | " %.80s found", |
| 9670 | Py_TYPE(separator)->tp_name); |
| 9671 | goto onError; |
| 9672 | } |
| 9673 | if (PyUnicode_READY(separator)) |
| 9674 | goto onError; |
| 9675 | sep = separator; |
| 9676 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9677 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9678 | /* inc refcount to keep this code path symmetric with the |
| 9679 | above case of a blank separator */ |
| 9680 | Py_INCREF(sep); |
| 9681 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9682 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9683 | } |
| 9684 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9685 | /* There are at least two things to join, or else we have a subclass |
| 9686 | * of str in the sequence. |
| 9687 | * Do a pre-pass to figure out the total amount of space we'll |
| 9688 | * need (sz), and see whether all argument are strings. |
| 9689 | */ |
| 9690 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9691 | #ifdef Py_DEBUG |
| 9692 | use_memcpy = 0; |
| 9693 | #else |
| 9694 | use_memcpy = 1; |
| 9695 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9696 | for (i = 0; i < seqlen; i++) { |
| 9697 | const Py_ssize_t old_sz = sz; |
| 9698 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9699 | if (!PyUnicode_Check(item)) { |
| 9700 | PyErr_Format(PyExc_TypeError, |
| 9701 | "sequence item %zd: expected str instance," |
| 9702 | " %.80s found", |
| 9703 | i, Py_TYPE(item)->tp_name); |
| 9704 | goto onError; |
| 9705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9706 | if (PyUnicode_READY(item) == -1) |
| 9707 | goto onError; |
| 9708 | sz += PyUnicode_GET_LENGTH(item); |
| 9709 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9710 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9711 | if (i != 0) |
| 9712 | sz += seplen; |
| 9713 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9714 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9715 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9716 | goto onError; |
| 9717 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9718 | if (use_memcpy && last_obj != NULL) { |
| 9719 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9720 | use_memcpy = 0; |
| 9721 | } |
| 9722 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9723 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9725 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9726 | if (res == NULL) |
| 9727 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9728 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9729 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9730 | #ifdef Py_DEBUG |
| 9731 | use_memcpy = 0; |
| 9732 | #else |
| 9733 | if (use_memcpy) { |
| 9734 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9735 | kind = PyUnicode_KIND(res); |
| 9736 | if (seplen != 0) |
| 9737 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9738 | } |
| 9739 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9741 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9742 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9743 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9744 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9745 | if (use_memcpy) { |
| 9746 | Py_MEMCPY(res_data, |
| 9747 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9748 | kind * seplen); |
| 9749 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9750 | } |
| 9751 | else { |
| 9752 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9753 | res_offset += seplen; |
| 9754 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9755 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9756 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9757 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9758 | if (use_memcpy) { |
| 9759 | Py_MEMCPY(res_data, |
| 9760 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9761 | kind * itemlen); |
| 9762 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9763 | } |
| 9764 | else { |
| 9765 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9766 | res_offset += itemlen; |
| 9767 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9768 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9769 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9770 | if (use_memcpy) |
| 9771 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9772 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9773 | else |
| 9774 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9775 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9776 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9778 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9780 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9781 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9782 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9784 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9785 | return NULL; |
| 9786 | } |
| 9787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | #define FILL(kind, data, value, start, length) \ |
| 9789 | do { \ |
| 9790 | Py_ssize_t i_ = 0; \ |
| 9791 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9792 | switch ((kind)) { \ |
| 9793 | case PyUnicode_1BYTE_KIND: { \ |
| 9794 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9795 | memset(to_, (unsigned char)value, length); \ |
| 9796 | break; \ |
| 9797 | } \ |
| 9798 | case PyUnicode_2BYTE_KIND: { \ |
| 9799 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9800 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9801 | break; \ |
| 9802 | } \ |
| 9803 | default: { \ |
| 9804 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9805 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9806 | break; \ |
| 9807 | } \ |
| 9808 | } \ |
| 9809 | } while (0) |
| 9810 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9811 | static PyObject * |
| 9812 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9813 | Py_ssize_t left, |
| 9814 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9815 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9816 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9817 | PyObject *u; |
| 9818 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9819 | int kind; |
| 9820 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9821 | |
| 9822 | if (left < 0) |
| 9823 | left = 0; |
| 9824 | if (right < 0) |
| 9825 | right = 0; |
| 9826 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9827 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9828 | Py_INCREF(self); |
| 9829 | return self; |
| 9830 | } |
| 9831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9833 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9834 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9835 | return NULL; |
| 9836 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9837 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9838 | if (fill > maxchar) |
| 9839 | maxchar = fill; |
| 9840 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9841 | if (!u) |
| 9842 | return NULL; |
| 9843 | |
| 9844 | kind = PyUnicode_KIND(u); |
| 9845 | data = PyUnicode_DATA(u); |
| 9846 | if (left) |
| 9847 | FILL(kind, data, fill, 0, left); |
| 9848 | if (right) |
| 9849 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9850 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9851 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9852 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9853 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9854 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9855 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9856 | PyObject * |
| 9857 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9858 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9859 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | |
| 9861 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9863 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | switch(PyUnicode_KIND(string)) { |
| 9866 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9867 | if (PyUnicode_IS_ASCII(string)) |
| 9868 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9869 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9870 | PyUnicode_GET_LENGTH(string), keepends); |
| 9871 | else |
| 9872 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9873 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9874 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | break; |
| 9876 | case PyUnicode_2BYTE_KIND: |
| 9877 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9878 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9879 | PyUnicode_GET_LENGTH(string), keepends); |
| 9880 | break; |
| 9881 | case PyUnicode_4BYTE_KIND: |
| 9882 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9883 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9884 | PyUnicode_GET_LENGTH(string), keepends); |
| 9885 | break; |
| 9886 | default: |
| 9887 | assert(0); |
| 9888 | list = 0; |
| 9889 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9890 | Py_DECREF(string); |
| 9891 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9892 | } |
| 9893 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9894 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9895 | split(PyObject *self, |
| 9896 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9897 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9898 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | int kind1, kind2, kind; |
| 9900 | void *buf1, *buf2; |
| 9901 | Py_ssize_t len1, len2; |
| 9902 | PyObject* out; |
| 9903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9905 | maxcount = PY_SSIZE_T_MAX; |
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 | if (PyUnicode_READY(self) == -1) |
| 9908 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9910 | if (substring == NULL) |
| 9911 | switch(PyUnicode_KIND(self)) { |
| 9912 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9913 | if (PyUnicode_IS_ASCII(self)) |
| 9914 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9915 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9916 | PyUnicode_GET_LENGTH(self), maxcount |
| 9917 | ); |
| 9918 | else |
| 9919 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9920 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9921 | PyUnicode_GET_LENGTH(self), maxcount |
| 9922 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9923 | case PyUnicode_2BYTE_KIND: |
| 9924 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9925 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | PyUnicode_GET_LENGTH(self), maxcount |
| 9927 | ); |
| 9928 | case PyUnicode_4BYTE_KIND: |
| 9929 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9930 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | PyUnicode_GET_LENGTH(self), maxcount |
| 9932 | ); |
| 9933 | default: |
| 9934 | assert(0); |
| 9935 | return NULL; |
| 9936 | } |
| 9937 | |
| 9938 | if (PyUnicode_READY(substring) == -1) |
| 9939 | return NULL; |
| 9940 | |
| 9941 | kind1 = PyUnicode_KIND(self); |
| 9942 | kind2 = PyUnicode_KIND(substring); |
| 9943 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9944 | buf1 = PyUnicode_DATA(self); |
| 9945 | buf2 = PyUnicode_DATA(substring); |
| 9946 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9947 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9948 | if (!buf1) |
| 9949 | return NULL; |
| 9950 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9951 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9952 | if (!buf2) { |
| 9953 | if (kind1 != kind) PyMem_Free(buf1); |
| 9954 | return NULL; |
| 9955 | } |
| 9956 | len1 = PyUnicode_GET_LENGTH(self); |
| 9957 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9958 | |
| 9959 | switch(kind) { |
| 9960 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9961 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9962 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9963 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9964 | else |
| 9965 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9966 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | break; |
| 9968 | case PyUnicode_2BYTE_KIND: |
| 9969 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9970 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9971 | break; |
| 9972 | case PyUnicode_4BYTE_KIND: |
| 9973 | out = ucs4lib_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 | default: |
| 9977 | out = NULL; |
| 9978 | } |
| 9979 | if (kind1 != kind) |
| 9980 | PyMem_Free(buf1); |
| 9981 | if (kind2 != kind) |
| 9982 | PyMem_Free(buf2); |
| 9983 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9984 | } |
| 9985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9986 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9987 | rsplit(PyObject *self, |
| 9988 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9989 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9990 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | int kind1, kind2, kind; |
| 9992 | void *buf1, *buf2; |
| 9993 | Py_ssize_t len1, len2; |
| 9994 | PyObject* out; |
| 9995 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9996 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9997 | maxcount = PY_SSIZE_T_MAX; |
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 | if (PyUnicode_READY(self) == -1) |
| 10000 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10002 | if (substring == NULL) |
| 10003 | switch(PyUnicode_KIND(self)) { |
| 10004 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10005 | if (PyUnicode_IS_ASCII(self)) |
| 10006 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10007 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10008 | PyUnicode_GET_LENGTH(self), maxcount |
| 10009 | ); |
| 10010 | else |
| 10011 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10012 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10013 | PyUnicode_GET_LENGTH(self), maxcount |
| 10014 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | case PyUnicode_2BYTE_KIND: |
| 10016 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10017 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10018 | PyUnicode_GET_LENGTH(self), maxcount |
| 10019 | ); |
| 10020 | case PyUnicode_4BYTE_KIND: |
| 10021 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10022 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | PyUnicode_GET_LENGTH(self), maxcount |
| 10024 | ); |
| 10025 | default: |
| 10026 | assert(0); |
| 10027 | return NULL; |
| 10028 | } |
| 10029 | |
| 10030 | if (PyUnicode_READY(substring) == -1) |
| 10031 | return NULL; |
| 10032 | |
| 10033 | kind1 = PyUnicode_KIND(self); |
| 10034 | kind2 = PyUnicode_KIND(substring); |
| 10035 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10036 | buf1 = PyUnicode_DATA(self); |
| 10037 | buf2 = PyUnicode_DATA(substring); |
| 10038 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10039 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | if (!buf1) |
| 10041 | return NULL; |
| 10042 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10043 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | if (!buf2) { |
| 10045 | if (kind1 != kind) PyMem_Free(buf1); |
| 10046 | return NULL; |
| 10047 | } |
| 10048 | len1 = PyUnicode_GET_LENGTH(self); |
| 10049 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10050 | |
| 10051 | switch(kind) { |
| 10052 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10053 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10054 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10055 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10056 | else |
| 10057 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10058 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | break; |
| 10060 | case PyUnicode_2BYTE_KIND: |
| 10061 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10062 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | break; |
| 10064 | case PyUnicode_4BYTE_KIND: |
| 10065 | out = ucs4lib_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 | default: |
| 10069 | out = NULL; |
| 10070 | } |
| 10071 | if (kind1 != kind) |
| 10072 | PyMem_Free(buf1); |
| 10073 | if (kind2 != kind) |
| 10074 | PyMem_Free(buf2); |
| 10075 | return out; |
| 10076 | } |
| 10077 | |
| 10078 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10079 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10080 | 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] | 10081 | { |
| 10082 | switch(kind) { |
| 10083 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10084 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10085 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10086 | else |
| 10087 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | case PyUnicode_2BYTE_KIND: |
| 10089 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10090 | case PyUnicode_4BYTE_KIND: |
| 10091 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10092 | } |
| 10093 | assert(0); |
| 10094 | return -1; |
| 10095 | } |
| 10096 | |
| 10097 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10098 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10099 | 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] | 10100 | { |
| 10101 | switch(kind) { |
| 10102 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10103 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10104 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10105 | else |
| 10106 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | case PyUnicode_2BYTE_KIND: |
| 10108 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10109 | case PyUnicode_4BYTE_KIND: |
| 10110 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10111 | } |
| 10112 | assert(0); |
| 10113 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10114 | } |
| 10115 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10116 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | replace(PyObject *self, PyObject *str1, |
| 10118 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10119 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10120 | PyObject *u; |
| 10121 | char *sbuf = PyUnicode_DATA(self); |
| 10122 | char *buf1 = PyUnicode_DATA(str1); |
| 10123 | char *buf2 = PyUnicode_DATA(str2); |
| 10124 | int srelease = 0, release1 = 0, release2 = 0; |
| 10125 | int skind = PyUnicode_KIND(self); |
| 10126 | int kind1 = PyUnicode_KIND(str1); |
| 10127 | int kind2 = PyUnicode_KIND(str2); |
| 10128 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10129 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10130 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10131 | int mayshrink; |
| 10132 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10133 | |
| 10134 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10135 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10137 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10138 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10139 | if (str1 == str2) |
| 10140 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | if (skind < kind1) |
| 10142 | /* substring too wide to be present */ |
| 10143 | goto nothing; |
| 10144 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10145 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10146 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10147 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10148 | result string. */ |
| 10149 | mayshrink = (maxchar_str2 < maxchar); |
| 10150 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 10151 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 10153 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10154 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10156 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10159 | Py_UCS4 u1, u2; |
| 10160 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 10162 | if (findchar(sbuf, PyUnicode_KIND(self), |
| 10163 | slen, u1, 1) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10164 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10167 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10169 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | rkind = PyUnicode_KIND(u); |
| 10171 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 10172 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10173 | if (--maxcount < 0) |
| 10174 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10176 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10177 | } |
| 10178 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | int rkind = skind; |
| 10180 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | if (kind1 < rkind) { |
| 10183 | /* widen substring */ |
| 10184 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10185 | if (!buf1) goto error; |
| 10186 | release1 = 1; |
| 10187 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10188 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10189 | if (i < 0) |
| 10190 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | if (rkind > kind2) { |
| 10192 | /* widen replacement */ |
| 10193 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10194 | if (!buf2) goto error; |
| 10195 | release2 = 1; |
| 10196 | } |
| 10197 | else if (rkind < kind2) { |
| 10198 | /* widen self and buf1 */ |
| 10199 | rkind = kind2; |
| 10200 | if (release1) PyMem_Free(buf1); |
| 10201 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10202 | if (!sbuf) goto error; |
| 10203 | srelease = 1; |
| 10204 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10205 | if (!buf1) goto error; |
| 10206 | release1 = 1; |
| 10207 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10208 | u = PyUnicode_New(slen, maxchar); |
| 10209 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10211 | assert(PyUnicode_KIND(u) == rkind); |
| 10212 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10213 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10214 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10215 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10216 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10218 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10219 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10220 | |
| 10221 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10222 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10223 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10224 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10225 | if (i == -1) |
| 10226 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10227 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10229 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10231 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10233 | } |
| 10234 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | Py_ssize_t n, i, j, ires; |
| 10236 | Py_ssize_t product, new_size; |
| 10237 | int rkind = skind; |
| 10238 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10240 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10241 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10243 | if (!buf1) goto error; |
| 10244 | release1 = 1; |
| 10245 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10246 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10247 | if (n == 0) |
| 10248 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10249 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10250 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10251 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10252 | if (!buf2) goto error; |
| 10253 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10256 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10257 | rkind = kind2; |
| 10258 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10259 | if (!sbuf) goto error; |
| 10260 | srelease = 1; |
| 10261 | if (release1) PyMem_Free(buf1); |
| 10262 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10263 | if (!buf1) goto error; |
| 10264 | release1 = 1; |
| 10265 | } |
| 10266 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10267 | PyUnicode_GET_LENGTH(str1))); */ |
| 10268 | product = n * (len2-len1); |
| 10269 | if ((product / (len2-len1)) != n) { |
| 10270 | PyErr_SetString(PyExc_OverflowError, |
| 10271 | "replace string is too long"); |
| 10272 | goto error; |
| 10273 | } |
| 10274 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10275 | if (new_size == 0) { |
| 10276 | Py_INCREF(unicode_empty); |
| 10277 | u = unicode_empty; |
| 10278 | goto done; |
| 10279 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10281 | PyErr_SetString(PyExc_OverflowError, |
| 10282 | "replace string is too long"); |
| 10283 | goto error; |
| 10284 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10285 | u = PyUnicode_New(new_size, maxchar); |
| 10286 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10288 | assert(PyUnicode_KIND(u) == rkind); |
| 10289 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10290 | ires = i = 0; |
| 10291 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10292 | while (n-- > 0) { |
| 10293 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10294 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10295 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10296 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10297 | if (j == -1) |
| 10298 | break; |
| 10299 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10300 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10301 | memcpy(res + rkind * ires, |
| 10302 | sbuf + rkind * i, |
| 10303 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10304 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10305 | } |
| 10306 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10308 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10310 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10312 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10313 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10314 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10316 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10317 | memcpy(res + rkind * ires, |
| 10318 | sbuf + rkind * i, |
| 10319 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10320 | } |
| 10321 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10322 | /* interleave */ |
| 10323 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10324 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10325 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10326 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10327 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10328 | if (--n <= 0) |
| 10329 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10330 | memcpy(res + rkind * ires, |
| 10331 | sbuf + rkind * i, |
| 10332 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | ires++; |
| 10334 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10335 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10336 | memcpy(res + rkind * ires, |
| 10337 | sbuf + rkind * i, |
| 10338 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10339 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10340 | } |
| 10341 | |
| 10342 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10343 | unicode_adjust_maxchar(&u); |
| 10344 | if (u == NULL) |
| 10345 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10346 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10347 | |
| 10348 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10349 | if (srelease) |
| 10350 | PyMem_FREE(sbuf); |
| 10351 | if (release1) |
| 10352 | PyMem_FREE(buf1); |
| 10353 | if (release2) |
| 10354 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10355 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10356 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10357 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10358 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10359 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | if (srelease) |
| 10361 | PyMem_FREE(sbuf); |
| 10362 | if (release1) |
| 10363 | PyMem_FREE(buf1); |
| 10364 | if (release2) |
| 10365 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10366 | if (PyUnicode_CheckExact(self)) { |
| 10367 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10368 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10369 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10370 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | error: |
| 10372 | if (srelease && sbuf) |
| 10373 | PyMem_FREE(sbuf); |
| 10374 | if (release1 && buf1) |
| 10375 | PyMem_FREE(buf1); |
| 10376 | if (release2 && buf2) |
| 10377 | PyMem_FREE(buf2); |
| 10378 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10379 | } |
| 10380 | |
| 10381 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10383 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10384 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | \n\ |
| 10386 | 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] | 10387 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10388 | |
| 10389 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10390 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10391 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10392 | return fixup(self, fixtitle); |
| 10393 | } |
| 10394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10395 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10396 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10397 | \n\ |
| 10398 | 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] | 10399 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | |
| 10401 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10402 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10403 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10404 | return fixup(self, fixcapitalize); |
| 10405 | } |
| 10406 | |
| 10407 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10408 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10409 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | \n\ |
| 10411 | 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] | 10412 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10413 | |
| 10414 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10415 | unicode_capwords(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10416 | { |
| 10417 | PyObject *list; |
| 10418 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10419 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | /* Split into words */ |
| 10422 | list = split(self, NULL, -1); |
| 10423 | if (!list) |
| 10424 | return NULL; |
| 10425 | |
| 10426 | /* Capitalize each word */ |
| 10427 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10428 | item = fixup(PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10429 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10430 | if (item == NULL) |
| 10431 | goto onError; |
| 10432 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 10433 | PyList_SET_ITEM(list, i, item); |
| 10434 | } |
| 10435 | |
| 10436 | /* Join the words to form a new string */ |
| 10437 | item = PyUnicode_Join(NULL, list); |
| 10438 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10439 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10440 | Py_DECREF(list); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10441 | return item; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | } |
| 10443 | #endif |
| 10444 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10445 | /* Argument converter. Coerces to a single unicode character */ |
| 10446 | |
| 10447 | static int |
| 10448 | convert_uc(PyObject *obj, void *addr) |
| 10449 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10450 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10451 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10452 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10453 | uniobj = PyUnicode_FromObject(obj); |
| 10454 | if (uniobj == NULL) { |
| 10455 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10456 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10457 | return 0; |
| 10458 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10460 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10461 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10462 | Py_DECREF(uniobj); |
| 10463 | return 0; |
| 10464 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10466 | Py_DECREF(uniobj); |
| 10467 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10470 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10471 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10473 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10474 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10475 | |
| 10476 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10477 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10479 | Py_ssize_t marg, left; |
| 10480 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10481 | Py_UCS4 fillchar = ' '; |
| 10482 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10483 | 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] | 10484 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10485 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10486 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10487 | return NULL; |
| 10488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10491 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10492 | } |
| 10493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10495 | left = marg / 2 + (marg & width & 1); |
| 10496 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10497 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | } |
| 10499 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10500 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10501 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10502 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10503 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10504 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10505 | int kind1, kind2; |
| 10506 | void *data1, *data2; |
| 10507 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | kind1 = PyUnicode_KIND(str1); |
| 10510 | kind2 = PyUnicode_KIND(str2); |
| 10511 | data1 = PyUnicode_DATA(str1); |
| 10512 | data2 = PyUnicode_DATA(str2); |
| 10513 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10514 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10515 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10516 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10517 | Py_UCS4 c1, c2; |
| 10518 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10519 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10520 | |
| 10521 | if (c1 != c2) |
| 10522 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10523 | } |
| 10524 | |
| 10525 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10526 | } |
| 10527 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10528 | int |
| 10529 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10531 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10532 | if (PyUnicode_READY(left) == -1 || |
| 10533 | PyUnicode_READY(right) == -1) |
| 10534 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10535 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10536 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10537 | PyErr_Format(PyExc_TypeError, |
| 10538 | "Can't compare %.100s and %.100s", |
| 10539 | left->ob_type->tp_name, |
| 10540 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 | return -1; |
| 10542 | } |
| 10543 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10544 | int |
| 10545 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10546 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | Py_ssize_t i; |
| 10548 | int kind; |
| 10549 | void *data; |
| 10550 | Py_UCS4 chr; |
| 10551 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10552 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10553 | if (PyUnicode_READY(uni) == -1) |
| 10554 | return -1; |
| 10555 | kind = PyUnicode_KIND(uni); |
| 10556 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10557 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10559 | if (chr != str[i]) |
| 10560 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10561 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10562 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10563 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10564 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10565 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10566 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10567 | return 0; |
| 10568 | } |
| 10569 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10570 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10571 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10572 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10573 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10574 | PyObject * |
| 10575 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10576 | { |
| 10577 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10578 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10579 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10580 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10581 | if (PyUnicode_READY(left) == -1 || |
| 10582 | PyUnicode_READY(right) == -1) |
| 10583 | return NULL; |
| 10584 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10585 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10586 | if (op == Py_EQ) { |
| 10587 | Py_INCREF(Py_False); |
| 10588 | return Py_False; |
| 10589 | } |
| 10590 | if (op == Py_NE) { |
| 10591 | Py_INCREF(Py_True); |
| 10592 | return Py_True; |
| 10593 | } |
| 10594 | } |
| 10595 | if (left == right) |
| 10596 | result = 0; |
| 10597 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10598 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10599 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10600 | /* Convert the return value to a Boolean */ |
| 10601 | switch (op) { |
| 10602 | case Py_EQ: |
| 10603 | v = TEST_COND(result == 0); |
| 10604 | break; |
| 10605 | case Py_NE: |
| 10606 | v = TEST_COND(result != 0); |
| 10607 | break; |
| 10608 | case Py_LE: |
| 10609 | v = TEST_COND(result <= 0); |
| 10610 | break; |
| 10611 | case Py_GE: |
| 10612 | v = TEST_COND(result >= 0); |
| 10613 | break; |
| 10614 | case Py_LT: |
| 10615 | v = TEST_COND(result == -1); |
| 10616 | break; |
| 10617 | case Py_GT: |
| 10618 | v = TEST_COND(result == 1); |
| 10619 | break; |
| 10620 | default: |
| 10621 | PyErr_BadArgument(); |
| 10622 | return NULL; |
| 10623 | } |
| 10624 | Py_INCREF(v); |
| 10625 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10626 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10627 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10628 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10629 | } |
| 10630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10631 | int |
| 10632 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10633 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10634 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | int kind1, kind2, kind; |
| 10636 | void *buf1, *buf2; |
| 10637 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10638 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10639 | |
| 10640 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10641 | sub = PyUnicode_FromObject(element); |
| 10642 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10643 | PyErr_Format(PyExc_TypeError, |
| 10644 | "'in <string>' requires string as left operand, not %s", |
| 10645 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10646 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10647 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10648 | if (PyUnicode_READY(sub) == -1) |
| 10649 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10650 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10651 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10652 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10653 | Py_DECREF(sub); |
| 10654 | return -1; |
| 10655 | } |
| 10656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10657 | kind1 = PyUnicode_KIND(str); |
| 10658 | kind2 = PyUnicode_KIND(sub); |
| 10659 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10660 | buf1 = PyUnicode_DATA(str); |
| 10661 | buf2 = PyUnicode_DATA(sub); |
| 10662 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10663 | buf1 = _PyUnicode_AsKind(str, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | if (!buf1) { |
| 10665 | Py_DECREF(sub); |
| 10666 | return -1; |
| 10667 | } |
| 10668 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10669 | buf2 = _PyUnicode_AsKind(sub, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10670 | if (!buf2) { |
| 10671 | Py_DECREF(sub); |
| 10672 | if (kind1 != kind) PyMem_Free(buf1); |
| 10673 | return -1; |
| 10674 | } |
| 10675 | len1 = PyUnicode_GET_LENGTH(str); |
| 10676 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10677 | |
| 10678 | switch(kind) { |
| 10679 | case PyUnicode_1BYTE_KIND: |
| 10680 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10681 | break; |
| 10682 | case PyUnicode_2BYTE_KIND: |
| 10683 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10684 | break; |
| 10685 | case PyUnicode_4BYTE_KIND: |
| 10686 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10687 | break; |
| 10688 | default: |
| 10689 | result = -1; |
| 10690 | assert(0); |
| 10691 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10692 | |
| 10693 | Py_DECREF(str); |
| 10694 | Py_DECREF(sub); |
| 10695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | if (kind1 != kind) |
| 10697 | PyMem_Free(buf1); |
| 10698 | if (kind2 != kind) |
| 10699 | PyMem_Free(buf2); |
| 10700 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10701 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10702 | } |
| 10703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10704 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10705 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10706 | PyObject * |
| 10707 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10709 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10710 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | |
| 10712 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10715 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10716 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10718 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | |
| 10720 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10721 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10722 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10723 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10725 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10726 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10727 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | } |
| 10729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10730 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10731 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10732 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10734 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10735 | w = PyUnicode_New( |
| 10736 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10737 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10738 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10739 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10740 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10741 | 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] | 10742 | Py_DECREF(u); |
| 10743 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10744 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10745 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10747 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | Py_XDECREF(u); |
| 10749 | Py_XDECREF(v); |
| 10750 | return NULL; |
| 10751 | } |
| 10752 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10753 | static void |
| 10754 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10755 | { |
| 10756 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10757 | |
| 10758 | assert(PyUnicode_IS_READY(*p_left)); |
| 10759 | assert(PyUnicode_IS_READY(right)); |
| 10760 | |
| 10761 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10762 | right_len = PyUnicode_GET_LENGTH(right); |
| 10763 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10764 | PyErr_SetString(PyExc_OverflowError, |
| 10765 | "strings are too large to concat"); |
| 10766 | goto error; |
| 10767 | } |
| 10768 | new_len = left_len + right_len; |
| 10769 | |
| 10770 | /* Now we own the last reference to 'left', so we can resize it |
| 10771 | * in-place. |
| 10772 | */ |
| 10773 | if (unicode_resize(p_left, new_len) != 0) { |
| 10774 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10775 | * deallocated so it cannot be put back into |
| 10776 | * 'variable'. The MemoryError is raised when there |
| 10777 | * is no value in 'variable', which might (very |
| 10778 | * remotely) be a cause of incompatibilities. |
| 10779 | */ |
| 10780 | goto error; |
| 10781 | } |
| 10782 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10783 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10784 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10785 | return; |
| 10786 | |
| 10787 | error: |
| 10788 | Py_DECREF(*p_left); |
| 10789 | *p_left = NULL; |
| 10790 | } |
| 10791 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10792 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10793 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10794 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10795 | PyObject *left, *res; |
| 10796 | |
| 10797 | if (p_left == NULL) { |
| 10798 | if (!PyErr_Occurred()) |
| 10799 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10800 | return; |
| 10801 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10802 | left = *p_left; |
| 10803 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10804 | if (!PyErr_Occurred()) |
| 10805 | PyErr_BadInternalCall(); |
| 10806 | goto error; |
| 10807 | } |
| 10808 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10809 | if (PyUnicode_READY(left)) |
| 10810 | goto error; |
| 10811 | if (PyUnicode_READY(right)) |
| 10812 | goto error; |
| 10813 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10814 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10815 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10816 | && unicode_resizable(left) |
| 10817 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10818 | || _PyUnicode_WSTR(left) != NULL)) |
| 10819 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10820 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10821 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10822 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10823 | not so different than duplicating the string. */ |
| 10824 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10825 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10826 | unicode_append_inplace(p_left, right); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10827 | assert(p_left == NULL || _PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10828 | return; |
| 10829 | } |
| 10830 | } |
| 10831 | |
| 10832 | res = PyUnicode_Concat(left, right); |
| 10833 | if (res == NULL) |
| 10834 | goto error; |
| 10835 | Py_DECREF(left); |
| 10836 | *p_left = res; |
| 10837 | return; |
| 10838 | |
| 10839 | error: |
| 10840 | Py_DECREF(*p_left); |
| 10841 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10842 | } |
| 10843 | |
| 10844 | void |
| 10845 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10846 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10847 | PyUnicode_Append(pleft, right); |
| 10848 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10849 | } |
| 10850 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10851 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10852 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10853 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10854 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10855 | 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] | 10856 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | |
| 10858 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10859 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10860 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10861 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10862 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10863 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10865 | int kind1, kind2, kind; |
| 10866 | void *buf1, *buf2; |
| 10867 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10869 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10870 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10871 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | kind1 = PyUnicode_KIND(self); |
| 10874 | kind2 = PyUnicode_KIND(substring); |
| 10875 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10876 | buf1 = PyUnicode_DATA(self); |
| 10877 | buf2 = PyUnicode_DATA(substring); |
| 10878 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10879 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10880 | if (!buf1) { |
| 10881 | Py_DECREF(substring); |
| 10882 | return NULL; |
| 10883 | } |
| 10884 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10885 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10886 | if (!buf2) { |
| 10887 | Py_DECREF(substring); |
| 10888 | if (kind1 != kind) PyMem_Free(buf1); |
| 10889 | return NULL; |
| 10890 | } |
| 10891 | len1 = PyUnicode_GET_LENGTH(self); |
| 10892 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10893 | |
| 10894 | ADJUST_INDICES(start, end, len1); |
| 10895 | switch(kind) { |
| 10896 | case PyUnicode_1BYTE_KIND: |
| 10897 | iresult = ucs1lib_count( |
| 10898 | ((Py_UCS1*)buf1) + start, end - start, |
| 10899 | buf2, len2, PY_SSIZE_T_MAX |
| 10900 | ); |
| 10901 | break; |
| 10902 | case PyUnicode_2BYTE_KIND: |
| 10903 | iresult = ucs2lib_count( |
| 10904 | ((Py_UCS2*)buf1) + start, end - start, |
| 10905 | buf2, len2, PY_SSIZE_T_MAX |
| 10906 | ); |
| 10907 | break; |
| 10908 | case PyUnicode_4BYTE_KIND: |
| 10909 | iresult = ucs4lib_count( |
| 10910 | ((Py_UCS4*)buf1) + start, end - start, |
| 10911 | buf2, len2, PY_SSIZE_T_MAX |
| 10912 | ); |
| 10913 | break; |
| 10914 | default: |
| 10915 | assert(0); iresult = 0; |
| 10916 | } |
| 10917 | |
| 10918 | result = PyLong_FromSsize_t(iresult); |
| 10919 | |
| 10920 | if (kind1 != kind) |
| 10921 | PyMem_Free(buf1); |
| 10922 | if (kind2 != kind) |
| 10923 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | |
| 10925 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10927 | return result; |
| 10928 | } |
| 10929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10930 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10931 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10932 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10933 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10934 | 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] | 10935 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10936 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10937 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10938 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10939 | |
| 10940 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10941 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10943 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10944 | char *encoding = NULL; |
| 10945 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10946 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10947 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10948 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10949 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10950 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10951 | } |
| 10952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10953 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10954 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10955 | \n\ |
| 10956 | 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] | 10957 | 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] | 10958 | |
| 10959 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10960 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10962 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10963 | Py_UCS4 ch; |
| 10964 | PyObject *u; |
| 10965 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10967 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10968 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10969 | |
| 10970 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10971 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10973 | if (PyUnicode_READY(self) == -1) |
| 10974 | return NULL; |
| 10975 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10976 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10977 | src_len = PyUnicode_GET_LENGTH(self); |
| 10978 | i = j = line_pos = 0; |
| 10979 | kind = PyUnicode_KIND(self); |
| 10980 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10981 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10982 | for (; i < src_len; i++) { |
| 10983 | ch = PyUnicode_READ(kind, src_data, i); |
| 10984 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10985 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10986 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10987 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10988 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10989 | goto overflow; |
| 10990 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10991 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10992 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10993 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10995 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10996 | goto overflow; |
| 10997 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10999 | if (ch == '\n' || ch == '\r') |
| 11000 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11002 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11003 | if (!found && PyUnicode_CheckExact(self)) { |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11004 | Py_INCREF(self); |
| 11005 | return self; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11006 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11009 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 | if (!u) |
| 11011 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11012 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11014 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11016 | for (; i < src_len; i++) { |
| 11017 | ch = PyUnicode_READ(kind, src_data, i); |
| 11018 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11019 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11020 | incr = tabsize - (line_pos % tabsize); |
| 11021 | line_pos += incr; |
| 11022 | while (incr--) { |
| 11023 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 11024 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11025 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11026 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11027 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11028 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11029 | line_pos++; |
| 11030 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11031 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11032 | if (ch == '\n' || ch == '\r') |
| 11033 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11035 | } |
| 11036 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11037 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11038 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11039 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11040 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11041 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | } |
| 11043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11044 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11045 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | \n\ |
| 11047 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11048 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | arguments start and end are interpreted as in slice notation.\n\ |
| 11050 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11051 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 | |
| 11053 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11056 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11057 | Py_ssize_t start; |
| 11058 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11059 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11061 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11062 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | if (PyUnicode_READY(self) == -1) |
| 11066 | return NULL; |
| 11067 | if (PyUnicode_READY(substring) == -1) |
| 11068 | return NULL; |
| 11069 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11070 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | |
| 11072 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11074 | if (result == -2) |
| 11075 | return NULL; |
| 11076 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11077 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | } |
| 11079 | |
| 11080 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11081 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11082 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11083 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 11084 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11086 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11087 | } |
| 11088 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11089 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11090 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11091 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11092 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11094 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11095 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11097 | if (_PyUnicode_HASH(self) != -1) |
| 11098 | return _PyUnicode_HASH(self); |
| 11099 | if (PyUnicode_READY(self) == -1) |
| 11100 | return -1; |
| 11101 | len = PyUnicode_GET_LENGTH(self); |
| 11102 | |
| 11103 | /* The hash function as a macro, gets expanded three times below. */ |
| 11104 | #define HASH(P) \ |
| 11105 | x = (Py_uhash_t)*P << 7; \ |
| 11106 | while (--len >= 0) \ |
| 11107 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 11108 | |
| 11109 | switch (PyUnicode_KIND(self)) { |
| 11110 | case PyUnicode_1BYTE_KIND: { |
| 11111 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11112 | HASH(c); |
| 11113 | break; |
| 11114 | } |
| 11115 | case PyUnicode_2BYTE_KIND: { |
| 11116 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11117 | HASH(s); |
| 11118 | break; |
| 11119 | } |
| 11120 | default: { |
| 11121 | Py_UCS4 *l; |
| 11122 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11123 | "Impossible switch case in unicode_hash"); |
| 11124 | l = PyUnicode_4BYTE_DATA(self); |
| 11125 | HASH(l); |
| 11126 | break; |
| 11127 | } |
| 11128 | } |
| 11129 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 11130 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11131 | if (x == -1) |
| 11132 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11133 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11134 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11137 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11138 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11139 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11140 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11141 | 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] | 11142 | |
| 11143 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11144 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11146 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11147 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11148 | Py_ssize_t start; |
| 11149 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11151 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11152 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11153 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | if (PyUnicode_READY(self) == -1) |
| 11156 | return NULL; |
| 11157 | if (PyUnicode_READY(substring) == -1) |
| 11158 | return NULL; |
| 11159 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11160 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | |
| 11162 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | if (result == -2) |
| 11165 | return NULL; |
| 11166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11167 | if (result < 0) { |
| 11168 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11169 | return NULL; |
| 11170 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11171 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11172 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | } |
| 11174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11175 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11176 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11178 | 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] | 11179 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
| 11181 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11182 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11184 | Py_ssize_t i, length; |
| 11185 | int kind; |
| 11186 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | int cased; |
| 11188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | if (PyUnicode_READY(self) == -1) |
| 11190 | return NULL; |
| 11191 | length = PyUnicode_GET_LENGTH(self); |
| 11192 | kind = PyUnicode_KIND(self); |
| 11193 | data = PyUnicode_DATA(self); |
| 11194 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | if (length == 1) |
| 11197 | return PyBool_FromLong( |
| 11198 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11200 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | for (i = 0; i < length; i++) { |
| 11206 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11208 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11209 | return PyBool_FromLong(0); |
| 11210 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11211 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11213 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11214 | } |
| 11215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11216 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11217 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11219 | 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] | 11220 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
| 11222 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11223 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11225 | Py_ssize_t i, length; |
| 11226 | int kind; |
| 11227 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | int cased; |
| 11229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | if (PyUnicode_READY(self) == -1) |
| 11231 | return NULL; |
| 11232 | length = PyUnicode_GET_LENGTH(self); |
| 11233 | kind = PyUnicode_KIND(self); |
| 11234 | data = PyUnicode_DATA(self); |
| 11235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11237 | if (length == 1) |
| 11238 | return PyBool_FromLong( |
| 11239 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11241 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11242 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11243 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | for (i = 0; i < length; i++) { |
| 11247 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11248 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11249 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11250 | return PyBool_FromLong(0); |
| 11251 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11252 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11254 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | } |
| 11256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11257 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11258 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11260 | Return True if S is a titlecased string and there is at least one\n\ |
| 11261 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11262 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11263 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | |
| 11265 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11266 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | Py_ssize_t i, length; |
| 11269 | int kind; |
| 11270 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11271 | int cased, previous_is_cased; |
| 11272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11273 | if (PyUnicode_READY(self) == -1) |
| 11274 | return NULL; |
| 11275 | length = PyUnicode_GET_LENGTH(self); |
| 11276 | kind = PyUnicode_KIND(self); |
| 11277 | data = PyUnicode_DATA(self); |
| 11278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11280 | if (length == 1) { |
| 11281 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11282 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11283 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11286 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11287 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11288 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | cased = 0; |
| 11291 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11292 | for (i = 0; i < length; i++) { |
| 11293 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11296 | if (previous_is_cased) |
| 11297 | return PyBool_FromLong(0); |
| 11298 | previous_is_cased = 1; |
| 11299 | cased = 1; |
| 11300 | } |
| 11301 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11302 | if (!previous_is_cased) |
| 11303 | return PyBool_FromLong(0); |
| 11304 | previous_is_cased = 1; |
| 11305 | cased = 1; |
| 11306 | } |
| 11307 | else |
| 11308 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11309 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11310 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11311 | } |
| 11312 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11313 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11314 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11316 | Return True if all characters in S are whitespace\n\ |
| 11317 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11318 | |
| 11319 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11320 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | Py_ssize_t i, length; |
| 11323 | int kind; |
| 11324 | void *data; |
| 11325 | |
| 11326 | if (PyUnicode_READY(self) == -1) |
| 11327 | return NULL; |
| 11328 | length = PyUnicode_GET_LENGTH(self); |
| 11329 | kind = PyUnicode_KIND(self); |
| 11330 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | if (length == 1) |
| 11334 | return PyBool_FromLong( |
| 11335 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11336 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11337 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11338 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11339 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | for (i = 0; i < length; i++) { |
| 11342 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11343 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11344 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11346 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | } |
| 11348 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11349 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11350 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11351 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11352 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11353 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11354 | |
| 11355 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11356 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11357 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11358 | Py_ssize_t i, length; |
| 11359 | int kind; |
| 11360 | void *data; |
| 11361 | |
| 11362 | if (PyUnicode_READY(self) == -1) |
| 11363 | return NULL; |
| 11364 | length = PyUnicode_GET_LENGTH(self); |
| 11365 | kind = PyUnicode_KIND(self); |
| 11366 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11367 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11368 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11369 | if (length == 1) |
| 11370 | return PyBool_FromLong( |
| 11371 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11372 | |
| 11373 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11375 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11377 | for (i = 0; i < length; i++) { |
| 11378 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11379 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11380 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11381 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11382 | } |
| 11383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11384 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11385 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11386 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11387 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11388 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11389 | |
| 11390 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11391 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11392 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | int kind; |
| 11394 | void *data; |
| 11395 | Py_ssize_t len, i; |
| 11396 | |
| 11397 | if (PyUnicode_READY(self) == -1) |
| 11398 | return NULL; |
| 11399 | |
| 11400 | kind = PyUnicode_KIND(self); |
| 11401 | data = PyUnicode_DATA(self); |
| 11402 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11403 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11404 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11405 | if (len == 1) { |
| 11406 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11407 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11408 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11409 | |
| 11410 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11411 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11412 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11414 | for (i = 0; i < len; i++) { |
| 11415 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11416 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11417 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11418 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11419 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11420 | } |
| 11421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11422 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11423 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11425 | 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] | 11426 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11427 | |
| 11428 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11429 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11431 | Py_ssize_t i, length; |
| 11432 | int kind; |
| 11433 | void *data; |
| 11434 | |
| 11435 | if (PyUnicode_READY(self) == -1) |
| 11436 | return NULL; |
| 11437 | length = PyUnicode_GET_LENGTH(self); |
| 11438 | kind = PyUnicode_KIND(self); |
| 11439 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | if (length == 1) |
| 11443 | return PyBool_FromLong( |
| 11444 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11446 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11449 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 | for (i = 0; i < length; i++) { |
| 11451 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11452 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11454 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11457 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11458 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11460 | Return True if all characters in S are digits\n\ |
| 11461 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
| 11463 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11464 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11466 | Py_ssize_t i, length; |
| 11467 | int kind; |
| 11468 | void *data; |
| 11469 | |
| 11470 | if (PyUnicode_READY(self) == -1) |
| 11471 | return NULL; |
| 11472 | length = PyUnicode_GET_LENGTH(self); |
| 11473 | kind = PyUnicode_KIND(self); |
| 11474 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | if (length == 1) { |
| 11478 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11479 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11482 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11484 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11485 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11486 | for (i = 0; i < length; i++) { |
| 11487 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11490 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | } |
| 11492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11493 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11494 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11496 | 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] | 11497 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | |
| 11499 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11500 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11502 | Py_ssize_t i, length; |
| 11503 | int kind; |
| 11504 | void *data; |
| 11505 | |
| 11506 | if (PyUnicode_READY(self) == -1) |
| 11507 | return NULL; |
| 11508 | length = PyUnicode_GET_LENGTH(self); |
| 11509 | kind = PyUnicode_KIND(self); |
| 11510 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | if (length == 1) |
| 11514 | return PyBool_FromLong( |
| 11515 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11517 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11518 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | for (i = 0; i < length; i++) { |
| 11522 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11525 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11526 | } |
| 11527 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11528 | int |
| 11529 | PyUnicode_IsIdentifier(PyObject *self) |
| 11530 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11531 | int kind; |
| 11532 | void *data; |
| 11533 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11534 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11535 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | if (PyUnicode_READY(self) == -1) { |
| 11537 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11538 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11539 | } |
| 11540 | |
| 11541 | /* Special case for empty strings */ |
| 11542 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11543 | return 0; |
| 11544 | kind = PyUnicode_KIND(self); |
| 11545 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11546 | |
| 11547 | /* PEP 3131 says that the first character must be in |
| 11548 | XID_Start and subsequent characters in XID_Continue, |
| 11549 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11550 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11551 | letters, digits, underscore). However, given the current |
| 11552 | definition of XID_Start and XID_Continue, it is sufficient |
| 11553 | to check just for these, except that _ must be allowed |
| 11554 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11556 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11557 | return 0; |
| 11558 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11559 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11561 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11562 | return 1; |
| 11563 | } |
| 11564 | |
| 11565 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11566 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11567 | \n\ |
| 11568 | Return True if S is a valid identifier according\n\ |
| 11569 | to the language definition."); |
| 11570 | |
| 11571 | static PyObject* |
| 11572 | unicode_isidentifier(PyObject *self) |
| 11573 | { |
| 11574 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11575 | } |
| 11576 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11577 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11578 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11579 | \n\ |
| 11580 | Return True if all characters in S are considered\n\ |
| 11581 | printable in repr() or S is empty, False otherwise."); |
| 11582 | |
| 11583 | static PyObject* |
| 11584 | unicode_isprintable(PyObject *self) |
| 11585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | Py_ssize_t i, length; |
| 11587 | int kind; |
| 11588 | void *data; |
| 11589 | |
| 11590 | if (PyUnicode_READY(self) == -1) |
| 11591 | return NULL; |
| 11592 | length = PyUnicode_GET_LENGTH(self); |
| 11593 | kind = PyUnicode_KIND(self); |
| 11594 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11595 | |
| 11596 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11597 | if (length == 1) |
| 11598 | return PyBool_FromLong( |
| 11599 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11601 | for (i = 0; i < length; i++) { |
| 11602 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11603 | Py_RETURN_FALSE; |
| 11604 | } |
| 11605 | } |
| 11606 | Py_RETURN_TRUE; |
| 11607 | } |
| 11608 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11609 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11610 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | \n\ |
| 11612 | 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] | 11613 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11614 | |
| 11615 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11616 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11617 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11618 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | } |
| 11620 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11621 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11622 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11623 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11624 | if (PyUnicode_READY(self) == -1) |
| 11625 | return -1; |
| 11626 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | } |
| 11628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11629 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11630 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11632 | 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] | 11633 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | |
| 11635 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11636 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11637 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11638 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11639 | Py_UCS4 fillchar = ' '; |
| 11640 | |
| 11641 | if (PyUnicode_READY(self) == -1) |
| 11642 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11643 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11644 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | return NULL; |
| 11646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11647 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11648 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11649 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11650 | } |
| 11651 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11652 | return pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | } |
| 11654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11655 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11656 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11657 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11658 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | |
| 11660 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11661 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11662 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11663 | return fixup(self, fixlower); |
| 11664 | } |
| 11665 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11666 | #define LEFTSTRIP 0 |
| 11667 | #define RIGHTSTRIP 1 |
| 11668 | #define BOTHSTRIP 2 |
| 11669 | |
| 11670 | /* Arrays indexed by above */ |
| 11671 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11672 | |
| 11673 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11674 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11675 | /* externally visible for str.strip(unicode) */ |
| 11676 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11677 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11678 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11679 | void *data; |
| 11680 | int kind; |
| 11681 | Py_ssize_t i, j, len; |
| 11682 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11684 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11685 | return NULL; |
| 11686 | |
| 11687 | kind = PyUnicode_KIND(self); |
| 11688 | data = PyUnicode_DATA(self); |
| 11689 | len = PyUnicode_GET_LENGTH(self); |
| 11690 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11691 | PyUnicode_DATA(sepobj), |
| 11692 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11693 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11694 | i = 0; |
| 11695 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | while (i < len && |
| 11697 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | i++; |
| 11699 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11700 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11701 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11702 | j = len; |
| 11703 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11704 | do { |
| 11705 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | } while (j >= i && |
| 11707 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11708 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11709 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11710 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11711 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11712 | } |
| 11713 | |
| 11714 | PyObject* |
| 11715 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11716 | { |
| 11717 | unsigned char *data; |
| 11718 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11719 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11721 | if (PyUnicode_READY(self) == -1) |
| 11722 | return NULL; |
| 11723 | |
| 11724 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11725 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11726 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11728 | if (PyUnicode_CheckExact(self)) { |
| 11729 | Py_INCREF(self); |
| 11730 | return self; |
| 11731 | } |
| 11732 | else |
| 11733 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11734 | } |
| 11735 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11736 | length = end - start; |
| 11737 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11738 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11739 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11740 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11741 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11742 | return NULL; |
| 11743 | } |
| 11744 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11745 | if (PyUnicode_IS_ASCII(self)) { |
| 11746 | kind = PyUnicode_KIND(self); |
| 11747 | data = PyUnicode_1BYTE_DATA(self); |
| 11748 | return unicode_fromascii(data + start, length); |
| 11749 | } |
| 11750 | else { |
| 11751 | kind = PyUnicode_KIND(self); |
| 11752 | data = PyUnicode_1BYTE_DATA(self); |
| 11753 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11754 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11755 | length); |
| 11756 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11757 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11758 | |
| 11759 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11760 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11761 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11762 | int kind; |
| 11763 | void *data; |
| 11764 | Py_ssize_t len, i, j; |
| 11765 | |
| 11766 | if (PyUnicode_READY(self) == -1) |
| 11767 | return NULL; |
| 11768 | |
| 11769 | kind = PyUnicode_KIND(self); |
| 11770 | data = PyUnicode_DATA(self); |
| 11771 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11772 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11773 | i = 0; |
| 11774 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11776 | i++; |
| 11777 | } |
| 11778 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11779 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11780 | j = len; |
| 11781 | if (striptype != LEFTSTRIP) { |
| 11782 | do { |
| 11783 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11784 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11785 | j++; |
| 11786 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11787 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11788 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 | } |
| 11790 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11791 | |
| 11792 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11793 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11794 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11795 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11796 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11797 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11798 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11799 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11800 | if (sep != NULL && sep != Py_None) { |
| 11801 | if (PyUnicode_Check(sep)) |
| 11802 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11803 | else { |
| 11804 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11805 | "%s arg must be None or str", |
| 11806 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11807 | return NULL; |
| 11808 | } |
| 11809 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11810 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11811 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11812 | } |
| 11813 | |
| 11814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11815 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11816 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11817 | \n\ |
| 11818 | Return a copy of the string S with leading and trailing\n\ |
| 11819 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11820 | 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] | 11821 | |
| 11822 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11823 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11824 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11825 | if (PyTuple_GET_SIZE(args) == 0) |
| 11826 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11827 | else |
| 11828 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11829 | } |
| 11830 | |
| 11831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11832 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11833 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11834 | \n\ |
| 11835 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11836 | 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] | 11837 | |
| 11838 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11839 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11840 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11841 | if (PyTuple_GET_SIZE(args) == 0) |
| 11842 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11843 | else |
| 11844 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11845 | } |
| 11846 | |
| 11847 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11848 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11849 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11850 | \n\ |
| 11851 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11852 | 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] | 11853 | |
| 11854 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11855 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11856 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11857 | if (PyTuple_GET_SIZE(args) == 0) |
| 11858 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11859 | else |
| 11860 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11861 | } |
| 11862 | |
| 11863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11865 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11866 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11867 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11868 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11869 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11870 | if (len < 1) { |
| 11871 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11872 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11873 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11875 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11876 | /* no repeat, return original string */ |
| 11877 | Py_INCREF(str); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11878 | return str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11881 | if (PyUnicode_READY(str) == -1) |
| 11882 | return NULL; |
| 11883 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11884 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11885 | PyErr_SetString(PyExc_OverflowError, |
| 11886 | "repeated string is too long"); |
| 11887 | return NULL; |
| 11888 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11889 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11890 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11891 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11892 | if (!u) |
| 11893 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11894 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11895 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11896 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11897 | const int kind = PyUnicode_KIND(str); |
| 11898 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11899 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11900 | if (kind == PyUnicode_1BYTE_KIND) |
| 11901 | memset(to, (unsigned char)fill_char, len); |
| 11902 | else { |
| 11903 | for (n = 0; n < len; ++n) |
| 11904 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11906 | } |
| 11907 | else { |
| 11908 | /* number of characters copied this far */ |
| 11909 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11910 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11911 | char *to = (char *) PyUnicode_DATA(u); |
| 11912 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11913 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11914 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11915 | n = (done <= nchars-done) ? done : nchars-done; |
| 11916 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11917 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11918 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | } |
| 11920 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11921 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11922 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11923 | } |
| 11924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11925 | PyObject * |
| 11926 | PyUnicode_Replace(PyObject *obj, |
| 11927 | PyObject *subobj, |
| 11928 | PyObject *replobj, |
| 11929 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11930 | { |
| 11931 | PyObject *self; |
| 11932 | PyObject *str1; |
| 11933 | PyObject *str2; |
| 11934 | PyObject *result; |
| 11935 | |
| 11936 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11937 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11938 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11940 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11941 | Py_DECREF(self); |
| 11942 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | } |
| 11944 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11945 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11946 | Py_DECREF(self); |
| 11947 | Py_DECREF(str1); |
| 11948 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11950 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | Py_DECREF(self); |
| 11952 | Py_DECREF(str1); |
| 11953 | Py_DECREF(str2); |
| 11954 | return result; |
| 11955 | } |
| 11956 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11957 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11958 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | \n\ |
| 11960 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11961 | old replaced by new. If the optional argument count is\n\ |
| 11962 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | |
| 11964 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11966 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11967 | PyObject *str1; |
| 11968 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11969 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | PyObject *result; |
| 11971 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11972 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11973 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11976 | str1 = PyUnicode_FromObject(str1); |
| 11977 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11978 | return NULL; |
| 11979 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11980 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11981 | Py_DECREF(str1); |
| 11982 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11984 | |
| 11985 | result = replace(self, str1, str2, maxcount); |
| 11986 | |
| 11987 | Py_DECREF(str1); |
| 11988 | Py_DECREF(str2); |
| 11989 | return result; |
| 11990 | } |
| 11991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11992 | static PyObject * |
| 11993 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11994 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11995 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | Py_ssize_t isize; |
| 11997 | Py_ssize_t osize, squote, dquote, i, o; |
| 11998 | Py_UCS4 max, quote; |
| 11999 | int ikind, okind; |
| 12000 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12003 | return NULL; |
| 12004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12005 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12006 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12007 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12008 | /* Compute length of output, quote characters, and |
| 12009 | maximum character */ |
| 12010 | osize = 2; /* quotes */ |
| 12011 | max = 127; |
| 12012 | squote = dquote = 0; |
| 12013 | ikind = PyUnicode_KIND(unicode); |
| 12014 | for (i = 0; i < isize; i++) { |
| 12015 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12016 | switch (ch) { |
| 12017 | case '\'': squote++; osize++; break; |
| 12018 | case '"': dquote++; osize++; break; |
| 12019 | case '\\': case '\t': case '\r': case '\n': |
| 12020 | osize += 2; break; |
| 12021 | default: |
| 12022 | /* Fast-path ASCII */ |
| 12023 | if (ch < ' ' || ch == 0x7f) |
| 12024 | osize += 4; /* \xHH */ |
| 12025 | else if (ch < 0x7f) |
| 12026 | osize++; |
| 12027 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12028 | osize++; |
| 12029 | max = ch > max ? ch : max; |
| 12030 | } |
| 12031 | else if (ch < 0x100) |
| 12032 | osize += 4; /* \xHH */ |
| 12033 | else if (ch < 0x10000) |
| 12034 | osize += 6; /* \uHHHH */ |
| 12035 | else |
| 12036 | osize += 10; /* \uHHHHHHHH */ |
| 12037 | } |
| 12038 | } |
| 12039 | |
| 12040 | quote = '\''; |
| 12041 | if (squote) { |
| 12042 | if (dquote) |
| 12043 | /* Both squote and dquote present. Use squote, |
| 12044 | and escape them */ |
| 12045 | osize += squote; |
| 12046 | else |
| 12047 | quote = '"'; |
| 12048 | } |
| 12049 | |
| 12050 | repr = PyUnicode_New(osize, max); |
| 12051 | if (repr == NULL) |
| 12052 | return NULL; |
| 12053 | okind = PyUnicode_KIND(repr); |
| 12054 | odata = PyUnicode_DATA(repr); |
| 12055 | |
| 12056 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12057 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12058 | |
| 12059 | for (i = 0, o = 1; i < isize; i++) { |
| 12060 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12061 | |
| 12062 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | if ((ch == quote) || (ch == '\\')) { |
| 12064 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12065 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12066 | continue; |
| 12067 | } |
| 12068 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12069 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12070 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12072 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12073 | } |
| 12074 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12075 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12076 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12077 | } |
| 12078 | else if (ch == '\r') { |
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++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12081 | } |
| 12082 | |
| 12083 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12084 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12085 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12086 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12087 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12088 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12089 | } |
| 12090 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12091 | /* Copy ASCII characters as-is */ |
| 12092 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12094 | } |
| 12095 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12096 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12097 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12098 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12099 | (categories Z* and C* except ASCII space) |
| 12100 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12102 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12103 | if (ch <= 0xff) { |
| 12104 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12106 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12107 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12108 | } |
| 12109 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12110 | else if (ch >= 0x10000) { |
| 12111 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12112 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12113 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12114 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12115 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12116 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12117 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12118 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12119 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12120 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12121 | } |
| 12122 | /* Map 16-bit characters to '\uxxxx' */ |
| 12123 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12124 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12125 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12126 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12127 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12128 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12129 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12130 | } |
| 12131 | } |
| 12132 | /* Copy characters as-is */ |
| 12133 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12134 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12135 | } |
| 12136 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12137 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12138 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12139 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12140 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12141 | } |
| 12142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12143 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12144 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | \n\ |
| 12146 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12147 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | arguments start and end are interpreted as in slice notation.\n\ |
| 12149 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12150 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12151 | |
| 12152 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12153 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12155 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12156 | Py_ssize_t start; |
| 12157 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12158 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12160 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12161 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12162 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 | if (PyUnicode_READY(self) == -1) |
| 12165 | return NULL; |
| 12166 | if (PyUnicode_READY(substring) == -1) |
| 12167 | return NULL; |
| 12168 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12169 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | |
| 12171 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12172 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12173 | if (result == -2) |
| 12174 | return NULL; |
| 12175 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12176 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | } |
| 12178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12179 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12180 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12182 | 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] | 12183 | |
| 12184 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12185 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12186 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12187 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12188 | Py_ssize_t start; |
| 12189 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12190 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12192 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12193 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12194 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | if (PyUnicode_READY(self) == -1) |
| 12197 | return NULL; |
| 12198 | if (PyUnicode_READY(substring) == -1) |
| 12199 | return NULL; |
| 12200 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12201 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | |
| 12203 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12205 | if (result == -2) |
| 12206 | return NULL; |
| 12207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | if (result < 0) { |
| 12209 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12210 | return NULL; |
| 12211 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12212 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12213 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12214 | } |
| 12215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12216 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12217 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12218 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12219 | 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] | 12220 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | |
| 12222 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12223 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12225 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12226 | Py_UCS4 fillchar = ' '; |
| 12227 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12228 | 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] | 12229 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12230 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12231 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | return NULL; |
| 12233 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12236 | return self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12237 | } |
| 12238 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12239 | return pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12240 | } |
| 12241 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12242 | PyObject * |
| 12243 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | { |
| 12245 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12247 | s = PyUnicode_FromObject(s); |
| 12248 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12249 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12250 | if (sep != NULL) { |
| 12251 | sep = PyUnicode_FromObject(sep); |
| 12252 | if (sep == NULL) { |
| 12253 | Py_DECREF(s); |
| 12254 | return NULL; |
| 12255 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | } |
| 12257 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12258 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12259 | |
| 12260 | Py_DECREF(s); |
| 12261 | Py_XDECREF(sep); |
| 12262 | return result; |
| 12263 | } |
| 12264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12265 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12266 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | \n\ |
| 12268 | Return a list of the words in S, using sep as the\n\ |
| 12269 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12270 | 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] | 12271 | whitespace string is a separator and empty strings are\n\ |
| 12272 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12273 | |
| 12274 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12275 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12276 | { |
| 12277 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12278 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12280 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | return NULL; |
| 12282 | |
| 12283 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12286 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12287 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12288 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | } |
| 12290 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12291 | PyObject * |
| 12292 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12293 | { |
| 12294 | PyObject* str_obj; |
| 12295 | PyObject* sep_obj; |
| 12296 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | int kind1, kind2, kind; |
| 12298 | void *buf1 = NULL, *buf2 = NULL; |
| 12299 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12300 | |
| 12301 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12302 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12303 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12304 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12305 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12306 | Py_DECREF(str_obj); |
| 12307 | return NULL; |
| 12308 | } |
| 12309 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12310 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12312 | kind = Py_MAX(kind1, kind2); |
| 12313 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12315 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12316 | if (!buf1) |
| 12317 | goto onError; |
| 12318 | buf2 = PyUnicode_DATA(sep_obj); |
| 12319 | if (kind2 != kind) |
| 12320 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12321 | if (!buf2) |
| 12322 | goto onError; |
| 12323 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12324 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12325 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12326 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12327 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12328 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12329 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12330 | else |
| 12331 | 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] | 12332 | break; |
| 12333 | case PyUnicode_2BYTE_KIND: |
| 12334 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12335 | break; |
| 12336 | case PyUnicode_4BYTE_KIND: |
| 12337 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12338 | break; |
| 12339 | default: |
| 12340 | assert(0); |
| 12341 | out = 0; |
| 12342 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12343 | |
| 12344 | Py_DECREF(sep_obj); |
| 12345 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12346 | if (kind1 != kind) |
| 12347 | PyMem_Free(buf1); |
| 12348 | if (kind2 != kind) |
| 12349 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12350 | |
| 12351 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12352 | onError: |
| 12353 | Py_DECREF(sep_obj); |
| 12354 | Py_DECREF(str_obj); |
| 12355 | if (kind1 != kind && buf1) |
| 12356 | PyMem_Free(buf1); |
| 12357 | if (kind2 != kind && buf2) |
| 12358 | PyMem_Free(buf2); |
| 12359 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12360 | } |
| 12361 | |
| 12362 | |
| 12363 | PyObject * |
| 12364 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12365 | { |
| 12366 | PyObject* str_obj; |
| 12367 | PyObject* sep_obj; |
| 12368 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | int kind1, kind2, kind; |
| 12370 | void *buf1 = NULL, *buf2 = NULL; |
| 12371 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12372 | |
| 12373 | str_obj = PyUnicode_FromObject(str_in); |
| 12374 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12375 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12376 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12377 | if (!sep_obj) { |
| 12378 | Py_DECREF(str_obj); |
| 12379 | return NULL; |
| 12380 | } |
| 12381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | kind1 = PyUnicode_KIND(str_in); |
| 12383 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12384 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12385 | buf1 = PyUnicode_DATA(str_in); |
| 12386 | if (kind1 != kind) |
| 12387 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12388 | if (!buf1) |
| 12389 | goto onError; |
| 12390 | buf2 = PyUnicode_DATA(sep_obj); |
| 12391 | if (kind2 != kind) |
| 12392 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12393 | if (!buf2) |
| 12394 | goto onError; |
| 12395 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12396 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12397 | |
| 12398 | switch(PyUnicode_KIND(str_in)) { |
| 12399 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12400 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12401 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12402 | else |
| 12403 | 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] | 12404 | break; |
| 12405 | case PyUnicode_2BYTE_KIND: |
| 12406 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12407 | break; |
| 12408 | case PyUnicode_4BYTE_KIND: |
| 12409 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12410 | break; |
| 12411 | default: |
| 12412 | assert(0); |
| 12413 | out = 0; |
| 12414 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12415 | |
| 12416 | Py_DECREF(sep_obj); |
| 12417 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12418 | if (kind1 != kind) |
| 12419 | PyMem_Free(buf1); |
| 12420 | if (kind2 != kind) |
| 12421 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12422 | |
| 12423 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12424 | onError: |
| 12425 | Py_DECREF(sep_obj); |
| 12426 | Py_DECREF(str_obj); |
| 12427 | if (kind1 != kind && buf1) |
| 12428 | PyMem_Free(buf1); |
| 12429 | if (kind2 != kind && buf2) |
| 12430 | PyMem_Free(buf2); |
| 12431 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12432 | } |
| 12433 | |
| 12434 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12435 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12436 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12437 | 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] | 12438 | 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] | 12439 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12440 | |
| 12441 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12442 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12443 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12444 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12445 | } |
| 12446 | |
| 12447 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12448 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12449 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12450 | 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] | 12451 | 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] | 12452 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12453 | |
| 12454 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12455 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12456 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12457 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12458 | } |
| 12459 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12460 | PyObject * |
| 12461 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12462 | { |
| 12463 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12464 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12465 | s = PyUnicode_FromObject(s); |
| 12466 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12467 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12468 | if (sep != NULL) { |
| 12469 | sep = PyUnicode_FromObject(sep); |
| 12470 | if (sep == NULL) { |
| 12471 | Py_DECREF(s); |
| 12472 | return NULL; |
| 12473 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12474 | } |
| 12475 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12476 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12477 | |
| 12478 | Py_DECREF(s); |
| 12479 | Py_XDECREF(sep); |
| 12480 | return result; |
| 12481 | } |
| 12482 | |
| 12483 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12484 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12485 | \n\ |
| 12486 | Return a list of the words in S, using sep as the\n\ |
| 12487 | delimiter string, starting at the end of the string and\n\ |
| 12488 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12489 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12490 | is a separator."); |
| 12491 | |
| 12492 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12493 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12494 | { |
| 12495 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12496 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12497 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12498 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12499 | return NULL; |
| 12500 | |
| 12501 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12502 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12503 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12504 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12505 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12506 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12507 | } |
| 12508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12509 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12510 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12511 | \n\ |
| 12512 | 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] | 12513 | 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] | 12514 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | |
| 12516 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12517 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12519 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12520 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12521 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12522 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12523 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12524 | return NULL; |
| 12525 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12526 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12527 | } |
| 12528 | |
| 12529 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12530 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12532 | if (PyUnicode_CheckExact(self)) { |
| 12533 | Py_INCREF(self); |
| 12534 | return self; |
| 12535 | } else |
| 12536 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12537 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12538 | } |
| 12539 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12540 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12541 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12542 | \n\ |
| 12543 | 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] | 12544 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12545 | |
| 12546 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12547 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12548 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | return fixup(self, fixswapcase); |
| 12550 | } |
| 12551 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12552 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12553 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12554 | \n\ |
| 12555 | Return a translation table usable for str.translate().\n\ |
| 12556 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12557 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12558 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12559 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12560 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12561 | character at the same position in y. If there is a third argument, it\n\ |
| 12562 | must be a string, whose characters will be mapped to None in the result."); |
| 12563 | |
| 12564 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12565 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12566 | { |
| 12567 | PyObject *x, *y = NULL, *z = NULL; |
| 12568 | PyObject *new = NULL, *key, *value; |
| 12569 | Py_ssize_t i = 0; |
| 12570 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12571 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12572 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12573 | return NULL; |
| 12574 | new = PyDict_New(); |
| 12575 | if (!new) |
| 12576 | return NULL; |
| 12577 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12578 | int x_kind, y_kind, z_kind; |
| 12579 | void *x_data, *y_data, *z_data; |
| 12580 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12581 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12582 | if (!PyUnicode_Check(x)) { |
| 12583 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12584 | "be a string if there is a second argument"); |
| 12585 | goto err; |
| 12586 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12587 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12588 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12589 | "arguments must have equal length"); |
| 12590 | goto err; |
| 12591 | } |
| 12592 | /* 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] | 12593 | x_kind = PyUnicode_KIND(x); |
| 12594 | y_kind = PyUnicode_KIND(y); |
| 12595 | x_data = PyUnicode_DATA(x); |
| 12596 | y_data = PyUnicode_DATA(y); |
| 12597 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12598 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12599 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12600 | if (!key || !value) |
| 12601 | goto err; |
| 12602 | res = PyDict_SetItem(new, key, value); |
| 12603 | Py_DECREF(key); |
| 12604 | Py_DECREF(value); |
| 12605 | if (res < 0) |
| 12606 | goto err; |
| 12607 | } |
| 12608 | /* create entries for deleting chars in z */ |
| 12609 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12610 | z_kind = PyUnicode_KIND(z); |
| 12611 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12612 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12613 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12614 | if (!key) |
| 12615 | goto err; |
| 12616 | res = PyDict_SetItem(new, key, Py_None); |
| 12617 | Py_DECREF(key); |
| 12618 | if (res < 0) |
| 12619 | goto err; |
| 12620 | } |
| 12621 | } |
| 12622 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12623 | int kind; |
| 12624 | void *data; |
| 12625 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12626 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12627 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12628 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12629 | "to maketrans it must be a dict"); |
| 12630 | goto err; |
| 12631 | } |
| 12632 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12633 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12634 | if (PyUnicode_Check(key)) { |
| 12635 | /* convert string keys to integer keys */ |
| 12636 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12637 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12638 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12639 | "table must be of length 1"); |
| 12640 | goto err; |
| 12641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12642 | kind = PyUnicode_KIND(key); |
| 12643 | data = PyUnicode_DATA(key); |
| 12644 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12645 | if (!newkey) |
| 12646 | goto err; |
| 12647 | res = PyDict_SetItem(new, newkey, value); |
| 12648 | Py_DECREF(newkey); |
| 12649 | if (res < 0) |
| 12650 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12651 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12652 | /* just keep integer keys */ |
| 12653 | if (PyDict_SetItem(new, key, value) < 0) |
| 12654 | goto err; |
| 12655 | } else { |
| 12656 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12657 | "be strings or integers"); |
| 12658 | goto err; |
| 12659 | } |
| 12660 | } |
| 12661 | } |
| 12662 | return new; |
| 12663 | err: |
| 12664 | Py_DECREF(new); |
| 12665 | return NULL; |
| 12666 | } |
| 12667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12668 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12669 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | \n\ |
| 12671 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12672 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12673 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12674 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12675 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12676 | |
| 12677 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12678 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12680 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | } |
| 12682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12683 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12684 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12686 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | |
| 12688 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12689 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | return fixup(self, fixupper); |
| 12692 | } |
| 12693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12694 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12695 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12697 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12698 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | |
| 12700 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12701 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12702 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12703 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12704 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12705 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12706 | int kind; |
| 12707 | void *data; |
| 12708 | Py_UCS4 chr; |
| 12709 | |
| 12710 | if (PyUnicode_READY(self) == -1) |
| 12711 | return NULL; |
| 12712 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12713 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12714 | return NULL; |
| 12715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12716 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12717 | if (PyUnicode_CheckExact(self)) { |
| 12718 | Py_INCREF(self); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12719 | return self; |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12720 | } |
| 12721 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12722 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | } |
| 12724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12725 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12726 | |
| 12727 | u = pad(self, fill, 0, '0'); |
| 12728 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12729 | if (u == NULL) |
| 12730 | return NULL; |
| 12731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12732 | kind = PyUnicode_KIND(u); |
| 12733 | data = PyUnicode_DATA(u); |
| 12734 | chr = PyUnicode_READ(kind, data, fill); |
| 12735 | |
| 12736 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12739 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12740 | } |
| 12741 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12742 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12743 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | |
| 12746 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12747 | static PyObject * |
| 12748 | unicode__decimal2ascii(PyObject *self) |
| 12749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12750 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12751 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12752 | #endif |
| 12753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12754 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12756 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12757 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12758 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12759 | With optional end, stop comparing S at that position.\n\ |
| 12760 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12761 | |
| 12762 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12763 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12764 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12765 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12766 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12767 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12768 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12769 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12770 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12771 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12772 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | if (PyTuple_Check(subobj)) { |
| 12775 | Py_ssize_t i; |
| 12776 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12777 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12778 | if (substring == NULL) |
| 12779 | return NULL; |
| 12780 | result = tailmatch(self, substring, start, end, -1); |
| 12781 | Py_DECREF(substring); |
| 12782 | if (result) { |
| 12783 | Py_RETURN_TRUE; |
| 12784 | } |
| 12785 | } |
| 12786 | /* nothing matched */ |
| 12787 | Py_RETURN_FALSE; |
| 12788 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12789 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12790 | if (substring == NULL) { |
| 12791 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12792 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12793 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12794 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12795 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12796 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12797 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12799 | } |
| 12800 | |
| 12801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12802 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12803 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12804 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12805 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12806 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12807 | With optional end, stop comparing S at that position.\n\ |
| 12808 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12809 | |
| 12810 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12811 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12812 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12813 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12814 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12815 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12816 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12817 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12818 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12819 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12820 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12821 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12822 | if (PyTuple_Check(subobj)) { |
| 12823 | Py_ssize_t i; |
| 12824 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12825 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12826 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12827 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12828 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12829 | result = tailmatch(self, substring, start, end, +1); |
| 12830 | Py_DECREF(substring); |
| 12831 | if (result) { |
| 12832 | Py_RETURN_TRUE; |
| 12833 | } |
| 12834 | } |
| 12835 | Py_RETURN_FALSE; |
| 12836 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12837 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12838 | if (substring == NULL) { |
| 12839 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12840 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12841 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12842 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12843 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12844 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12846 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | } |
| 12848 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12849 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12850 | |
| 12851 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12852 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12853 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12854 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12855 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12856 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12857 | PyDoc_STRVAR(format_map__doc__, |
| 12858 | "S.format_map(mapping) -> str\n\ |
| 12859 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12860 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12861 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12862 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12863 | static PyObject * |
| 12864 | unicode__format__(PyObject* self, PyObject* args) |
| 12865 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12866 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12867 | |
| 12868 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12869 | return NULL; |
| 12870 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12871 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12872 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12873 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12874 | } |
| 12875 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12876 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12877 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12878 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12879 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12880 | |
| 12881 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12882 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12883 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12884 | Py_ssize_t size; |
| 12885 | |
| 12886 | /* If it's a compact object, account for base structure + |
| 12887 | character data. */ |
| 12888 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12889 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12890 | else if (PyUnicode_IS_COMPACT(v)) |
| 12891 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12892 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12893 | else { |
| 12894 | /* If it is a two-block object, account for base object, and |
| 12895 | for character block if present. */ |
| 12896 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12897 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12898 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12899 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12900 | } |
| 12901 | /* 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] | 12902 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12903 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12905 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12906 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12907 | |
| 12908 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12909 | } |
| 12910 | |
| 12911 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12912 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12913 | |
| 12914 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12915 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12916 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12917 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12918 | if (!copy) |
| 12919 | return NULL; |
| 12920 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12921 | } |
| 12922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12923 | static PyMethodDef unicode_methods[] = { |
| 12924 | |
| 12925 | /* Order is according to common usage: often used methods should |
| 12926 | appear first, since lookup is done sequentially. */ |
| 12927 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12928 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12929 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12930 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12931 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12932 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12933 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12934 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12935 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12936 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12937 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12938 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12939 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12940 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12941 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12942 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12943 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12944 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12945 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12946 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12947 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12948 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12949 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12950 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12951 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12952 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12953 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12954 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12955 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12956 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12957 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12958 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12959 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12960 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12961 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12962 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12963 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12964 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12965 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12966 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12967 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12968 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12969 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12970 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12971 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12972 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12973 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12974 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12975 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12976 | #endif |
| 12977 | |
| 12978 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12979 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12980 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12981 | #endif |
| 12982 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12983 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12984 | {NULL, NULL} |
| 12985 | }; |
| 12986 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12987 | static PyObject * |
| 12988 | unicode_mod(PyObject *v, PyObject *w) |
| 12989 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12990 | if (!PyUnicode_Check(v)) |
| 12991 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12993 | } |
| 12994 | |
| 12995 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12996 | 0, /*nb_add*/ |
| 12997 | 0, /*nb_subtract*/ |
| 12998 | 0, /*nb_multiply*/ |
| 12999 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13000 | }; |
| 13001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13002 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13003 | (lenfunc) unicode_length, /* sq_length */ |
| 13004 | PyUnicode_Concat, /* sq_concat */ |
| 13005 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13006 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13007 | 0, /* sq_slice */ |
| 13008 | 0, /* sq_ass_item */ |
| 13009 | 0, /* sq_ass_slice */ |
| 13010 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13011 | }; |
| 13012 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13013 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13014 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13015 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13016 | if (PyUnicode_READY(self) == -1) |
| 13017 | return NULL; |
| 13018 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13019 | if (PyIndex_Check(item)) { |
| 13020 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13021 | if (i == -1 && PyErr_Occurred()) |
| 13022 | return NULL; |
| 13023 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13024 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13025 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13026 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13027 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13028 | PyObject *result; |
| 13029 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13030 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13031 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13033 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13034 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13035 | return NULL; |
| 13036 | } |
| 13037 | |
| 13038 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13039 | return PyUnicode_New(0, 0); |
| 13040 | } else if (start == 0 && step == 1 && |
| 13041 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13042 | PyUnicode_CheckExact(self)) { |
| 13043 | Py_INCREF(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13044 | return self; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13045 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13046 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13047 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13048 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13049 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13050 | src_kind = PyUnicode_KIND(self); |
| 13051 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13052 | if (!PyUnicode_IS_ASCII(self)) { |
| 13053 | kind_limit = kind_maxchar_limit(src_kind); |
| 13054 | max_char = 0; |
| 13055 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13056 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13057 | if (ch > max_char) { |
| 13058 | max_char = ch; |
| 13059 | if (max_char >= kind_limit) |
| 13060 | break; |
| 13061 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13062 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13063 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13064 | else |
| 13065 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13066 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13067 | if (result == NULL) |
| 13068 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13069 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13070 | dest_data = PyUnicode_DATA(result); |
| 13071 | |
| 13072 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13073 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13074 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13075 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13076 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13077 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13078 | } else { |
| 13079 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13080 | return NULL; |
| 13081 | } |
| 13082 | } |
| 13083 | |
| 13084 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13085 | (lenfunc)unicode_length, /* mp_length */ |
| 13086 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13087 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13088 | }; |
| 13089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | /* Helpers for PyUnicode_Format() */ |
| 13092 | |
| 13093 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13094 | 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] | 13095 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13096 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13097 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13098 | (*p_argidx)++; |
| 13099 | if (arglen < 0) |
| 13100 | return args; |
| 13101 | else |
| 13102 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | } |
| 13104 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13105 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | return NULL; |
| 13107 | } |
| 13108 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13109 | /* 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] | 13110 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13111 | static PyObject * |
| 13112 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13113 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13114 | char *p; |
| 13115 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13116 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13118 | x = PyFloat_AsDouble(v); |
| 13119 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13120 | return NULL; |
| 13121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13122 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13123 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13124 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13125 | p = PyOS_double_to_string(x, type, prec, |
| 13126 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13127 | if (p == NULL) |
| 13128 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13129 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13130 | PyMem_Free(p); |
| 13131 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13132 | } |
| 13133 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13134 | static PyObject* |
| 13135 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13136 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13137 | char *buf; |
| 13138 | int len; |
| 13139 | PyObject *str; /* temporary string object. */ |
| 13140 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13141 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13142 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 13143 | if (!str) |
| 13144 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13145 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13146 | Py_DECREF(str); |
| 13147 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13148 | } |
| 13149 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13150 | static Py_UCS4 |
| 13151 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13152 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13153 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13154 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13155 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13156 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13157 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13158 | goto onError; |
| 13159 | } |
| 13160 | else { |
| 13161 | /* Integer input truncated to a character */ |
| 13162 | long x; |
| 13163 | x = PyLong_AsLong(v); |
| 13164 | if (x == -1 && PyErr_Occurred()) |
| 13165 | goto onError; |
| 13166 | |
| 13167 | if (x < 0 || x > 0x10ffff) { |
| 13168 | PyErr_SetString(PyExc_OverflowError, |
| 13169 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13170 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13171 | } |
| 13172 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13173 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13174 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13175 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13177 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13178 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13179 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13180 | } |
| 13181 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13182 | static int |
| 13183 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 13184 | { |
| 13185 | int r; |
| 13186 | assert(count > 0); |
| 13187 | assert(PyUnicode_Check(obj)); |
| 13188 | if (count > 5) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13189 | PyObject *repeated = unicode_repeat(obj, count); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13190 | if (repeated == NULL) |
| 13191 | return -1; |
| 13192 | r = _PyAccu_Accumulate(acc, repeated); |
| 13193 | Py_DECREF(repeated); |
| 13194 | return r; |
| 13195 | } |
| 13196 | else { |
| 13197 | do { |
| 13198 | if (_PyAccu_Accumulate(acc, obj)) |
| 13199 | return -1; |
| 13200 | } while (--count); |
| 13201 | return 0; |
| 13202 | } |
| 13203 | } |
| 13204 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13205 | PyObject * |
| 13206 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13207 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13208 | void *fmt; |
| 13209 | int fmtkind; |
| 13210 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13211 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13212 | int r; |
| 13213 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13215 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13216 | PyObject *temp = NULL; |
| 13217 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13218 | PyObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13219 | _PyAccu acc; |
| 13220 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 13221 | |
| 13222 | if (!plus && !(plus = get_latin1_char('+'))) |
| 13223 | return NULL; |
| 13224 | if (!minus && !(minus = get_latin1_char('-'))) |
| 13225 | return NULL; |
| 13226 | if (!blank && !(blank = get_latin1_char(' '))) |
| 13227 | return NULL; |
| 13228 | if (!zero && !(zero = get_latin1_char('0'))) |
| 13229 | return NULL; |
| 13230 | if (!percent && !(percent = get_latin1_char('%'))) |
| 13231 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13233 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13234 | PyErr_BadInternalCall(); |
| 13235 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13236 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13237 | uformat = PyUnicode_FromObject(format); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13238 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13239 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13240 | if (_PyAccu_Init(&acc)) |
| 13241 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13242 | fmt = PyUnicode_DATA(uformat); |
| 13243 | fmtkind = PyUnicode_KIND(uformat); |
| 13244 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13245 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13247 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13248 | arglen = PyTuple_Size(args); |
| 13249 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13250 | } |
| 13251 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13252 | arglen = -1; |
| 13253 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13254 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13255 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13256 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13257 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13258 | |
| 13259 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13260 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13261 | PyObject *nonfmt; |
| 13262 | Py_ssize_t nonfmtpos; |
| 13263 | nonfmtpos = fmtpos++; |
| 13264 | while (fmtcnt >= 0 && |
| 13265 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13266 | fmtpos++; |
| 13267 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13268 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13269 | nonfmt = PyUnicode_Substring(uformat, nonfmtpos, fmtpos); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13270 | if (nonfmt == NULL) |
| 13271 | goto onError; |
| 13272 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 13273 | Py_DECREF(nonfmt); |
| 13274 | if (r) |
| 13275 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13276 | } |
| 13277 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13278 | /* Got a format specifier */ |
| 13279 | int flags = 0; |
| 13280 | Py_ssize_t width = -1; |
| 13281 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13282 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13283 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13284 | int isnumok; |
| 13285 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13286 | void *pbuf = NULL; |
| 13287 | Py_ssize_t pindex, len; |
| 13288 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13290 | fmtpos++; |
| 13291 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 13292 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13293 | Py_ssize_t keylen; |
| 13294 | PyObject *key; |
| 13295 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13296 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13297 | if (dict == NULL) { |
| 13298 | PyErr_SetString(PyExc_TypeError, |
| 13299 | "format requires a mapping"); |
| 13300 | goto onError; |
| 13301 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13302 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13303 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13304 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13305 | /* Skip over balanced parentheses */ |
| 13306 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13307 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13308 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13309 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13310 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13311 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13312 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13313 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13314 | if (fmtcnt < 0 || pcount > 0) { |
| 13315 | PyErr_SetString(PyExc_ValueError, |
| 13316 | "incomplete format key"); |
| 13317 | goto onError; |
| 13318 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13319 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13320 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13321 | if (key == NULL) |
| 13322 | goto onError; |
| 13323 | if (args_owned) { |
| 13324 | Py_DECREF(args); |
| 13325 | args_owned = 0; |
| 13326 | } |
| 13327 | args = PyObject_GetItem(dict, key); |
| 13328 | Py_DECREF(key); |
| 13329 | if (args == NULL) { |
| 13330 | goto onError; |
| 13331 | } |
| 13332 | args_owned = 1; |
| 13333 | arglen = -1; |
| 13334 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13335 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13336 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13337 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13338 | case '-': flags |= F_LJUST; continue; |
| 13339 | case '+': flags |= F_SIGN; continue; |
| 13340 | case ' ': flags |= F_BLANK; continue; |
| 13341 | case '#': flags |= F_ALT; continue; |
| 13342 | case '0': flags |= F_ZERO; continue; |
| 13343 | } |
| 13344 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13345 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13346 | if (c == '*') { |
| 13347 | v = getnextarg(args, arglen, &argidx); |
| 13348 | if (v == NULL) |
| 13349 | goto onError; |
| 13350 | if (!PyLong_Check(v)) { |
| 13351 | PyErr_SetString(PyExc_TypeError, |
| 13352 | "* wants int"); |
| 13353 | goto onError; |
| 13354 | } |
| 13355 | width = PyLong_AsLong(v); |
| 13356 | if (width == -1 && PyErr_Occurred()) |
| 13357 | goto onError; |
| 13358 | if (width < 0) { |
| 13359 | flags |= F_LJUST; |
| 13360 | width = -width; |
| 13361 | } |
| 13362 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13363 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13364 | } |
| 13365 | else if (c >= '0' && c <= '9') { |
| 13366 | width = c - '0'; |
| 13367 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13368 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13369 | if (c < '0' || c > '9') |
| 13370 | break; |
| 13371 | if ((width*10) / 10 != width) { |
| 13372 | PyErr_SetString(PyExc_ValueError, |
| 13373 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13374 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13375 | } |
| 13376 | width = width*10 + (c - '0'); |
| 13377 | } |
| 13378 | } |
| 13379 | if (c == '.') { |
| 13380 | prec = 0; |
| 13381 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13382 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13383 | if (c == '*') { |
| 13384 | v = getnextarg(args, arglen, &argidx); |
| 13385 | if (v == NULL) |
| 13386 | goto onError; |
| 13387 | if (!PyLong_Check(v)) { |
| 13388 | PyErr_SetString(PyExc_TypeError, |
| 13389 | "* wants int"); |
| 13390 | goto onError; |
| 13391 | } |
| 13392 | prec = PyLong_AsLong(v); |
| 13393 | if (prec == -1 && PyErr_Occurred()) |
| 13394 | goto onError; |
| 13395 | if (prec < 0) |
| 13396 | prec = 0; |
| 13397 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13398 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13399 | } |
| 13400 | else if (c >= '0' && c <= '9') { |
| 13401 | prec = c - '0'; |
| 13402 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13403 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13404 | if (c < '0' || c > '9') |
| 13405 | break; |
| 13406 | if ((prec*10) / 10 != prec) { |
| 13407 | PyErr_SetString(PyExc_ValueError, |
| 13408 | "prec too big"); |
| 13409 | goto onError; |
| 13410 | } |
| 13411 | prec = prec*10 + (c - '0'); |
| 13412 | } |
| 13413 | } |
| 13414 | } /* prec */ |
| 13415 | if (fmtcnt >= 0) { |
| 13416 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13417 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13418 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13419 | } |
| 13420 | } |
| 13421 | if (fmtcnt < 0) { |
| 13422 | PyErr_SetString(PyExc_ValueError, |
| 13423 | "incomplete format"); |
| 13424 | goto onError; |
| 13425 | } |
| 13426 | if (c != '%') { |
| 13427 | v = getnextarg(args, arglen, &argidx); |
| 13428 | if (v == NULL) |
| 13429 | goto onError; |
| 13430 | } |
| 13431 | sign = 0; |
| 13432 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13433 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13434 | switch (c) { |
| 13435 | |
| 13436 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13437 | _PyAccu_Accumulate(&acc, percent); |
| 13438 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13439 | |
| 13440 | case 's': |
| 13441 | case 'r': |
| 13442 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13443 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13444 | temp = v; |
| 13445 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13446 | } |
| 13447 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | if (c == 's') |
| 13449 | temp = PyObject_Str(v); |
| 13450 | else if (c == 'r') |
| 13451 | temp = PyObject_Repr(v); |
| 13452 | else |
| 13453 | temp = PyObject_ASCII(v); |
| 13454 | if (temp == NULL) |
| 13455 | goto onError; |
| 13456 | if (PyUnicode_Check(temp)) |
| 13457 | /* nothing to do */; |
| 13458 | else { |
| 13459 | Py_DECREF(temp); |
| 13460 | PyErr_SetString(PyExc_TypeError, |
| 13461 | "%s argument has non-string str()"); |
| 13462 | goto onError; |
| 13463 | } |
| 13464 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13465 | if (PyUnicode_READY(temp) == -1) { |
| 13466 | Py_CLEAR(temp); |
| 13467 | goto onError; |
| 13468 | } |
| 13469 | pbuf = PyUnicode_DATA(temp); |
| 13470 | kind = PyUnicode_KIND(temp); |
| 13471 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13472 | if (prec >= 0 && len > prec) |
| 13473 | len = prec; |
| 13474 | break; |
| 13475 | |
| 13476 | case 'i': |
| 13477 | case 'd': |
| 13478 | case 'u': |
| 13479 | case 'o': |
| 13480 | case 'x': |
| 13481 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13482 | isnumok = 0; |
| 13483 | if (PyNumber_Check(v)) { |
| 13484 | PyObject *iobj=NULL; |
| 13485 | |
| 13486 | if (PyLong_Check(v)) { |
| 13487 | iobj = v; |
| 13488 | Py_INCREF(iobj); |
| 13489 | } |
| 13490 | else { |
| 13491 | iobj = PyNumber_Long(v); |
| 13492 | } |
| 13493 | if (iobj!=NULL) { |
| 13494 | if (PyLong_Check(iobj)) { |
| 13495 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13496 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | Py_DECREF(iobj); |
| 13498 | if (!temp) |
| 13499 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13500 | if (PyUnicode_READY(temp) == -1) { |
| 13501 | Py_CLEAR(temp); |
| 13502 | goto onError; |
| 13503 | } |
| 13504 | pbuf = PyUnicode_DATA(temp); |
| 13505 | kind = PyUnicode_KIND(temp); |
| 13506 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13507 | sign = 1; |
| 13508 | } |
| 13509 | else { |
| 13510 | Py_DECREF(iobj); |
| 13511 | } |
| 13512 | } |
| 13513 | } |
| 13514 | if (!isnumok) { |
| 13515 | PyErr_Format(PyExc_TypeError, |
| 13516 | "%%%c format: a number is required, " |
| 13517 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13518 | goto onError; |
| 13519 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13520 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13521 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13522 | fillobj = zero; |
| 13523 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13524 | break; |
| 13525 | |
| 13526 | case 'e': |
| 13527 | case 'E': |
| 13528 | case 'f': |
| 13529 | case 'F': |
| 13530 | case 'g': |
| 13531 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13532 | temp = formatfloat(v, flags, prec, c); |
| 13533 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13534 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13535 | if (PyUnicode_READY(temp) == -1) { |
| 13536 | Py_CLEAR(temp); |
| 13537 | goto onError; |
| 13538 | } |
| 13539 | pbuf = PyUnicode_DATA(temp); |
| 13540 | kind = PyUnicode_KIND(temp); |
| 13541 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13542 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13543 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13544 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13545 | fillobj = zero; |
| 13546 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13547 | break; |
| 13548 | |
| 13549 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13550 | { |
| 13551 | Py_UCS4 ch = formatchar(v); |
| 13552 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13553 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13554 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13555 | if (temp == NULL) |
| 13556 | goto onError; |
| 13557 | pbuf = PyUnicode_DATA(temp); |
| 13558 | kind = PyUnicode_KIND(temp); |
| 13559 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13560 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13561 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13562 | |
| 13563 | default: |
| 13564 | PyErr_Format(PyExc_ValueError, |
| 13565 | "unsupported format character '%c' (0x%x) " |
| 13566 | "at index %zd", |
| 13567 | (31<=c && c<=126) ? (char)c : '?', |
| 13568 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13569 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13570 | goto onError; |
| 13571 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13572 | /* pbuf is initialized here. */ |
| 13573 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13574 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13575 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13576 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13577 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13578 | pindex++; |
| 13579 | } |
| 13580 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13581 | signobj = plus; |
| 13582 | len--; |
| 13583 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13584 | } |
| 13585 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13586 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13587 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13588 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13589 | else |
| 13590 | sign = 0; |
| 13591 | } |
| 13592 | if (width < len) |
| 13593 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13594 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13595 | if (fill != ' ') { |
| 13596 | assert(signobj != NULL); |
| 13597 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13598 | goto onError; |
| 13599 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13600 | if (width > len) |
| 13601 | width--; |
| 13602 | } |
| 13603 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13604 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13605 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13606 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13607 | second = get_latin1_char( |
| 13608 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13609 | pindex += 2; |
| 13610 | if (second == NULL || |
| 13611 | _PyAccu_Accumulate(&acc, zero) || |
| 13612 | _PyAccu_Accumulate(&acc, second)) |
| 13613 | goto onError; |
| 13614 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13615 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13616 | width -= 2; |
| 13617 | if (width < 0) |
| 13618 | width = 0; |
| 13619 | len -= 2; |
| 13620 | } |
| 13621 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13622 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13623 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13624 | goto onError; |
| 13625 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13626 | } |
| 13627 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13628 | if (sign) { |
| 13629 | assert(signobj != NULL); |
| 13630 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13631 | goto onError; |
| 13632 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13633 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13634 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13635 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13636 | second = get_latin1_char( |
| 13637 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13638 | pindex += 2; |
| 13639 | if (second == NULL || |
| 13640 | _PyAccu_Accumulate(&acc, zero) || |
| 13641 | _PyAccu_Accumulate(&acc, second)) |
| 13642 | goto onError; |
| 13643 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13644 | } |
| 13645 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13646 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13647 | if (temp != NULL) { |
| 13648 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13649 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13650 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13651 | else { |
| 13652 | const char *p = (const char *) pbuf; |
| 13653 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13654 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13655 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13656 | } |
| 13657 | if (v == NULL) |
| 13658 | goto onError; |
| 13659 | r = _PyAccu_Accumulate(&acc, v); |
| 13660 | Py_DECREF(v); |
| 13661 | if (r) |
| 13662 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13663 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13664 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13665 | if (dict && (argidx < arglen) && c != '%') { |
| 13666 | PyErr_SetString(PyExc_TypeError, |
| 13667 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13668 | goto onError; |
| 13669 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13670 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13671 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13672 | } /* until end */ |
| 13673 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13674 | PyErr_SetString(PyExc_TypeError, |
| 13675 | "not all arguments converted during string formatting"); |
| 13676 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13677 | } |
| 13678 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13679 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13680 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13681 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13682 | } |
| 13683 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13684 | Py_XDECREF(temp); |
| 13685 | Py_XDECREF(second); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13686 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13687 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13688 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13689 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13690 | Py_XDECREF(temp); |
| 13691 | Py_XDECREF(second); |
| 13692 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13693 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13694 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13695 | } |
| 13696 | return NULL; |
| 13697 | } |
| 13698 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13699 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13700 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13701 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13702 | static PyObject * |
| 13703 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13704 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13705 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13706 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13707 | char *encoding = NULL; |
| 13708 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13709 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13710 | if (type != &PyUnicode_Type) |
| 13711 | return unicode_subtype_new(type, args, kwds); |
| 13712 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13713 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13714 | return NULL; |
| 13715 | if (x == NULL) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13716 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13717 | if (encoding == NULL && errors == NULL) |
| 13718 | return PyObject_Str(x); |
| 13719 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13720 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13721 | } |
| 13722 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13723 | static PyObject * |
| 13724 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13725 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13726 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13727 | Py_ssize_t length, char_size; |
| 13728 | int share_wstr, share_utf8; |
| 13729 | unsigned int kind; |
| 13730 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13731 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13732 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13733 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13734 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13735 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13736 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13737 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13738 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13739 | return NULL; |
| 13740 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13741 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13742 | if (self == NULL) { |
| 13743 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13744 | return NULL; |
| 13745 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13746 | kind = PyUnicode_KIND(unicode); |
| 13747 | length = PyUnicode_GET_LENGTH(unicode); |
| 13748 | |
| 13749 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13750 | #ifdef Py_DEBUG |
| 13751 | _PyUnicode_HASH(self) = -1; |
| 13752 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13753 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13754 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13755 | _PyUnicode_STATE(self).interned = 0; |
| 13756 | _PyUnicode_STATE(self).kind = kind; |
| 13757 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13758 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13759 | _PyUnicode_STATE(self).ready = 1; |
| 13760 | _PyUnicode_WSTR(self) = NULL; |
| 13761 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13762 | _PyUnicode_UTF8(self) = NULL; |
| 13763 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13764 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13765 | |
| 13766 | share_utf8 = 0; |
| 13767 | share_wstr = 0; |
| 13768 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13769 | char_size = 1; |
| 13770 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13771 | share_utf8 = 1; |
| 13772 | } |
| 13773 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13774 | char_size = 2; |
| 13775 | if (sizeof(wchar_t) == 2) |
| 13776 | share_wstr = 1; |
| 13777 | } |
| 13778 | else { |
| 13779 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13780 | char_size = 4; |
| 13781 | if (sizeof(wchar_t) == 4) |
| 13782 | share_wstr = 1; |
| 13783 | } |
| 13784 | |
| 13785 | /* Ensure we won't overflow the length. */ |
| 13786 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13787 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13788 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13789 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13790 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13791 | if (data == NULL) { |
| 13792 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13793 | goto onError; |
| 13794 | } |
| 13795 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13796 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13797 | if (share_utf8) { |
| 13798 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13799 | _PyUnicode_UTF8(self) = data; |
| 13800 | } |
| 13801 | if (share_wstr) { |
| 13802 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13803 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13804 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13805 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13806 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13807 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13808 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13809 | #ifdef Py_DEBUG |
| 13810 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13811 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 13812 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13813 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13814 | |
| 13815 | onError: |
| 13816 | Py_DECREF(unicode); |
| 13817 | Py_DECREF(self); |
| 13818 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13819 | } |
| 13820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13821 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13822 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13823 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13824 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13825 | encoding defaults to the current default string encoding.\n\ |
| 13826 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13827 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13828 | static PyObject *unicode_iter(PyObject *seq); |
| 13829 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13830 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13831 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13832 | "str", /* tp_name */ |
| 13833 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13834 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13835 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13836 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13837 | 0, /* tp_print */ |
| 13838 | 0, /* tp_getattr */ |
| 13839 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13840 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13841 | unicode_repr, /* tp_repr */ |
| 13842 | &unicode_as_number, /* tp_as_number */ |
| 13843 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13844 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13845 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13846 | 0, /* tp_call*/ |
| 13847 | (reprfunc) unicode_str, /* tp_str */ |
| 13848 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13849 | 0, /* tp_setattro */ |
| 13850 | 0, /* tp_as_buffer */ |
| 13851 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13852 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13853 | unicode_doc, /* tp_doc */ |
| 13854 | 0, /* tp_traverse */ |
| 13855 | 0, /* tp_clear */ |
| 13856 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13857 | 0, /* tp_weaklistoffset */ |
| 13858 | unicode_iter, /* tp_iter */ |
| 13859 | 0, /* tp_iternext */ |
| 13860 | unicode_methods, /* tp_methods */ |
| 13861 | 0, /* tp_members */ |
| 13862 | 0, /* tp_getset */ |
| 13863 | &PyBaseObject_Type, /* tp_base */ |
| 13864 | 0, /* tp_dict */ |
| 13865 | 0, /* tp_descr_get */ |
| 13866 | 0, /* tp_descr_set */ |
| 13867 | 0, /* tp_dictoffset */ |
| 13868 | 0, /* tp_init */ |
| 13869 | 0, /* tp_alloc */ |
| 13870 | unicode_new, /* tp_new */ |
| 13871 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13872 | }; |
| 13873 | |
| 13874 | /* Initialize the Unicode implementation */ |
| 13875 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13876 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13877 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13878 | int i; |
| 13879 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13880 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13881 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13882 | 0x000A, /* LINE FEED */ |
| 13883 | 0x000D, /* CARRIAGE RETURN */ |
| 13884 | 0x001C, /* FILE SEPARATOR */ |
| 13885 | 0x001D, /* GROUP SEPARATOR */ |
| 13886 | 0x001E, /* RECORD SEPARATOR */ |
| 13887 | 0x0085, /* NEXT LINE */ |
| 13888 | 0x2028, /* LINE SEPARATOR */ |
| 13889 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13890 | }; |
| 13891 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13892 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13893 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13894 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13895 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 13896 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13897 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13898 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13899 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13900 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13901 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13902 | |
| 13903 | /* initialize the linebreak bloom filter */ |
| 13904 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13905 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13906 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13907 | |
| 13908 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 13909 | |
| 13910 | #ifdef HAVE_MBCS |
| 13911 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 13912 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 13913 | PyErr_SetFromWindowsErr(0); |
| 13914 | return -1; |
| 13915 | } |
| 13916 | #endif |
| 13917 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13918 | } |
| 13919 | |
| 13920 | /* Finalize the Unicode implementation */ |
| 13921 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13922 | int |
| 13923 | PyUnicode_ClearFreeList(void) |
| 13924 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13925 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13926 | } |
| 13927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13928 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13929 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13930 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13931 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13932 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13933 | Py_XDECREF(unicode_empty); |
| 13934 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13935 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13936 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13937 | if (unicode_latin1[i]) { |
| 13938 | Py_DECREF(unicode_latin1[i]); |
| 13939 | unicode_latin1[i] = NULL; |
| 13940 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13941 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13942 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13943 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13944 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13945 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13946 | void |
| 13947 | PyUnicode_InternInPlace(PyObject **p) |
| 13948 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13949 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13950 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13951 | #ifdef Py_DEBUG |
| 13952 | assert(s != NULL); |
| 13953 | assert(_PyUnicode_CHECK(s)); |
| 13954 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13955 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13956 | return; |
| 13957 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13958 | /* If it's a subclass, we don't really know what putting |
| 13959 | it in the interned dict might do. */ |
| 13960 | if (!PyUnicode_CheckExact(s)) |
| 13961 | return; |
| 13962 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13963 | return; |
| 13964 | if (interned == NULL) { |
| 13965 | interned = PyDict_New(); |
| 13966 | if (interned == NULL) { |
| 13967 | PyErr_Clear(); /* Don't leave an exception */ |
| 13968 | return; |
| 13969 | } |
| 13970 | } |
| 13971 | /* It might be that the GetItem call fails even |
| 13972 | though the key is present in the dictionary, |
| 13973 | namely when this happens during a stack overflow. */ |
| 13974 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13975 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13976 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13977 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13978 | if (t) { |
| 13979 | Py_INCREF(t); |
| 13980 | Py_DECREF(*p); |
| 13981 | *p = t; |
| 13982 | return; |
| 13983 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13984 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13985 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13986 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13987 | PyErr_Clear(); |
| 13988 | PyThreadState_GET()->recursion_critical = 0; |
| 13989 | return; |
| 13990 | } |
| 13991 | PyThreadState_GET()->recursion_critical = 0; |
| 13992 | /* The two references in interned are not counted by refcnt. |
| 13993 | The deallocator will take care of this */ |
| 13994 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13995 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13996 | } |
| 13997 | |
| 13998 | void |
| 13999 | PyUnicode_InternImmortal(PyObject **p) |
| 14000 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14001 | PyUnicode_InternInPlace(p); |
| 14002 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14003 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14004 | Py_INCREF(*p); |
| 14005 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14006 | } |
| 14007 | |
| 14008 | PyObject * |
| 14009 | PyUnicode_InternFromString(const char *cp) |
| 14010 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14011 | PyObject *s = PyUnicode_FromString(cp); |
| 14012 | if (s == NULL) |
| 14013 | return NULL; |
| 14014 | PyUnicode_InternInPlace(&s); |
| 14015 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14016 | } |
| 14017 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14018 | void |
| 14019 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14020 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14021 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14022 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14023 | Py_ssize_t i, n; |
| 14024 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14025 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14026 | if (interned == NULL || !PyDict_Check(interned)) |
| 14027 | return; |
| 14028 | keys = PyDict_Keys(interned); |
| 14029 | if (keys == NULL || !PyList_Check(keys)) { |
| 14030 | PyErr_Clear(); |
| 14031 | return; |
| 14032 | } |
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 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14035 | detector, interned unicode strings are not forcibly deallocated; |
| 14036 | rather, we give them their stolen references back, and then clear |
| 14037 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14038 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14039 | n = PyList_GET_SIZE(keys); |
| 14040 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14041 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14043 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14044 | if (PyUnicode_READY(s) == -1) { |
| 14045 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14046 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14048 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14049 | case SSTATE_NOT_INTERNED: |
| 14050 | /* XXX Shouldn't happen */ |
| 14051 | break; |
| 14052 | case SSTATE_INTERNED_IMMORTAL: |
| 14053 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14054 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14055 | break; |
| 14056 | case SSTATE_INTERNED_MORTAL: |
| 14057 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14058 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14059 | break; |
| 14060 | default: |
| 14061 | Py_FatalError("Inconsistent interned string state."); |
| 14062 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14063 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14064 | } |
| 14065 | fprintf(stderr, "total size of all interned strings: " |
| 14066 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14067 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14068 | Py_DECREF(keys); |
| 14069 | PyDict_Clear(interned); |
| 14070 | Py_DECREF(interned); |
| 14071 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14072 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14073 | |
| 14074 | |
| 14075 | /********************* Unicode Iterator **************************/ |
| 14076 | |
| 14077 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14078 | PyObject_HEAD |
| 14079 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14080 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14081 | } unicodeiterobject; |
| 14082 | |
| 14083 | static void |
| 14084 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14085 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14086 | _PyObject_GC_UNTRACK(it); |
| 14087 | Py_XDECREF(it->it_seq); |
| 14088 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14089 | } |
| 14090 | |
| 14091 | static int |
| 14092 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14093 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14094 | Py_VISIT(it->it_seq); |
| 14095 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14096 | } |
| 14097 | |
| 14098 | static PyObject * |
| 14099 | unicodeiter_next(unicodeiterobject *it) |
| 14100 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14101 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14102 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14103 | assert(it != NULL); |
| 14104 | seq = it->it_seq; |
| 14105 | if (seq == NULL) |
| 14106 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14107 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14109 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14110 | int kind = PyUnicode_KIND(seq); |
| 14111 | void *data = PyUnicode_DATA(seq); |
| 14112 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14113 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14114 | if (item != NULL) |
| 14115 | ++it->it_index; |
| 14116 | return item; |
| 14117 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14118 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14119 | Py_DECREF(seq); |
| 14120 | it->it_seq = NULL; |
| 14121 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14122 | } |
| 14123 | |
| 14124 | static PyObject * |
| 14125 | unicodeiter_len(unicodeiterobject *it) |
| 14126 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14127 | Py_ssize_t len = 0; |
| 14128 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14129 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14130 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14131 | } |
| 14132 | |
| 14133 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14134 | |
| 14135 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14136 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14137 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14138 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14139 | }; |
| 14140 | |
| 14141 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14142 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14143 | "str_iterator", /* tp_name */ |
| 14144 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14145 | 0, /* tp_itemsize */ |
| 14146 | /* methods */ |
| 14147 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14148 | 0, /* tp_print */ |
| 14149 | 0, /* tp_getattr */ |
| 14150 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14151 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14152 | 0, /* tp_repr */ |
| 14153 | 0, /* tp_as_number */ |
| 14154 | 0, /* tp_as_sequence */ |
| 14155 | 0, /* tp_as_mapping */ |
| 14156 | 0, /* tp_hash */ |
| 14157 | 0, /* tp_call */ |
| 14158 | 0, /* tp_str */ |
| 14159 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14160 | 0, /* tp_setattro */ |
| 14161 | 0, /* tp_as_buffer */ |
| 14162 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14163 | 0, /* tp_doc */ |
| 14164 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14165 | 0, /* tp_clear */ |
| 14166 | 0, /* tp_richcompare */ |
| 14167 | 0, /* tp_weaklistoffset */ |
| 14168 | PyObject_SelfIter, /* tp_iter */ |
| 14169 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14170 | unicodeiter_methods, /* tp_methods */ |
| 14171 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14172 | }; |
| 14173 | |
| 14174 | static PyObject * |
| 14175 | unicode_iter(PyObject *seq) |
| 14176 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14177 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14178 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14179 | if (!PyUnicode_Check(seq)) { |
| 14180 | PyErr_BadInternalCall(); |
| 14181 | return NULL; |
| 14182 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14183 | if (PyUnicode_READY(seq) == -1) |
| 14184 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14185 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14186 | if (it == NULL) |
| 14187 | return NULL; |
| 14188 | it->it_index = 0; |
| 14189 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14190 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14191 | _PyObject_GC_TRACK(it); |
| 14192 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14193 | } |
| 14194 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14195 | |
| 14196 | size_t |
| 14197 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14198 | { |
| 14199 | int res = 0; |
| 14200 | while(*u++) |
| 14201 | res++; |
| 14202 | return res; |
| 14203 | } |
| 14204 | |
| 14205 | Py_UNICODE* |
| 14206 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14207 | { |
| 14208 | Py_UNICODE *u = s1; |
| 14209 | while ((*u++ = *s2++)); |
| 14210 | return s1; |
| 14211 | } |
| 14212 | |
| 14213 | Py_UNICODE* |
| 14214 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14215 | { |
| 14216 | Py_UNICODE *u = s1; |
| 14217 | while ((*u++ = *s2++)) |
| 14218 | if (n-- == 0) |
| 14219 | break; |
| 14220 | return s1; |
| 14221 | } |
| 14222 | |
| 14223 | Py_UNICODE* |
| 14224 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14225 | { |
| 14226 | Py_UNICODE *u1 = s1; |
| 14227 | u1 += Py_UNICODE_strlen(u1); |
| 14228 | Py_UNICODE_strcpy(u1, s2); |
| 14229 | return s1; |
| 14230 | } |
| 14231 | |
| 14232 | int |
| 14233 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14234 | { |
| 14235 | while (*s1 && *s2 && *s1 == *s2) |
| 14236 | s1++, s2++; |
| 14237 | if (*s1 && *s2) |
| 14238 | return (*s1 < *s2) ? -1 : +1; |
| 14239 | if (*s1) |
| 14240 | return 1; |
| 14241 | if (*s2) |
| 14242 | return -1; |
| 14243 | return 0; |
| 14244 | } |
| 14245 | |
| 14246 | int |
| 14247 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14248 | { |
| 14249 | register Py_UNICODE u1, u2; |
| 14250 | for (; n != 0; n--) { |
| 14251 | u1 = *s1; |
| 14252 | u2 = *s2; |
| 14253 | if (u1 != u2) |
| 14254 | return (u1 < u2) ? -1 : +1; |
| 14255 | if (u1 == '\0') |
| 14256 | return 0; |
| 14257 | s1++; |
| 14258 | s2++; |
| 14259 | } |
| 14260 | return 0; |
| 14261 | } |
| 14262 | |
| 14263 | Py_UNICODE* |
| 14264 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14265 | { |
| 14266 | const Py_UNICODE *p; |
| 14267 | for (p = s; *p; p++) |
| 14268 | if (*p == c) |
| 14269 | return (Py_UNICODE*)p; |
| 14270 | return NULL; |
| 14271 | } |
| 14272 | |
| 14273 | Py_UNICODE* |
| 14274 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14275 | { |
| 14276 | const Py_UNICODE *p; |
| 14277 | p = s + Py_UNICODE_strlen(s); |
| 14278 | while (p != s) { |
| 14279 | p--; |
| 14280 | if (*p == c) |
| 14281 | return (Py_UNICODE*)p; |
| 14282 | } |
| 14283 | return NULL; |
| 14284 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14285 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14286 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14287 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14288 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14289 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14290 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14292 | if (!PyUnicode_Check(unicode)) { |
| 14293 | PyErr_BadArgument(); |
| 14294 | return NULL; |
| 14295 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14296 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14297 | if (u == NULL) |
| 14298 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14299 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14300 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14301 | PyErr_NoMemory(); |
| 14302 | return NULL; |
| 14303 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14304 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14305 | size *= sizeof(Py_UNICODE); |
| 14306 | copy = PyMem_Malloc(size); |
| 14307 | if (copy == NULL) { |
| 14308 | PyErr_NoMemory(); |
| 14309 | return NULL; |
| 14310 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14311 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14312 | return copy; |
| 14313 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14314 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14315 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14316 | to the string.Formatter class implemented in Python. */ |
| 14317 | |
| 14318 | static PyMethodDef _string_methods[] = { |
| 14319 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14320 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14321 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14322 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14323 | {NULL, NULL} |
| 14324 | }; |
| 14325 | |
| 14326 | static struct PyModuleDef _string_module = { |
| 14327 | PyModuleDef_HEAD_INIT, |
| 14328 | "_string", |
| 14329 | PyDoc_STR("string helper module"), |
| 14330 | 0, |
| 14331 | _string_methods, |
| 14332 | NULL, |
| 14333 | NULL, |
| 14334 | NULL, |
| 14335 | NULL |
| 14336 | }; |
| 14337 | |
| 14338 | PyMODINIT_FUNC |
| 14339 | PyInit__string(void) |
| 14340 | { |
| 14341 | return PyModule_Create(&_string_module); |
| 14342 | } |
| 14343 | |
| 14344 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14345 | #ifdef __cplusplus |
| 14346 | } |
| 14347 | #endif |