Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* Endianness switches; defaults to little endian */ |
| 51 | |
| 52 | #ifdef WORDS_BIGENDIAN |
| 53 | # define BYTEORDER_IS_BIG_ENDIAN |
| 54 | #else |
| 55 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 56 | #endif |
| 57 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 58 | /* --- Globals ------------------------------------------------------------ |
| 59 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 60 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 61 | initialized dynamically as needed. In the process Unicode objects may |
| 62 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 63 | |
| 64 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | |
| 67 | #ifdef __cplusplus |
| 68 | extern "C" { |
| 69 | #endif |
| 70 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 71 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 72 | #define MAX_UNICODE 0x10ffff |
| 73 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 74 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 75 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 76 | #else |
| 77 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 78 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 79 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 80 | #define _PyUnicode_UTF8(op) \ |
| 81 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 82 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 83 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 84 | assert(PyUnicode_IS_READY(op)), \ |
| 85 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 86 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 87 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 88 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 89 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 90 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 91 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 92 | assert(PyUnicode_IS_READY(op)), \ |
| 93 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 94 | ((PyASCIIObject*)(op))->length : \ |
| 95 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 96 | #define _PyUnicode_WSTR(op) \ |
| 97 | (((PyASCIIObject*)(op))->wstr) |
| 98 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 99 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 100 | #define _PyUnicode_LENGTH(op) \ |
| 101 | (((PyASCIIObject *)(op))->length) |
| 102 | #define _PyUnicode_STATE(op) \ |
| 103 | (((PyASCIIObject *)(op))->state) |
| 104 | #define _PyUnicode_HASH(op) \ |
| 105 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 106 | #define _PyUnicode_KIND(op) \ |
| 107 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 108 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 109 | #define _PyUnicode_GET_LENGTH(op) \ |
| 110 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 111 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 112 | #define _PyUnicode_DATA_ANY(op) \ |
| 113 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 114 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 115 | #undef PyUnicode_READY |
| 116 | #define PyUnicode_READY(op) \ |
| 117 | (assert(_PyUnicode_CHECK(op)), \ |
| 118 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 119 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 120 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 121 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 122 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 123 | (assert(_PyUnicode_CHECK(op)), \ |
| 124 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 125 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 126 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 127 | (assert(_PyUnicode_CHECK(op)), \ |
| 128 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 129 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 130 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 131 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 132 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 133 | (assert(_PyUnicode_CHECK(op)), \ |
| 134 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 135 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 136 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 137 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 138 | /* true if the Unicode object has an allocated wstr memory block |
| 139 | (not shared with other data) */ |
| 140 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 141 | (assert(_PyUnicode_CHECK(op)), \ |
| 142 | (_PyUnicode_WSTR(op) && \ |
| 143 | (!PyUnicode_IS_READY(op) || \ |
| 144 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 145 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 146 | /* Generic helper macro to convert characters of different types. |
| 147 | from_type and to_type have to be valid type names, begin and end |
| 148 | are pointers to the source characters which should be of type |
| 149 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 150 | buffer where the result characters are written to. */ |
| 151 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 152 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 153 | to_type *_to = (to_type *) to; \ |
| 154 | const from_type *_iter = (begin); \ |
| 155 | const from_type *_end = (end); \ |
| 156 | Py_ssize_t n = (_end) - (_iter); \ |
| 157 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 158 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 159 | while (_iter < (_unrolled_end)) { \ |
| 160 | _to[0] = (to_type) _iter[0]; \ |
| 161 | _to[1] = (to_type) _iter[1]; \ |
| 162 | _to[2] = (to_type) _iter[2]; \ |
| 163 | _to[3] = (to_type) _iter[3]; \ |
| 164 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 165 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 166 | while (_iter < (_end)) \ |
| 167 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 168 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 169 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 170 | /* This dictionary holds all interned unicode strings. Note that references |
| 171 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 172 | When the interned string reaches a refcnt of 0 the string deallocation |
| 173 | function will delete the reference from this dictionary. |
| 174 | |
| 175 | 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] | 176 | 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] | 177 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 178 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 179 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 180 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 181 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 182 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 183 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 184 | do { \ |
| 185 | if (unicode_empty != NULL) \ |
| 186 | Py_INCREF(unicode_empty); \ |
| 187 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 188 | unicode_empty = PyUnicode_New(0, 0); \ |
| 189 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 190 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 191 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 192 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 193 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 194 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 195 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 196 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 197 | do { \ |
| 198 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 199 | return unicode_empty; \ |
| 200 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 201 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 202 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 203 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 204 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 205 | /* Single character Unicode strings in the Latin-1 range are being |
| 206 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 207 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 208 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | /* Fast detection of the most frequent whitespace characters */ |
| 210 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 212 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 213 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 214 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 215 | /* case 0x000C: * FORM FEED */ |
| 216 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 217 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 219 | /* case 0x001C: * FILE SEPARATOR */ |
| 220 | /* case 0x001D: * GROUP SEPARATOR */ |
| 221 | /* case 0x001E: * RECORD SEPARATOR */ |
| 222 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 223 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 224 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 225 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 228 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 229 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 237 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 240 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 241 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 242 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 243 | static int unicode_modifiable(PyObject *unicode); |
| 244 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 246 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 247 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 248 | static PyObject * |
| 249 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 250 | static PyObject * |
| 251 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 252 | |
| 253 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 254 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 255 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 256 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 257 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 258 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 259 | static void |
| 260 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 261 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 262 | PyObject *unicode, |
| 263 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 264 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 265 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 266 | /* Same for linebreaks */ |
| 267 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 270 | /* 0x000B, * LINE TABULATION */ |
| 271 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 272 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 273 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 275 | /* 0x001C, * FILE SEPARATOR */ |
| 276 | /* 0x001D, * GROUP SEPARATOR */ |
| 277 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 278 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 283 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 294 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 295 | 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] | 296 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 297 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 298 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 299 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 300 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 301 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 302 | /* This is actually an illegal character, so it should |
| 303 | not be passed to unichr. */ |
| 304 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 305 | #endif |
| 306 | } |
| 307 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 308 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 309 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 310 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 311 | { |
| 312 | PyASCIIObject *ascii; |
| 313 | unsigned int kind; |
| 314 | |
| 315 | assert(PyUnicode_Check(op)); |
| 316 | |
| 317 | ascii = (PyASCIIObject *)op; |
| 318 | kind = ascii->state.kind; |
| 319 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 320 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 322 | assert(ascii->state.ready == 1); |
| 323 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 324 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 325 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 326 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 327 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 328 | if (ascii->state.compact == 1) { |
| 329 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 330 | assert(kind == PyUnicode_1BYTE_KIND |
| 331 | || kind == PyUnicode_2BYTE_KIND |
| 332 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 334 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 335 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 336 | } |
| 337 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 338 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 339 | |
| 340 | data = unicode->data.any; |
| 341 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 342 | assert(ascii->length == 0); |
| 343 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 344 | assert(ascii->state.compact == 0); |
| 345 | assert(ascii->state.ascii == 0); |
| 346 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 347 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 348 | assert(ascii->wstr != NULL); |
| 349 | assert(data == NULL); |
| 350 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 351 | } |
| 352 | else { |
| 353 | assert(kind == PyUnicode_1BYTE_KIND |
| 354 | || kind == PyUnicode_2BYTE_KIND |
| 355 | || kind == PyUnicode_4BYTE_KIND); |
| 356 | assert(ascii->state.compact == 0); |
| 357 | assert(ascii->state.ready == 1); |
| 358 | assert(data != NULL); |
| 359 | if (ascii->state.ascii) { |
| 360 | assert (compact->utf8 == data); |
| 361 | assert (compact->utf8_length == ascii->length); |
| 362 | } |
| 363 | else |
| 364 | assert (compact->utf8 != data); |
| 365 | } |
| 366 | } |
| 367 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 368 | if ( |
| 369 | #if SIZEOF_WCHAR_T == 2 |
| 370 | kind == PyUnicode_2BYTE_KIND |
| 371 | #else |
| 372 | kind == PyUnicode_4BYTE_KIND |
| 373 | #endif |
| 374 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 375 | { |
| 376 | assert(ascii->wstr == data); |
| 377 | assert(compact->wstr_length == ascii->length); |
| 378 | } else |
| 379 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 380 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 381 | |
| 382 | if (compact->utf8 == NULL) |
| 383 | assert(compact->utf8_length == 0); |
| 384 | if (ascii->wstr == NULL) |
| 385 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 386 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 387 | /* check that the best kind is used */ |
| 388 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 389 | { |
| 390 | Py_ssize_t i; |
| 391 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 392 | void *data; |
| 393 | Py_UCS4 ch; |
| 394 | |
| 395 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 396 | for (i=0; i < ascii->length; i++) |
| 397 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 398 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 399 | if (ch > maxchar) |
| 400 | maxchar = ch; |
| 401 | } |
| 402 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 403 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 404 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 405 | assert(maxchar <= 255); |
| 406 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 407 | else |
| 408 | assert(maxchar < 128); |
| 409 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 410 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 411 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 412 | assert(maxchar <= 0xFFFF); |
| 413 | } |
| 414 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 415 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 416 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 417 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 418 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 419 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 420 | return 1; |
| 421 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 422 | #endif |
| 423 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 424 | static PyObject* |
| 425 | unicode_result_wchar(PyObject *unicode) |
| 426 | { |
| 427 | #ifndef Py_DEBUG |
| 428 | Py_ssize_t len; |
| 429 | |
| 430 | assert(Py_REFCNT(unicode) == 1); |
| 431 | |
| 432 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 433 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 434 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 435 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (len == 1) { |
| 439 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 440 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 441 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 442 | Py_DECREF(unicode); |
| 443 | return latin1_char; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (_PyUnicode_Ready(unicode) < 0) { |
| 448 | Py_XDECREF(unicode); |
| 449 | return NULL; |
| 450 | } |
| 451 | #else |
| 452 | /* don't make the result ready in debug mode to ensure that the caller |
| 453 | makes the string ready before using it */ |
| 454 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 455 | #endif |
| 456 | return unicode; |
| 457 | } |
| 458 | |
| 459 | static PyObject* |
| 460 | unicode_result_ready(PyObject *unicode) |
| 461 | { |
| 462 | Py_ssize_t length; |
| 463 | |
| 464 | length = PyUnicode_GET_LENGTH(unicode); |
| 465 | if (length == 0) { |
| 466 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 467 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 468 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 469 | } |
| 470 | return unicode_empty; |
| 471 | } |
| 472 | |
| 473 | if (length == 1) { |
| 474 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 475 | if (ch < 256) { |
| 476 | PyObject *latin1_char = unicode_latin1[ch]; |
| 477 | if (latin1_char != NULL) { |
| 478 | if (unicode != latin1_char) { |
| 479 | Py_INCREF(latin1_char); |
| 480 | Py_DECREF(unicode); |
| 481 | } |
| 482 | return latin1_char; |
| 483 | } |
| 484 | else { |
| 485 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 486 | Py_INCREF(unicode); |
| 487 | unicode_latin1[ch] = unicode; |
| 488 | return unicode; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 494 | return unicode; |
| 495 | } |
| 496 | |
| 497 | static PyObject* |
| 498 | unicode_result(PyObject *unicode) |
| 499 | { |
| 500 | assert(_PyUnicode_CHECK(unicode)); |
| 501 | if (PyUnicode_IS_READY(unicode)) |
| 502 | return unicode_result_ready(unicode); |
| 503 | else |
| 504 | return unicode_result_wchar(unicode); |
| 505 | } |
| 506 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 507 | static PyObject* |
| 508 | unicode_result_unchanged(PyObject *unicode) |
| 509 | { |
| 510 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 511 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 512 | return NULL; |
| 513 | Py_INCREF(unicode); |
| 514 | return unicode; |
| 515 | } |
| 516 | else |
| 517 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 518 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 521 | #ifdef HAVE_MBCS |
| 522 | static OSVERSIONINFOEX winver; |
| 523 | #endif |
| 524 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 525 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 526 | |
| 527 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 528 | to keep things simple, we use a single bitmask, using the least 5 |
| 529 | bits from each unicode characters as the bit index. */ |
| 530 | |
| 531 | /* the linebreak mask is set up by Unicode_Init below */ |
| 532 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 533 | #if LONG_BIT >= 128 |
| 534 | #define BLOOM_WIDTH 128 |
| 535 | #elif LONG_BIT >= 64 |
| 536 | #define BLOOM_WIDTH 64 |
| 537 | #elif LONG_BIT >= 32 |
| 538 | #define BLOOM_WIDTH 32 |
| 539 | #else |
| 540 | #error "LONG_BIT is smaller than 32" |
| 541 | #endif |
| 542 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | #define BLOOM_MASK unsigned long |
| 544 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 545 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 547 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 548 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 550 | #define BLOOM_LINEBREAK(ch) \ |
| 551 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 552 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 554 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 555 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 556 | { |
| 557 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 558 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 559 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 560 | Py_ssize_t i; |
| 561 | |
| 562 | mask = 0; |
| 563 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 564 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 565 | |
| 566 | return mask; |
| 567 | } |
| 568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 569 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 570 | (BLOOM(mask, chr) \ |
| 571 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 572 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 573 | /* Compilation of templated routines */ |
| 574 | |
| 575 | #include "stringlib/asciilib.h" |
| 576 | #include "stringlib/fastsearch.h" |
| 577 | #include "stringlib/partition.h" |
| 578 | #include "stringlib/split.h" |
| 579 | #include "stringlib/count.h" |
| 580 | #include "stringlib/find.h" |
| 581 | #include "stringlib/find_max_char.h" |
| 582 | #include "stringlib/localeutil.h" |
| 583 | #include "stringlib/undef.h" |
| 584 | |
| 585 | #include "stringlib/ucs1lib.h" |
| 586 | #include "stringlib/fastsearch.h" |
| 587 | #include "stringlib/partition.h" |
| 588 | #include "stringlib/split.h" |
| 589 | #include "stringlib/count.h" |
| 590 | #include "stringlib/find.h" |
| 591 | #include "stringlib/find_max_char.h" |
| 592 | #include "stringlib/localeutil.h" |
| 593 | #include "stringlib/undef.h" |
| 594 | |
| 595 | #include "stringlib/ucs2lib.h" |
| 596 | #include "stringlib/fastsearch.h" |
| 597 | #include "stringlib/partition.h" |
| 598 | #include "stringlib/split.h" |
| 599 | #include "stringlib/count.h" |
| 600 | #include "stringlib/find.h" |
| 601 | #include "stringlib/find_max_char.h" |
| 602 | #include "stringlib/localeutil.h" |
| 603 | #include "stringlib/undef.h" |
| 604 | |
| 605 | #include "stringlib/ucs4lib.h" |
| 606 | #include "stringlib/fastsearch.h" |
| 607 | #include "stringlib/partition.h" |
| 608 | #include "stringlib/split.h" |
| 609 | #include "stringlib/count.h" |
| 610 | #include "stringlib/find.h" |
| 611 | #include "stringlib/find_max_char.h" |
| 612 | #include "stringlib/localeutil.h" |
| 613 | #include "stringlib/undef.h" |
| 614 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 615 | #include "stringlib/unicodedefs.h" |
| 616 | #include "stringlib/fastsearch.h" |
| 617 | #include "stringlib/count.h" |
| 618 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 619 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 621 | /* --- Unicode Object ----------------------------------------------------- */ |
| 622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 623 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 624 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 625 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 626 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 627 | Py_ssize_t size, Py_UCS4 ch, |
| 628 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 629 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 630 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 631 | |
| 632 | switch (kind) { |
| 633 | case PyUnicode_1BYTE_KIND: |
| 634 | { |
| 635 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 636 | if (ch1 == ch) |
| 637 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 638 | else |
| 639 | return -1; |
| 640 | } |
| 641 | case PyUnicode_2BYTE_KIND: |
| 642 | { |
| 643 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 644 | if (ch2 == ch) |
| 645 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 646 | else |
| 647 | return -1; |
| 648 | } |
| 649 | case PyUnicode_4BYTE_KIND: |
| 650 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 651 | default: |
| 652 | assert(0); |
| 653 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 654 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | static PyObject* |
| 658 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 659 | { |
| 660 | Py_ssize_t char_size; |
| 661 | Py_ssize_t struct_size; |
| 662 | Py_ssize_t new_size; |
| 663 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 664 | PyObject *new_unicode; |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 665 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 666 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 667 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 668 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 669 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 670 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 671 | struct_size = sizeof(PyASCIIObject); |
| 672 | else |
| 673 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 674 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 675 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 676 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 677 | PyErr_NoMemory(); |
| 678 | return NULL; |
| 679 | } |
| 680 | new_size = (struct_size + (length + 1) * char_size); |
| 681 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 682 | _Py_DEC_REFTOTAL; |
| 683 | _Py_ForgetReference(unicode); |
| 684 | |
| 685 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 686 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 687 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 688 | PyErr_NoMemory(); |
| 689 | return NULL; |
| 690 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 691 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 692 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 693 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 694 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 695 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 696 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 697 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 698 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 699 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 700 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 701 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 702 | _PyUnicode_WSTR(unicode) = NULL; |
| 703 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 705 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 706 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 707 | return unicode; |
| 708 | } |
| 709 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 710 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 711 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 713 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 714 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 715 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 717 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | if (PyUnicode_IS_READY(unicode)) { |
| 719 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 720 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 721 | void *data; |
| 722 | |
| 723 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 724 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 725 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 726 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 727 | |
| 728 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 729 | PyErr_NoMemory(); |
| 730 | return -1; |
| 731 | } |
| 732 | new_size = (length + 1) * char_size; |
| 733 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 734 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 735 | { |
| 736 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 737 | _PyUnicode_UTF8(unicode) = NULL; |
| 738 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 739 | } |
| 740 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 742 | if (data == NULL) { |
| 743 | PyErr_NoMemory(); |
| 744 | return -1; |
| 745 | } |
| 746 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 747 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 748 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 749 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 750 | } |
| 751 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 752 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 753 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 754 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 755 | _PyUnicode_LENGTH(unicode) = length; |
| 756 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 757 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 758 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 759 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 760 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 761 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 762 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 763 | |
| 764 | /* check for integer overflow */ |
| 765 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 766 | PyErr_NoMemory(); |
| 767 | return -1; |
| 768 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 769 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 770 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 771 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 772 | if (!wstr) { |
| 773 | PyErr_NoMemory(); |
| 774 | return -1; |
| 775 | } |
| 776 | _PyUnicode_WSTR(unicode) = wstr; |
| 777 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 778 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 779 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 783 | static PyObject* |
| 784 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 785 | { |
| 786 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 787 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 788 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 789 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 790 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 791 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 792 | |
| 793 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 794 | if (copy == NULL) |
| 795 | return NULL; |
| 796 | |
| 797 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 798 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 799 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 800 | } |
| 801 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 802 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 803 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 804 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 805 | if (w == NULL) |
| 806 | return NULL; |
| 807 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 808 | copy_length = Py_MIN(copy_length, length); |
| 809 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 810 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 811 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 815 | /* 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] | 816 | Ux0000 terminated; some code (e.g. new_identifier) |
| 817 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 818 | |
| 819 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 820 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 821 | |
| 822 | */ |
| 823 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 824 | static PyUnicodeObject * |
| 825 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 826 | { |
| 827 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 828 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 829 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 830 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 831 | if (length == 0 && unicode_empty != NULL) { |
| 832 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 833 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 836 | /* Ensure we won't overflow the size. */ |
| 837 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 838 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 839 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 840 | if (length < 0) { |
| 841 | PyErr_SetString(PyExc_SystemError, |
| 842 | "Negative size passed to _PyUnicode_New"); |
| 843 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 846 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 847 | if (unicode == NULL) |
| 848 | return NULL; |
| 849 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 850 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 851 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 852 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 853 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 854 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 855 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 856 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 857 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 858 | * the caller fails before initializing str -- unicode_resize() |
| 859 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 860 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 861 | * We don't want unicode_resize to read uninitialized memory in |
| 862 | * that case. |
| 863 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 864 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 865 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 866 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 867 | _PyUnicode_HASH(unicode) = -1; |
| 868 | _PyUnicode_STATE(unicode).interned = 0; |
| 869 | _PyUnicode_STATE(unicode).kind = 0; |
| 870 | _PyUnicode_STATE(unicode).compact = 0; |
| 871 | _PyUnicode_STATE(unicode).ready = 0; |
| 872 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 873 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 874 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 875 | _PyUnicode_UTF8(unicode) = NULL; |
| 876 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 877 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 878 | return unicode; |
| 879 | } |
| 880 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 881 | static const char* |
| 882 | unicode_kind_name(PyObject *unicode) |
| 883 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 884 | /* don't check consistency: unicode_kind_name() is called from |
| 885 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 886 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 887 | { |
| 888 | if (!PyUnicode_IS_READY(unicode)) |
| 889 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 890 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 891 | { |
| 892 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 893 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 894 | return "legacy ascii"; |
| 895 | else |
| 896 | return "legacy latin1"; |
| 897 | case PyUnicode_2BYTE_KIND: |
| 898 | return "legacy UCS2"; |
| 899 | case PyUnicode_4BYTE_KIND: |
| 900 | return "legacy UCS4"; |
| 901 | default: |
| 902 | return "<legacy invalid kind>"; |
| 903 | } |
| 904 | } |
| 905 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 906 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 907 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 908 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 909 | return "ascii"; |
| 910 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 911 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 912 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 913 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 914 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 915 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 916 | default: |
| 917 | return "<invalid compact kind>"; |
| 918 | } |
| 919 | } |
| 920 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 921 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 922 | /* Functions wrapping macros for use in debugger */ |
| 923 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 924 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | void *_PyUnicode_compact_data(void *unicode) { |
| 928 | return _PyUnicode_COMPACT_DATA(unicode); |
| 929 | } |
| 930 | void *_PyUnicode_data(void *unicode){ |
| 931 | printf("obj %p\n", unicode); |
| 932 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 933 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 934 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 935 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 936 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 937 | return PyUnicode_DATA(unicode); |
| 938 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 939 | |
| 940 | void |
| 941 | _PyUnicode_Dump(PyObject *op) |
| 942 | { |
| 943 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 944 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 945 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 946 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 947 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 948 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 949 | { |
| 950 | if (ascii->state.ascii) |
| 951 | data = (ascii + 1); |
| 952 | else |
| 953 | data = (compact + 1); |
| 954 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 955 | else |
| 956 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 957 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 958 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 959 | if (ascii->wstr == data) |
| 960 | printf("shared "); |
| 961 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 962 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 963 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 964 | printf(" (%zu), ", compact->wstr_length); |
| 965 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 966 | printf("shared "); |
| 967 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 968 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 969 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 970 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 971 | #endif |
| 972 | |
| 973 | PyObject * |
| 974 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 975 | { |
| 976 | PyObject *obj; |
| 977 | PyCompactUnicodeObject *unicode; |
| 978 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 979 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 980 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | Py_ssize_t char_size; |
| 982 | Py_ssize_t struct_size; |
| 983 | |
| 984 | /* Optimization for empty strings */ |
| 985 | if (size == 0 && unicode_empty != NULL) { |
| 986 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 987 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 990 | is_ascii = 0; |
| 991 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | struct_size = sizeof(PyCompactUnicodeObject); |
| 993 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 994 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 995 | char_size = 1; |
| 996 | is_ascii = 1; |
| 997 | struct_size = sizeof(PyASCIIObject); |
| 998 | } |
| 999 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1000 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1001 | char_size = 1; |
| 1002 | } |
| 1003 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1004 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1005 | char_size = 2; |
| 1006 | if (sizeof(wchar_t) == 2) |
| 1007 | is_sharing = 1; |
| 1008 | } |
| 1009 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1010 | if (maxchar > MAX_UNICODE) { |
| 1011 | PyErr_SetString(PyExc_SystemError, |
| 1012 | "invalid maximum character passed to PyUnicode_New"); |
| 1013 | return NULL; |
| 1014 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1015 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1016 | char_size = 4; |
| 1017 | if (sizeof(wchar_t) == 4) |
| 1018 | is_sharing = 1; |
| 1019 | } |
| 1020 | |
| 1021 | /* Ensure we won't overflow the size. */ |
| 1022 | if (size < 0) { |
| 1023 | PyErr_SetString(PyExc_SystemError, |
| 1024 | "Negative size passed to PyUnicode_New"); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1028 | return PyErr_NoMemory(); |
| 1029 | |
| 1030 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1031 | * PyObject_New() so we are able to allocate space for the object and |
| 1032 | * it's data buffer. |
| 1033 | */ |
| 1034 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1035 | if (obj == NULL) |
| 1036 | return PyErr_NoMemory(); |
| 1037 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1038 | if (obj == NULL) |
| 1039 | return NULL; |
| 1040 | |
| 1041 | unicode = (PyCompactUnicodeObject *)obj; |
| 1042 | if (is_ascii) |
| 1043 | data = ((PyASCIIObject*)obj) + 1; |
| 1044 | else |
| 1045 | data = unicode + 1; |
| 1046 | _PyUnicode_LENGTH(unicode) = size; |
| 1047 | _PyUnicode_HASH(unicode) = -1; |
| 1048 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1049 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | _PyUnicode_STATE(unicode).compact = 1; |
| 1051 | _PyUnicode_STATE(unicode).ready = 1; |
| 1052 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1053 | if (is_ascii) { |
| 1054 | ((char*)data)[size] = 0; |
| 1055 | _PyUnicode_WSTR(unicode) = NULL; |
| 1056 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1057 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | ((char*)data)[size] = 0; |
| 1059 | _PyUnicode_WSTR(unicode) = NULL; |
| 1060 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1061 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1062 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1063 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | else { |
| 1065 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1066 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1067 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1069 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | ((Py_UCS4*)data)[size] = 0; |
| 1071 | if (is_sharing) { |
| 1072 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1073 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1074 | } |
| 1075 | else { |
| 1076 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1077 | _PyUnicode_WSTR(unicode) = NULL; |
| 1078 | } |
| 1079 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1080 | #ifdef Py_DEBUG |
| 1081 | /* Fill the data with invalid characters to detect bugs earlier. |
| 1082 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, |
| 1083 | at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII |
| 1084 | and U+FFFFFFFF is an invalid character in Unicode 6.0. */ |
| 1085 | memset(data, 0xff, size * kind); |
| 1086 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1087 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1088 | return obj; |
| 1089 | } |
| 1090 | |
| 1091 | #if SIZEOF_WCHAR_T == 2 |
| 1092 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1093 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1094 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1095 | |
| 1096 | This function assumes that unicode can hold one more code point than wstr |
| 1097 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1098 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | 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] | 1100 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | { |
| 1102 | const wchar_t *iter; |
| 1103 | Py_UCS4 *ucs4_out; |
| 1104 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1105 | assert(unicode != NULL); |
| 1106 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1108 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1109 | |
| 1110 | for (iter = begin; iter < end; ) { |
| 1111 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1112 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1113 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1114 | && (iter+1) < end |
| 1115 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1117 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | iter += 2; |
| 1119 | } |
| 1120 | else { |
| 1121 | *ucs4_out++ = *iter; |
| 1122 | iter++; |
| 1123 | } |
| 1124 | } |
| 1125 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1126 | _PyUnicode_GET_LENGTH(unicode))); |
| 1127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | } |
| 1129 | #endif |
| 1130 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1131 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1132 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1133 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1134 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1135 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1136 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1137 | return -1; |
| 1138 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1142 | static int |
| 1143 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1144 | PyObject *from, Py_ssize_t from_start, |
| 1145 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1147 | unsigned int from_kind, to_kind; |
| 1148 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1150 | assert(0 <= how_many); |
| 1151 | assert(0 <= from_start); |
| 1152 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1153 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1154 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1155 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1157 | assert(PyUnicode_Check(to)); |
| 1158 | assert(PyUnicode_IS_READY(to)); |
| 1159 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1160 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1161 | if (how_many == 0) |
| 1162 | return 0; |
| 1163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1164 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1165 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1166 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1167 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1168 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1169 | #ifdef Py_DEBUG |
| 1170 | if (!check_maxchar |
| 1171 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1172 | { |
| 1173 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1174 | Py_UCS4 ch; |
| 1175 | Py_ssize_t i; |
| 1176 | for (i=0; i < how_many; i++) { |
| 1177 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1178 | assert(ch <= to_maxchar); |
| 1179 | } |
| 1180 | } |
| 1181 | #endif |
| 1182 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1183 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1184 | if (check_maxchar |
| 1185 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1186 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1187 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1188 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1189 | Py_UCS4 max_char; |
| 1190 | max_char = ucs1lib_find_max_char(from_data, |
| 1191 | (Py_UCS1*)from_data + how_many); |
| 1192 | if (max_char >= 128) |
| 1193 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1194 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1195 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1196 | (char*)from_data + from_kind * from_start, |
| 1197 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1198 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1199 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1200 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1201 | { |
| 1202 | _PyUnicode_CONVERT_BYTES( |
| 1203 | Py_UCS1, Py_UCS2, |
| 1204 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1205 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1206 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1207 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1208 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1209 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1210 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1211 | { |
| 1212 | _PyUnicode_CONVERT_BYTES( |
| 1213 | Py_UCS1, Py_UCS4, |
| 1214 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1215 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1216 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1217 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1218 | } |
| 1219 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1220 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1221 | { |
| 1222 | _PyUnicode_CONVERT_BYTES( |
| 1223 | Py_UCS2, Py_UCS4, |
| 1224 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1225 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1226 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1227 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1228 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1229 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1230 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1231 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1232 | if (!check_maxchar) { |
| 1233 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1234 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1235 | { |
| 1236 | _PyUnicode_CONVERT_BYTES( |
| 1237 | Py_UCS2, Py_UCS1, |
| 1238 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1239 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1240 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1241 | ); |
| 1242 | } |
| 1243 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1244 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1245 | { |
| 1246 | _PyUnicode_CONVERT_BYTES( |
| 1247 | Py_UCS4, Py_UCS1, |
| 1248 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1249 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1250 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1251 | ); |
| 1252 | } |
| 1253 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1254 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1255 | { |
| 1256 | _PyUnicode_CONVERT_BYTES( |
| 1257 | Py_UCS4, Py_UCS2, |
| 1258 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1259 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1260 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1261 | ); |
| 1262 | } |
| 1263 | else { |
| 1264 | assert(0); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1268 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1269 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1270 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1271 | Py_ssize_t i; |
| 1272 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1273 | for (i=0; i < how_many; i++) { |
| 1274 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1275 | if (ch > to_maxchar) |
| 1276 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1277 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1278 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1279 | } |
| 1280 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1281 | return 0; |
| 1282 | } |
| 1283 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1284 | void |
| 1285 | _PyUnicode_FastCopyCharacters( |
| 1286 | PyObject *to, Py_ssize_t to_start, |
| 1287 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1288 | { |
| 1289 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1290 | } |
| 1291 | |
| 1292 | Py_ssize_t |
| 1293 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1294 | PyObject *from, Py_ssize_t from_start, |
| 1295 | Py_ssize_t how_many) |
| 1296 | { |
| 1297 | int err; |
| 1298 | |
| 1299 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1300 | PyErr_BadInternalCall(); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1304 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1305 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1306 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1307 | return -1; |
| 1308 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1309 | if (from_start < 0) { |
| 1310 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1311 | return -1; |
| 1312 | } |
| 1313 | if (to_start < 0) { |
| 1314 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1315 | return -1; |
| 1316 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1317 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1318 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1319 | PyErr_Format(PyExc_SystemError, |
| 1320 | "Cannot write %zi characters at %zi " |
| 1321 | "in a string of %zi characters", |
| 1322 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1323 | return -1; |
| 1324 | } |
| 1325 | |
| 1326 | if (how_many == 0) |
| 1327 | return 0; |
| 1328 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1329 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1330 | return -1; |
| 1331 | |
| 1332 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1333 | if (err) { |
| 1334 | PyErr_Format(PyExc_SystemError, |
| 1335 | "Cannot copy %s characters " |
| 1336 | "into a string of %s characters", |
| 1337 | unicode_kind_name(from), |
| 1338 | unicode_kind_name(to)); |
| 1339 | return -1; |
| 1340 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1341 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1344 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1345 | correct string length can be computed before converting a string to UCS4. |
| 1346 | This function counts single surrogates as a character and not as a pair. |
| 1347 | |
| 1348 | Return 0 on success, or -1 on error. */ |
| 1349 | static int |
| 1350 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1351 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1352 | { |
| 1353 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1354 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1355 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1356 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1357 | *num_surrogates = 0; |
| 1358 | *maxchar = 0; |
| 1359 | |
| 1360 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1361 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1362 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1363 | && (iter+1) < end |
| 1364 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1365 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1366 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | iter += 2; |
| 1369 | } |
| 1370 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1371 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1372 | { |
| 1373 | ch = *iter; |
| 1374 | iter++; |
| 1375 | } |
| 1376 | if (ch > *maxchar) { |
| 1377 | *maxchar = ch; |
| 1378 | if (*maxchar > MAX_UNICODE) { |
| 1379 | PyErr_Format(PyExc_ValueError, |
| 1380 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1381 | ch); |
| 1382 | return -1; |
| 1383 | } |
| 1384 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | } |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1389 | int |
| 1390 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | { |
| 1392 | wchar_t *end; |
| 1393 | Py_UCS4 maxchar = 0; |
| 1394 | Py_ssize_t num_surrogates; |
| 1395 | #if SIZEOF_WCHAR_T == 2 |
| 1396 | Py_ssize_t length_wo_surrogates; |
| 1397 | #endif |
| 1398 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1399 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1400 | strings were created using _PyObject_New() and where no canonical |
| 1401 | representation (the str field) has been set yet aka strings |
| 1402 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1403 | assert(_PyUnicode_CHECK(unicode)); |
| 1404 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1406 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1407 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1408 | /* Actually, it should neither be interned nor be anything else: */ |
| 1409 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1411 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1412 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1413 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | |
| 1416 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1417 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1418 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1419 | PyErr_NoMemory(); |
| 1420 | return -1; |
| 1421 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1422 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | _PyUnicode_WSTR(unicode), end, |
| 1424 | PyUnicode_1BYTE_DATA(unicode)); |
| 1425 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1426 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1427 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1428 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1429 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1430 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1431 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1432 | } |
| 1433 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1434 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1435 | _PyUnicode_UTF8(unicode) = NULL; |
| 1436 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1439 | _PyUnicode_WSTR(unicode) = NULL; |
| 1440 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1441 | } |
| 1442 | /* In this case we might have to convert down from 4-byte native |
| 1443 | wchar_t to 2-byte unicode. */ |
| 1444 | else if (maxchar < 65536) { |
| 1445 | assert(num_surrogates == 0 && |
| 1446 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1447 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1448 | #if SIZEOF_WCHAR_T == 2 |
| 1449 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1450 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1451 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1452 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1453 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1454 | _PyUnicode_UTF8(unicode) = NULL; |
| 1455 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1456 | #else |
| 1457 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1458 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1459 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1460 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1461 | PyErr_NoMemory(); |
| 1462 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1464 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1465 | _PyUnicode_WSTR(unicode), end, |
| 1466 | PyUnicode_2BYTE_DATA(unicode)); |
| 1467 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1468 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1469 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1470 | _PyUnicode_UTF8(unicode) = NULL; |
| 1471 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1472 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1473 | _PyUnicode_WSTR(unicode) = NULL; |
| 1474 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1475 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | } |
| 1477 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1478 | else { |
| 1479 | #if SIZEOF_WCHAR_T == 2 |
| 1480 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1481 | new normalized 4-byte version. */ |
| 1482 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1483 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1484 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | PyErr_NoMemory(); |
| 1486 | return -1; |
| 1487 | } |
| 1488 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1489 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1490 | _PyUnicode_UTF8(unicode) = NULL; |
| 1491 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1492 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1493 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1494 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1495 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1496 | _PyUnicode_WSTR(unicode) = NULL; |
| 1497 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1498 | #else |
| 1499 | assert(num_surrogates == 0); |
| 1500 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1501 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1502 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1503 | _PyUnicode_UTF8(unicode) = NULL; |
| 1504 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1505 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1506 | #endif |
| 1507 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1508 | } |
| 1509 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1510 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1511 | return 0; |
| 1512 | } |
| 1513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1514 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1515 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1516 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1517 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1518 | case SSTATE_NOT_INTERNED: |
| 1519 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1520 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1521 | case SSTATE_INTERNED_MORTAL: |
| 1522 | /* revive dead object temporarily for DelItem */ |
| 1523 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1524 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1525 | Py_FatalError( |
| 1526 | "deletion of interned string failed"); |
| 1527 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1529 | case SSTATE_INTERNED_IMMORTAL: |
| 1530 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1531 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1532 | default: |
| 1533 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1536 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1537 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1538 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1539 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1540 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1541 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1542 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1543 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1546 | #ifdef Py_DEBUG |
| 1547 | static int |
| 1548 | unicode_is_singleton(PyObject *unicode) |
| 1549 | { |
| 1550 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1551 | if (unicode == unicode_empty) |
| 1552 | return 1; |
| 1553 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1554 | { |
| 1555 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1556 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1557 | return 1; |
| 1558 | } |
| 1559 | return 0; |
| 1560 | } |
| 1561 | #endif |
| 1562 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1563 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1564 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1565 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1566 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1567 | if (Py_REFCNT(unicode) != 1) |
| 1568 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1569 | if (_PyUnicode_HASH(unicode) != -1) |
| 1570 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1571 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1572 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1573 | if (!PyUnicode_CheckExact(unicode)) |
| 1574 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1575 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1576 | /* singleton refcount is greater than 1 */ |
| 1577 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1578 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1579 | return 1; |
| 1580 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1581 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1582 | static int |
| 1583 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1584 | { |
| 1585 | PyObject *unicode; |
| 1586 | Py_ssize_t old_length; |
| 1587 | |
| 1588 | assert(p_unicode != NULL); |
| 1589 | unicode = *p_unicode; |
| 1590 | |
| 1591 | assert(unicode != NULL); |
| 1592 | assert(PyUnicode_Check(unicode)); |
| 1593 | assert(0 <= length); |
| 1594 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1595 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1596 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1597 | else |
| 1598 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1599 | if (old_length == length) |
| 1600 | return 0; |
| 1601 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1602 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1603 | _Py_INCREF_UNICODE_EMPTY(); |
| 1604 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1605 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1606 | Py_DECREF(*p_unicode); |
| 1607 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
| 1610 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1611 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1612 | PyObject *copy = resize_copy(unicode, length); |
| 1613 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1614 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | Py_DECREF(*p_unicode); |
| 1616 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1617 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1621 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1622 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1623 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1624 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1625 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1626 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1627 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1630 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1631 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1632 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1633 | PyObject *unicode; |
| 1634 | if (p_unicode == NULL) { |
| 1635 | PyErr_BadInternalCall(); |
| 1636 | return -1; |
| 1637 | } |
| 1638 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1639 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1640 | { |
| 1641 | PyErr_BadInternalCall(); |
| 1642 | return -1; |
| 1643 | } |
| 1644 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1645 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1646 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1647 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1648 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1649 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1650 | { |
| 1651 | PyObject *result; |
| 1652 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1653 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1654 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1655 | return 0; |
| 1656 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1657 | maxchar); |
| 1658 | if (result == NULL) |
| 1659 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1660 | _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1661 | Py_DECREF(*p_unicode); |
| 1662 | *p_unicode = result; |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | static int |
| 1667 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1668 | Py_UCS4 ch) |
| 1669 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1670 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1671 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1672 | return -1; |
| 1673 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1674 | PyUnicode_DATA(*p_unicode), |
| 1675 | (*pos)++, ch); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1679 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1680 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1681 | WARNING: The function doesn't copy the terminating null character and |
| 1682 | doesn't check the maximum character (may write a latin1 character in an |
| 1683 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1684 | static void |
| 1685 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1686 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1687 | { |
| 1688 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1689 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1690 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1691 | |
| 1692 | switch (kind) { |
| 1693 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1694 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1695 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1696 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1697 | } |
| 1698 | case PyUnicode_2BYTE_KIND: { |
| 1699 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1700 | Py_UCS2 *ucs2 = start; |
| 1701 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1702 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1703 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1704 | *ucs2 = (Py_UCS2)*str; |
| 1705 | |
| 1706 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1707 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1708 | } |
| 1709 | default: { |
| 1710 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1711 | Py_UCS4 *ucs4 = start; |
| 1712 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1713 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1714 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1715 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1716 | *ucs4 = (Py_UCS4)*str; |
| 1717 | |
| 1718 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | static PyObject* |
| 1725 | get_latin1_char(unsigned char ch) |
| 1726 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1727 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1728 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1729 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1730 | if (!unicode) |
| 1731 | return NULL; |
| 1732 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1733 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1734 | unicode_latin1[ch] = unicode; |
| 1735 | } |
| 1736 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1737 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1740 | PyObject * |
| 1741 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1742 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1743 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1744 | Py_UCS4 maxchar = 0; |
| 1745 | Py_ssize_t num_surrogates; |
| 1746 | |
| 1747 | if (u == NULL) |
| 1748 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1749 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1750 | /* If the Unicode data is known at construction time, we can apply |
| 1751 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1752 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1753 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1754 | if (size == 0) |
| 1755 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | /* Single character Unicode objects in the Latin-1 range are |
| 1758 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1759 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1760 | return get_latin1_char((unsigned char)*u); |
| 1761 | |
| 1762 | /* If not empty and not single character, copy the Unicode data |
| 1763 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1764 | if (find_maxchar_surrogates(u, u + size, |
| 1765 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | return NULL; |
| 1767 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1768 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1769 | if (!unicode) |
| 1770 | return NULL; |
| 1771 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1772 | switch (PyUnicode_KIND(unicode)) { |
| 1773 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1774 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1775 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1776 | break; |
| 1777 | case PyUnicode_2BYTE_KIND: |
| 1778 | #if Py_UNICODE_SIZE == 2 |
| 1779 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1780 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1781 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1782 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1783 | #endif |
| 1784 | break; |
| 1785 | case PyUnicode_4BYTE_KIND: |
| 1786 | #if SIZEOF_WCHAR_T == 2 |
| 1787 | /* This is the only case which has to process surrogates, thus |
| 1788 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1789 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1790 | #else |
| 1791 | assert(num_surrogates == 0); |
| 1792 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1793 | #endif |
| 1794 | break; |
| 1795 | default: |
| 1796 | assert(0 && "Impossible state"); |
| 1797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1799 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1802 | PyObject * |
| 1803 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1804 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1805 | if (size < 0) { |
| 1806 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1807 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1808 | return NULL; |
| 1809 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1810 | if (u != NULL) |
| 1811 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1812 | else |
| 1813 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1816 | PyObject * |
| 1817 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1818 | { |
| 1819 | size_t size = strlen(u); |
| 1820 | if (size > PY_SSIZE_T_MAX) { |
| 1821 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1822 | return NULL; |
| 1823 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1824 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1827 | PyObject * |
| 1828 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1829 | { |
| 1830 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1831 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1832 | strlen(id->string), |
| 1833 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1834 | if (!id->object) |
| 1835 | return NULL; |
| 1836 | PyUnicode_InternInPlace(&id->object); |
| 1837 | assert(!id->next); |
| 1838 | id->next = static_strings; |
| 1839 | static_strings = id; |
| 1840 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1841 | return id->object; |
| 1842 | } |
| 1843 | |
| 1844 | void |
| 1845 | _PyUnicode_ClearStaticStrings() |
| 1846 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1847 | _Py_Identifier *tmp, *s = static_strings; |
| 1848 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 1849 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1850 | tmp = s->next; |
| 1851 | s->next = NULL; |
| 1852 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1853 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1854 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1855 | } |
| 1856 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1857 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1858 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1859 | PyObject* |
| 1860 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1861 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1862 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1863 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1864 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1865 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1866 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1867 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1868 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1869 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1870 | unicode = PyUnicode_New(size, 127); |
| 1871 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1872 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1873 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1874 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1875 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1878 | static Py_UCS4 |
| 1879 | kind_maxchar_limit(unsigned int kind) |
| 1880 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1881 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1882 | case PyUnicode_1BYTE_KIND: |
| 1883 | return 0x80; |
| 1884 | case PyUnicode_2BYTE_KIND: |
| 1885 | return 0x100; |
| 1886 | case PyUnicode_4BYTE_KIND: |
| 1887 | return 0x10000; |
| 1888 | default: |
| 1889 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1890 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1894 | Py_LOCAL_INLINE(Py_UCS4) |
| 1895 | align_maxchar(Py_UCS4 maxchar) |
| 1896 | { |
| 1897 | if (maxchar <= 127) |
| 1898 | return 127; |
| 1899 | else if (maxchar <= 255) |
| 1900 | return 255; |
| 1901 | else if (maxchar <= 65535) |
| 1902 | return 65535; |
| 1903 | else |
| 1904 | return MAX_UNICODE; |
| 1905 | } |
| 1906 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1907 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1908 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1909 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1910 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1911 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1912 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1913 | if (size == 0) |
| 1914 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1915 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1916 | if (size == 1) |
| 1917 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1918 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1919 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1920 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1921 | if (!res) |
| 1922 | return NULL; |
| 1923 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1924 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1925 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1928 | static PyObject* |
| 1929 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1930 | { |
| 1931 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1932 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1933 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1934 | if (size == 0) |
| 1935 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1936 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1937 | if (size == 1) { |
| 1938 | Py_UCS4 ch = u[0]; |
| 1939 | if (ch < 256) |
| 1940 | return get_latin1_char((unsigned char)ch); |
| 1941 | |
| 1942 | res = PyUnicode_New(1, ch); |
| 1943 | if (res == NULL) |
| 1944 | return NULL; |
| 1945 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1946 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1947 | return res; |
| 1948 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1949 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1950 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1951 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1952 | if (!res) |
| 1953 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1954 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1955 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1956 | else { |
| 1957 | _PyUnicode_CONVERT_BYTES( |
| 1958 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1959 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1960 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1961 | return res; |
| 1962 | } |
| 1963 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1964 | static PyObject* |
| 1965 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1966 | { |
| 1967 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1968 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1969 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1970 | if (size == 0) |
| 1971 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1972 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1973 | if (size == 1) { |
| 1974 | Py_UCS4 ch = u[0]; |
| 1975 | if (ch < 256) |
| 1976 | return get_latin1_char((unsigned char)ch); |
| 1977 | |
| 1978 | res = PyUnicode_New(1, ch); |
| 1979 | if (res == NULL) |
| 1980 | return NULL; |
| 1981 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1982 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1983 | return res; |
| 1984 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1985 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1986 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1987 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | if (!res) |
| 1989 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1990 | if (max_char < 256) |
| 1991 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1992 | PyUnicode_1BYTE_DATA(res)); |
| 1993 | else if (max_char < 0x10000) |
| 1994 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1995 | PyUnicode_2BYTE_DATA(res)); |
| 1996 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1998 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | return res; |
| 2000 | } |
| 2001 | |
| 2002 | PyObject* |
| 2003 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2004 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2005 | if (size < 0) { |
| 2006 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2007 | return NULL; |
| 2008 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2009 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2011 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2013 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2015 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2016 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2017 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2018 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2019 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2022 | Py_UCS4 |
| 2023 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2024 | { |
| 2025 | enum PyUnicode_Kind kind; |
| 2026 | void *startptr, *endptr; |
| 2027 | |
| 2028 | assert(PyUnicode_IS_READY(unicode)); |
| 2029 | assert(0 <= start); |
| 2030 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2031 | assert(start <= end); |
| 2032 | |
| 2033 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2034 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2035 | |
| 2036 | if (start == end) |
| 2037 | return 127; |
| 2038 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2039 | if (PyUnicode_IS_ASCII(unicode)) |
| 2040 | return 127; |
| 2041 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2042 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2043 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2044 | endptr = (char *)startptr + end * kind; |
| 2045 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2046 | switch(kind) { |
| 2047 | case PyUnicode_1BYTE_KIND: |
| 2048 | return ucs1lib_find_max_char(startptr, endptr); |
| 2049 | case PyUnicode_2BYTE_KIND: |
| 2050 | return ucs2lib_find_max_char(startptr, endptr); |
| 2051 | case PyUnicode_4BYTE_KIND: |
| 2052 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2053 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2054 | assert(0); |
| 2055 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2059 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2060 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2061 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2062 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2063 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2064 | { |
| 2065 | PyObject *unicode, *copy; |
| 2066 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2067 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2068 | unsigned int kind; |
| 2069 | |
| 2070 | assert(p_unicode != NULL); |
| 2071 | unicode = *p_unicode; |
| 2072 | assert(PyUnicode_IS_READY(unicode)); |
| 2073 | if (PyUnicode_IS_ASCII(unicode)) |
| 2074 | return; |
| 2075 | |
| 2076 | len = PyUnicode_GET_LENGTH(unicode); |
| 2077 | kind = PyUnicode_KIND(unicode); |
| 2078 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2079 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2080 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2081 | if (max_char >= 128) |
| 2082 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2083 | } |
| 2084 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2085 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2086 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2087 | if (max_char >= 256) |
| 2088 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2089 | } |
| 2090 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2091 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2092 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2093 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2094 | if (max_char >= 0x10000) |
| 2095 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2096 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2097 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2098 | if (copy != NULL) |
| 2099 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2100 | Py_DECREF(unicode); |
| 2101 | *p_unicode = copy; |
| 2102 | } |
| 2103 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2104 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2105 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2106 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2107 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2108 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2109 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2110 | if (!PyUnicode_Check(unicode)) { |
| 2111 | PyErr_BadInternalCall(); |
| 2112 | return NULL; |
| 2113 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2114 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2115 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2116 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2117 | length = PyUnicode_GET_LENGTH(unicode); |
| 2118 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2119 | if (!copy) |
| 2120 | return NULL; |
| 2121 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2122 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2123 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2124 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2125 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2126 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2127 | } |
| 2128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2129 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2130 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2131 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2132 | |
| 2133 | void* |
| 2134 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2135 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2136 | Py_ssize_t len; |
| 2137 | void *result; |
| 2138 | unsigned int skind; |
| 2139 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2140 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2141 | return NULL; |
| 2142 | |
| 2143 | len = PyUnicode_GET_LENGTH(s); |
| 2144 | skind = PyUnicode_KIND(s); |
| 2145 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2146 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2147 | return NULL; |
| 2148 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2149 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2150 | case PyUnicode_2BYTE_KIND: |
| 2151 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2152 | if (!result) |
| 2153 | return PyErr_NoMemory(); |
| 2154 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2155 | _PyUnicode_CONVERT_BYTES( |
| 2156 | Py_UCS1, Py_UCS2, |
| 2157 | PyUnicode_1BYTE_DATA(s), |
| 2158 | PyUnicode_1BYTE_DATA(s) + len, |
| 2159 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2161 | case PyUnicode_4BYTE_KIND: |
| 2162 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2163 | if (!result) |
| 2164 | return PyErr_NoMemory(); |
| 2165 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2166 | _PyUnicode_CONVERT_BYTES( |
| 2167 | Py_UCS2, Py_UCS4, |
| 2168 | PyUnicode_2BYTE_DATA(s), |
| 2169 | PyUnicode_2BYTE_DATA(s) + len, |
| 2170 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2172 | else { |
| 2173 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2174 | _PyUnicode_CONVERT_BYTES( |
| 2175 | Py_UCS1, Py_UCS4, |
| 2176 | PyUnicode_1BYTE_DATA(s), |
| 2177 | PyUnicode_1BYTE_DATA(s) + len, |
| 2178 | result); |
| 2179 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2181 | default: |
| 2182 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2183 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2184 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2185 | return NULL; |
| 2186 | } |
| 2187 | |
| 2188 | static Py_UCS4* |
| 2189 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2190 | int copy_null) |
| 2191 | { |
| 2192 | int kind; |
| 2193 | void *data; |
| 2194 | Py_ssize_t len, targetlen; |
| 2195 | if (PyUnicode_READY(string) == -1) |
| 2196 | return NULL; |
| 2197 | kind = PyUnicode_KIND(string); |
| 2198 | data = PyUnicode_DATA(string); |
| 2199 | len = PyUnicode_GET_LENGTH(string); |
| 2200 | targetlen = len; |
| 2201 | if (copy_null) |
| 2202 | targetlen++; |
| 2203 | if (!target) { |
| 2204 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2205 | PyErr_NoMemory(); |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2209 | if (!target) { |
| 2210 | PyErr_NoMemory(); |
| 2211 | return NULL; |
| 2212 | } |
| 2213 | } |
| 2214 | else { |
| 2215 | if (targetsize < targetlen) { |
| 2216 | PyErr_Format(PyExc_SystemError, |
| 2217 | "string is longer than the buffer"); |
| 2218 | if (copy_null && 0 < targetsize) |
| 2219 | target[0] = 0; |
| 2220 | return NULL; |
| 2221 | } |
| 2222 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2223 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2224 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2225 | _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] | 2226 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2227 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2228 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2229 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2230 | } |
| 2231 | else { |
| 2232 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2233 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2234 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2235 | if (copy_null) |
| 2236 | target[len] = 0; |
| 2237 | return target; |
| 2238 | } |
| 2239 | |
| 2240 | Py_UCS4* |
| 2241 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2242 | int copy_null) |
| 2243 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2244 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2245 | PyErr_BadInternalCall(); |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | return as_ucs4(string, target, targetsize, copy_null); |
| 2249 | } |
| 2250 | |
| 2251 | Py_UCS4* |
| 2252 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2253 | { |
| 2254 | return as_ucs4(string, NULL, 0, 1); |
| 2255 | } |
| 2256 | |
| 2257 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2258 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2259 | PyObject * |
| 2260 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2261 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2264 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2265 | PyErr_BadInternalCall(); |
| 2266 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2269 | if (size == -1) { |
| 2270 | size = wcslen(w); |
| 2271 | } |
| 2272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2273 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2277 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2278 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2279 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2280 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2281 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2282 | *fmt++ = '%'; |
| 2283 | if (width) { |
| 2284 | if (zeropad) |
| 2285 | *fmt++ = '0'; |
| 2286 | fmt += sprintf(fmt, "%d", width); |
| 2287 | } |
| 2288 | if (precision) |
| 2289 | fmt += sprintf(fmt, ".%d", precision); |
| 2290 | if (longflag) |
| 2291 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2292 | else if (longlongflag) { |
| 2293 | /* longlongflag should only ever be nonzero on machines with |
| 2294 | HAVE_LONG_LONG defined */ |
| 2295 | #ifdef HAVE_LONG_LONG |
| 2296 | char *f = PY_FORMAT_LONG_LONG; |
| 2297 | while (*f) |
| 2298 | *fmt++ = *f++; |
| 2299 | #else |
| 2300 | /* we shouldn't ever get here */ |
| 2301 | assert(0); |
| 2302 | *fmt++ = 'l'; |
| 2303 | #endif |
| 2304 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2305 | else if (size_tflag) { |
| 2306 | char *f = PY_FORMAT_SIZE_T; |
| 2307 | while (*f) |
| 2308 | *fmt++ = *f++; |
| 2309 | } |
| 2310 | *fmt++ = c; |
| 2311 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2314 | /* helper for PyUnicode_FromFormatV() */ |
| 2315 | |
| 2316 | static const char* |
| 2317 | parse_format_flags(const char *f, |
| 2318 | int *p_width, int *p_precision, |
| 2319 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2320 | { |
| 2321 | int width, precision, longflag, longlongflag, size_tflag; |
| 2322 | |
| 2323 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2324 | f++; |
| 2325 | width = 0; |
| 2326 | while (Py_ISDIGIT((unsigned)*f)) |
| 2327 | width = (width*10) + *f++ - '0'; |
| 2328 | precision = 0; |
| 2329 | if (*f == '.') { |
| 2330 | f++; |
| 2331 | while (Py_ISDIGIT((unsigned)*f)) |
| 2332 | precision = (precision*10) + *f++ - '0'; |
| 2333 | if (*f == '%') { |
| 2334 | /* "%.3%s" => f points to "3" */ |
| 2335 | f--; |
| 2336 | } |
| 2337 | } |
| 2338 | if (*f == '\0') { |
| 2339 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2340 | f--; |
| 2341 | } |
| 2342 | if (p_width != NULL) |
| 2343 | *p_width = width; |
| 2344 | if (p_precision != NULL) |
| 2345 | *p_precision = precision; |
| 2346 | |
| 2347 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2348 | longflag = 0; |
| 2349 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2350 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2351 | |
| 2352 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2353 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2354 | longflag = 1; |
| 2355 | ++f; |
| 2356 | } |
| 2357 | #ifdef HAVE_LONG_LONG |
| 2358 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2359 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2360 | longlongflag = 1; |
| 2361 | f += 2; |
| 2362 | } |
| 2363 | #endif |
| 2364 | } |
| 2365 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2366 | 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] | 2367 | size_tflag = 1; |
| 2368 | ++f; |
| 2369 | } |
| 2370 | if (p_longflag != NULL) |
| 2371 | *p_longflag = longflag; |
| 2372 | if (p_longlongflag != NULL) |
| 2373 | *p_longlongflag = longlongflag; |
| 2374 | if (p_size_tflag != NULL) |
| 2375 | *p_size_tflag = size_tflag; |
| 2376 | return f; |
| 2377 | } |
| 2378 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2379 | /* maximum number of characters required for output of %ld. 21 characters |
| 2380 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2381 | #define MAX_LONG_CHARS 21 |
| 2382 | /* maximum number of characters required for output of %lld. |
| 2383 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2384 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2385 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2386 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2387 | PyObject * |
| 2388 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2389 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2390 | va_list count; |
| 2391 | Py_ssize_t callcount = 0; |
| 2392 | PyObject **callresults = NULL; |
| 2393 | PyObject **callresult = NULL; |
| 2394 | Py_ssize_t n = 0; |
| 2395 | int width = 0; |
| 2396 | int precision = 0; |
| 2397 | int zeropad; |
| 2398 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2399 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2400 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2401 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2402 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2403 | Py_UCS4 argmaxchar; |
| 2404 | Py_ssize_t numbersize = 0; |
| 2405 | char *numberresults = NULL; |
| 2406 | char *numberresult = NULL; |
| 2407 | Py_ssize_t i; |
| 2408 | int kind; |
| 2409 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2410 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2411 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2412 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2413 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2414 | * 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] | 2415 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2416 | * also estimate a upper bound for all the number formats in the string, |
| 2417 | * 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] | 2418 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2419 | for (f = format; *f; f++) { |
| 2420 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2421 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2422 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2423 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2424 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2425 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2428 | #ifdef HAVE_LONG_LONG |
| 2429 | if (longlongflag) { |
| 2430 | if (width < MAX_LONG_LONG_CHARS) |
| 2431 | width = MAX_LONG_LONG_CHARS; |
| 2432 | } |
| 2433 | else |
| 2434 | #endif |
| 2435 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2436 | including sign. Decimal takes the most space. This |
| 2437 | isn't enough for octal. If a width is specified we |
| 2438 | need more (which we allocate later). */ |
| 2439 | if (width < MAX_LONG_CHARS) |
| 2440 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2441 | |
| 2442 | /* account for the size + '\0' to separate numbers |
| 2443 | inside of the numberresults buffer */ |
| 2444 | numbersize += (width + 1); |
| 2445 | } |
| 2446 | } |
| 2447 | else if ((unsigned char)*f > 127) { |
| 2448 | PyErr_Format(PyExc_ValueError, |
| 2449 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2450 | "string, got a non-ASCII byte: 0x%02x", |
| 2451 | (unsigned char)*f); |
| 2452 | return NULL; |
| 2453 | } |
| 2454 | } |
| 2455 | /* step 2: allocate memory for the results of |
| 2456 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2457 | if (callcount) { |
| 2458 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2459 | if (!callresults) { |
| 2460 | PyErr_NoMemory(); |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | callresult = callresults; |
| 2464 | } |
| 2465 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2466 | if (numbersize) { |
| 2467 | numberresults = PyObject_Malloc(numbersize); |
| 2468 | if (!numberresults) { |
| 2469 | PyErr_NoMemory(); |
| 2470 | goto fail; |
| 2471 | } |
| 2472 | numberresult = numberresults; |
| 2473 | } |
| 2474 | |
| 2475 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2476 | for (f = format; *f; f++) { |
| 2477 | if (*f == '%') { |
| 2478 | const char* p; |
| 2479 | int longflag; |
| 2480 | int longlongflag; |
| 2481 | int size_tflag; |
| 2482 | int numprinted; |
| 2483 | |
| 2484 | p = f; |
| 2485 | zeropad = (f[1] == '0'); |
| 2486 | f = parse_format_flags(f, &width, &precision, |
| 2487 | &longflag, &longlongflag, &size_tflag); |
| 2488 | switch (*f) { |
| 2489 | case 'c': |
| 2490 | { |
Serhiy Storchaka | 8eeae21 | 2013-06-23 20:12:14 +0300 | [diff] [blame] | 2491 | int ordinal = va_arg(count, int); |
| 2492 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2493 | PyErr_SetString(PyExc_OverflowError, |
| 2494 | "%c arg not in range(0x110000)"); |
| 2495 | goto fail; |
| 2496 | } |
| 2497 | maxchar = Py_MAX(maxchar, (Py_UCS4)ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2498 | n++; |
| 2499 | break; |
| 2500 | } |
| 2501 | case '%': |
| 2502 | n++; |
| 2503 | break; |
| 2504 | case 'i': |
| 2505 | case 'd': |
| 2506 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2507 | width, precision, *f); |
| 2508 | if (longflag) |
| 2509 | numprinted = sprintf(numberresult, fmt, |
| 2510 | va_arg(count, long)); |
| 2511 | #ifdef HAVE_LONG_LONG |
| 2512 | else if (longlongflag) |
| 2513 | numprinted = sprintf(numberresult, fmt, |
| 2514 | va_arg(count, PY_LONG_LONG)); |
| 2515 | #endif |
| 2516 | else if (size_tflag) |
| 2517 | numprinted = sprintf(numberresult, fmt, |
| 2518 | va_arg(count, Py_ssize_t)); |
| 2519 | else |
| 2520 | numprinted = sprintf(numberresult, fmt, |
| 2521 | va_arg(count, int)); |
| 2522 | n += numprinted; |
| 2523 | /* advance by +1 to skip over the '\0' */ |
| 2524 | numberresult += (numprinted + 1); |
| 2525 | assert(*(numberresult - 1) == '\0'); |
| 2526 | assert(*(numberresult - 2) != '\0'); |
| 2527 | assert(numprinted >= 0); |
| 2528 | assert(numberresult <= numberresults + numbersize); |
| 2529 | break; |
| 2530 | case 'u': |
| 2531 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2532 | width, precision, 'u'); |
| 2533 | if (longflag) |
| 2534 | numprinted = sprintf(numberresult, fmt, |
| 2535 | va_arg(count, unsigned long)); |
| 2536 | #ifdef HAVE_LONG_LONG |
| 2537 | else if (longlongflag) |
| 2538 | numprinted = sprintf(numberresult, fmt, |
| 2539 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2540 | #endif |
| 2541 | else if (size_tflag) |
| 2542 | numprinted = sprintf(numberresult, fmt, |
| 2543 | va_arg(count, size_t)); |
| 2544 | else |
| 2545 | numprinted = sprintf(numberresult, fmt, |
| 2546 | va_arg(count, unsigned int)); |
| 2547 | n += numprinted; |
| 2548 | numberresult += (numprinted + 1); |
| 2549 | assert(*(numberresult - 1) == '\0'); |
| 2550 | assert(*(numberresult - 2) != '\0'); |
| 2551 | assert(numprinted >= 0); |
| 2552 | assert(numberresult <= numberresults + numbersize); |
| 2553 | break; |
| 2554 | case 'x': |
| 2555 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2556 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2557 | n += numprinted; |
| 2558 | numberresult += (numprinted + 1); |
| 2559 | assert(*(numberresult - 1) == '\0'); |
| 2560 | assert(*(numberresult - 2) != '\0'); |
| 2561 | assert(numprinted >= 0); |
| 2562 | assert(numberresult <= numberresults + numbersize); |
| 2563 | break; |
| 2564 | case 'p': |
| 2565 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2566 | /* %p is ill-defined: ensure leading 0x. */ |
| 2567 | if (numberresult[1] == 'X') |
| 2568 | numberresult[1] = 'x'; |
| 2569 | else if (numberresult[1] != 'x') { |
| 2570 | memmove(numberresult + 2, numberresult, |
| 2571 | strlen(numberresult) + 1); |
| 2572 | numberresult[0] = '0'; |
| 2573 | numberresult[1] = 'x'; |
| 2574 | numprinted += 2; |
| 2575 | } |
| 2576 | n += numprinted; |
| 2577 | numberresult += (numprinted + 1); |
| 2578 | assert(*(numberresult - 1) == '\0'); |
| 2579 | assert(*(numberresult - 2) != '\0'); |
| 2580 | assert(numprinted >= 0); |
| 2581 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | break; |
| 2583 | case 's': |
| 2584 | { |
| 2585 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2586 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2587 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2588 | if (!str) |
| 2589 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2591 | unicode objects, there is no need to call ready on them */ |
| 2592 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2593 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2594 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2595 | /* Remember the str and switch to the next slot */ |
| 2596 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2597 | break; |
| 2598 | } |
| 2599 | case 'U': |
| 2600 | { |
| 2601 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2602 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2603 | if (PyUnicode_READY(obj) == -1) |
| 2604 | goto fail; |
| 2605 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2606 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2607 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2608 | break; |
| 2609 | } |
| 2610 | case 'V': |
| 2611 | { |
| 2612 | PyObject *obj = va_arg(count, PyObject *); |
| 2613 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2614 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2615 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2616 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2617 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | if (PyUnicode_READY(obj) == -1) |
| 2619 | goto fail; |
| 2620 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2621 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2622 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2623 | *callresult++ = NULL; |
| 2624 | } |
| 2625 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2626 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2627 | if (!str_obj) |
| 2628 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2629 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2630 | Py_DECREF(str_obj); |
| 2631 | goto fail; |
| 2632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2633 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2634 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2636 | *callresult++ = str_obj; |
| 2637 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | } |
| 2640 | case 'S': |
| 2641 | { |
| 2642 | PyObject *obj = va_arg(count, PyObject *); |
| 2643 | PyObject *str; |
| 2644 | assert(obj); |
| 2645 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2646 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2647 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2648 | if (PyUnicode_READY(str) == -1) { |
| 2649 | Py_DECREF(str); |
| 2650 | goto fail; |
| 2651 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2652 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2653 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2655 | /* Remember the str and switch to the next slot */ |
| 2656 | *callresult++ = str; |
| 2657 | break; |
| 2658 | } |
| 2659 | case 'R': |
| 2660 | { |
| 2661 | PyObject *obj = va_arg(count, PyObject *); |
| 2662 | PyObject *repr; |
| 2663 | assert(obj); |
| 2664 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2665 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2666 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2667 | if (PyUnicode_READY(repr) == -1) { |
| 2668 | Py_DECREF(repr); |
| 2669 | goto fail; |
| 2670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2671 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2672 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2674 | /* Remember the repr and switch to the next slot */ |
| 2675 | *callresult++ = repr; |
| 2676 | break; |
| 2677 | } |
| 2678 | case 'A': |
| 2679 | { |
| 2680 | PyObject *obj = va_arg(count, PyObject *); |
| 2681 | PyObject *ascii; |
| 2682 | assert(obj); |
| 2683 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2684 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2685 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2686 | if (PyUnicode_READY(ascii) == -1) { |
| 2687 | Py_DECREF(ascii); |
| 2688 | goto fail; |
| 2689 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2690 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 2691 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2692 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2693 | /* Remember the repr and switch to the next slot */ |
| 2694 | *callresult++ = ascii; |
| 2695 | break; |
| 2696 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2697 | default: |
| 2698 | /* if we stumble upon an unknown |
| 2699 | formatting code, copy the rest of |
| 2700 | the format string to the output |
| 2701 | string. (we cannot just skip the |
| 2702 | code, since there's no way to know |
| 2703 | what's in the argument list) */ |
| 2704 | n += strlen(p); |
| 2705 | goto expand; |
| 2706 | } |
| 2707 | } else |
| 2708 | n++; |
| 2709 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2710 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2711 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2712 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2713 | we don't have to resize the string. |
| 2714 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2715 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2716 | if (!string) |
| 2717 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2718 | kind = PyUnicode_KIND(string); |
| 2719 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2720 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2721 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2722 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2724 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2725 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2726 | |
| 2727 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2728 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2729 | /* checking for == because the last argument could be a empty |
| 2730 | string, which causes i to point to end, the assert at the end of |
| 2731 | the loop */ |
| 2732 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2733 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2734 | switch (*f) { |
| 2735 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2736 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2737 | const int ordinal = va_arg(vargs, int); |
| 2738 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2739 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2740 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2741 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2742 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2743 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2744 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2745 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2746 | { |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2747 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2748 | /* unused, since we already have the result */ |
| 2749 | if (*f == 'p') |
| 2750 | (void) va_arg(vargs, void *); |
| 2751 | else |
| 2752 | (void) va_arg(vargs, int); |
| 2753 | /* extract the result from numberresults and append. */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2754 | len = strlen(numberresult); |
| 2755 | unicode_write_cstr(string, i, numberresult, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2756 | /* skip over the separating '\0' */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2757 | i += len; |
| 2758 | numberresult += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2759 | assert(*numberresult == '\0'); |
| 2760 | numberresult++; |
| 2761 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2762 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2763 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2764 | case 's': |
| 2765 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2766 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2767 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2768 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2769 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2770 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2771 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2772 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2773 | /* We're done with the unicode()/repr() => forget it */ |
| 2774 | Py_DECREF(*callresult); |
| 2775 | /* switch to next unicode()/repr() result */ |
| 2776 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 | break; |
| 2778 | } |
| 2779 | case 'U': |
| 2780 | { |
| 2781 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2782 | Py_ssize_t size; |
| 2783 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2784 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2785 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2786 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2787 | break; |
| 2788 | } |
| 2789 | case 'V': |
| 2790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2791 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2792 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2793 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2794 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2795 | size = PyUnicode_GET_LENGTH(obj); |
| 2796 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2797 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2798 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2799 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2801 | assert(PyUnicode_KIND(*callresult) <= |
| 2802 | PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2803 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2804 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2805 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2806 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2807 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2808 | break; |
| 2809 | } |
| 2810 | case 'S': |
| 2811 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2812 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2813 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2814 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2815 | /* unused, since we already have the result */ |
| 2816 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2817 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2818 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2819 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2820 | /* We're done with the unicode()/repr() => forget it */ |
| 2821 | Py_DECREF(*callresult); |
| 2822 | /* switch to next unicode()/repr() result */ |
| 2823 | ++callresult; |
| 2824 | break; |
| 2825 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2826 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2828 | break; |
| 2829 | default: |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2830 | { |
| 2831 | Py_ssize_t len = strlen(p); |
| 2832 | unicode_write_cstr(string, i, p, len); |
| 2833 | i += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2834 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2835 | goto end; |
| 2836 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2837 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2838 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 | else { |
| 2840 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2841 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2842 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2844 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2845 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2846 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2847 | if (callresults) |
| 2848 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2849 | if (numberresults) |
| 2850 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2851 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2852 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2853 | if (callresults) { |
| 2854 | PyObject **callresult2 = callresults; |
| 2855 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2856 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2857 | ++callresult2; |
| 2858 | } |
| 2859 | PyObject_Free(callresults); |
| 2860 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2861 | if (numberresults) |
| 2862 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2863 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2866 | PyObject * |
| 2867 | PyUnicode_FromFormat(const char *format, ...) |
| 2868 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2869 | PyObject* ret; |
| 2870 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2871 | |
| 2872 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2873 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2874 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2875 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2876 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2877 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2878 | va_end(vargs); |
| 2879 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2882 | #ifdef HAVE_WCHAR_H |
| 2883 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2884 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2885 | convert a Unicode object to a wide character string. |
| 2886 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2887 | - 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] | 2888 | character) required to convert the unicode object. Ignore size argument. |
| 2889 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2890 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2891 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2892 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2893 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2894 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2895 | wchar_t *w, |
| 2896 | Py_ssize_t size) |
| 2897 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2898 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2899 | const wchar_t *wstr; |
| 2900 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2901 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2902 | if (wstr == NULL) |
| 2903 | return -1; |
| 2904 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2905 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2906 | if (size > res) |
| 2907 | size = res + 1; |
| 2908 | else |
| 2909 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2910 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2911 | return res; |
| 2912 | } |
| 2913 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2914 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2918 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2919 | wchar_t *w, |
| 2920 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | { |
| 2922 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2923 | PyErr_BadInternalCall(); |
| 2924 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2925 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2926 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2929 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2930 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2931 | Py_ssize_t *size) |
| 2932 | { |
| 2933 | wchar_t* buffer; |
| 2934 | Py_ssize_t buflen; |
| 2935 | |
| 2936 | if (unicode == NULL) { |
| 2937 | PyErr_BadInternalCall(); |
| 2938 | return NULL; |
| 2939 | } |
| 2940 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2941 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2942 | if (buflen == -1) |
| 2943 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2944 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2945 | PyErr_NoMemory(); |
| 2946 | return NULL; |
| 2947 | } |
| 2948 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2949 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2950 | if (buffer == NULL) { |
| 2951 | PyErr_NoMemory(); |
| 2952 | return NULL; |
| 2953 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2954 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2955 | if (buflen == -1) { |
| 2956 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2957 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2958 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2959 | if (size != NULL) |
| 2960 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2961 | return buffer; |
| 2962 | } |
| 2963 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2964 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2966 | PyObject * |
| 2967 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2968 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2969 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2970 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2971 | PyErr_SetString(PyExc_ValueError, |
| 2972 | "chr() arg not in range(0x110000)"); |
| 2973 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2974 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2975 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2976 | if ((Py_UCS4)ordinal < 256) |
| 2977 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2979 | v = PyUnicode_New(1, ordinal); |
| 2980 | if (v == NULL) |
| 2981 | return NULL; |
| 2982 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2983 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2984 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2987 | PyObject * |
| 2988 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2989 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2990 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2991 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2992 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2993 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2994 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2995 | Py_INCREF(obj); |
| 2996 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2997 | } |
| 2998 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2999 | /* For a Unicode subtype that's not a Unicode object, |
| 3000 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 3001 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3002 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3003 | PyErr_Format(PyExc_TypeError, |
| 3004 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3005 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3006 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3009 | PyObject * |
| 3010 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3011 | const char *encoding, |
| 3012 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3013 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3014 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3015 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3018 | PyErr_BadInternalCall(); |
| 3019 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3022 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3023 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3024 | if (PyBytes_GET_SIZE(obj) == 0) |
| 3025 | _Py_RETURN_UNICODE_EMPTY(); |
| 3026 | v = PyUnicode_Decode( |
| 3027 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3028 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3029 | return v; |
| 3030 | } |
| 3031 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3032 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3033 | PyErr_SetString(PyExc_TypeError, |
| 3034 | "decoding str is not supported"); |
| 3035 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3036 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3037 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3038 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3039 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3040 | PyErr_Format(PyExc_TypeError, |
| 3041 | "coercing to str: need bytes, bytearray " |
| 3042 | "or buffer-like object, %.80s found", |
| 3043 | Py_TYPE(obj)->tp_name); |
| 3044 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3045 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3046 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3047 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3048 | PyBuffer_Release(&buffer); |
| 3049 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3051 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3052 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3053 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3054 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3057 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3058 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3059 | 1 on success. */ |
Victor Stinner | 20b654a | 2013-01-03 01:08:58 +0100 | [diff] [blame] | 3060 | int |
| 3061 | _Py_normalize_encoding(const char *encoding, |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3062 | char *lower, |
| 3063 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3064 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3065 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3066 | char *l; |
| 3067 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3068 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3069 | if (encoding == NULL) { |
| 3070 | strcpy(lower, "utf-8"); |
| 3071 | return 1; |
| 3072 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3073 | e = encoding; |
| 3074 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3075 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3076 | while (*e) { |
| 3077 | if (l == l_end) |
| 3078 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3079 | if (Py_ISUPPER(*e)) { |
| 3080 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3081 | } |
| 3082 | else if (*e == '_') { |
| 3083 | *l++ = '-'; |
| 3084 | e++; |
| 3085 | } |
| 3086 | else { |
| 3087 | *l++ = *e++; |
| 3088 | } |
| 3089 | } |
| 3090 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3091 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3094 | PyObject * |
| 3095 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3096 | Py_ssize_t size, |
| 3097 | const char *encoding, |
| 3098 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3099 | { |
| 3100 | PyObject *buffer = NULL, *unicode; |
| 3101 | Py_buffer info; |
| 3102 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3103 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3104 | /* Shortcuts for common default encodings */ |
Victor Stinner | 20b654a | 2013-01-03 01:08:58 +0100 | [diff] [blame] | 3105 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3106 | if ((strcmp(lower, "utf-8") == 0) || |
| 3107 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3108 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3109 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3110 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3111 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3112 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3113 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3114 | else if (strcmp(lower, "mbcs") == 0) |
| 3115 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3116 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3117 | else if (strcmp(lower, "ascii") == 0) |
| 3118 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3119 | else if (strcmp(lower, "utf-16") == 0) |
| 3120 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3121 | else if (strcmp(lower, "utf-32") == 0) |
| 3122 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3123 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3124 | |
| 3125 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3126 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3127 | 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] | 3128 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3129 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3130 | if (buffer == NULL) |
| 3131 | goto onError; |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 3132 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3133 | if (unicode == NULL) |
| 3134 | goto onError; |
| 3135 | if (!PyUnicode_Check(unicode)) { |
| 3136 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3137 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3138 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3139 | Py_DECREF(unicode); |
| 3140 | goto onError; |
| 3141 | } |
| 3142 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3143 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3144 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | Py_XDECREF(buffer); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3150 | PyObject * |
| 3151 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3152 | const char *encoding, |
| 3153 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3154 | { |
| 3155 | PyObject *v; |
| 3156 | |
| 3157 | if (!PyUnicode_Check(unicode)) { |
| 3158 | PyErr_BadArgument(); |
| 3159 | goto onError; |
| 3160 | } |
| 3161 | |
| 3162 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3163 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* Decode via the codec registry */ |
| 3166 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3167 | if (v == NULL) |
| 3168 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3169 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3170 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3171 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3172 | return NULL; |
| 3173 | } |
| 3174 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3175 | PyObject * |
| 3176 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3177 | const char *encoding, |
| 3178 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3179 | { |
| 3180 | PyObject *v; |
| 3181 | |
| 3182 | if (!PyUnicode_Check(unicode)) { |
| 3183 | PyErr_BadArgument(); |
| 3184 | goto onError; |
| 3185 | } |
| 3186 | |
| 3187 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3188 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3189 | |
| 3190 | /* Decode via the codec registry */ |
| 3191 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3192 | if (v == NULL) |
| 3193 | goto onError; |
| 3194 | if (!PyUnicode_Check(v)) { |
| 3195 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3196 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3197 | Py_TYPE(v)->tp_name); |
| 3198 | Py_DECREF(v); |
| 3199 | goto onError; |
| 3200 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3201 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3203 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3204 | return NULL; |
| 3205 | } |
| 3206 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3207 | PyObject * |
| 3208 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3209 | Py_ssize_t size, |
| 3210 | const char *encoding, |
| 3211 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3212 | { |
| 3213 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | unicode = PyUnicode_FromUnicode(s, size); |
| 3216 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3217 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3218 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3219 | Py_DECREF(unicode); |
| 3220 | return v; |
| 3221 | } |
| 3222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3223 | PyObject * |
| 3224 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3225 | const char *encoding, |
| 3226 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3227 | { |
| 3228 | PyObject *v; |
| 3229 | |
| 3230 | if (!PyUnicode_Check(unicode)) { |
| 3231 | PyErr_BadArgument(); |
| 3232 | goto onError; |
| 3233 | } |
| 3234 | |
| 3235 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3236 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* Encode via the codec registry */ |
| 3239 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3240 | if (v == NULL) |
| 3241 | goto onError; |
| 3242 | return v; |
| 3243 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3244 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3245 | return NULL; |
| 3246 | } |
| 3247 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3248 | static size_t |
| 3249 | wcstombs_errorpos(const wchar_t *wstr) |
| 3250 | { |
| 3251 | size_t len; |
| 3252 | #if SIZEOF_WCHAR_T == 2 |
| 3253 | wchar_t buf[3]; |
| 3254 | #else |
| 3255 | wchar_t buf[2]; |
| 3256 | #endif |
| 3257 | char outbuf[MB_LEN_MAX]; |
| 3258 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3259 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3260 | #if SIZEOF_WCHAR_T == 2 |
| 3261 | buf[2] = 0; |
| 3262 | #else |
| 3263 | buf[1] = 0; |
| 3264 | #endif |
| 3265 | start = wstr; |
| 3266 | while (*wstr != L'\0') |
| 3267 | { |
| 3268 | previous = wstr; |
| 3269 | #if SIZEOF_WCHAR_T == 2 |
| 3270 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3271 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3272 | { |
| 3273 | buf[0] = wstr[0]; |
| 3274 | buf[1] = wstr[1]; |
| 3275 | wstr += 2; |
| 3276 | } |
| 3277 | else { |
| 3278 | buf[0] = *wstr; |
| 3279 | buf[1] = 0; |
| 3280 | wstr++; |
| 3281 | } |
| 3282 | #else |
| 3283 | buf[0] = *wstr; |
| 3284 | wstr++; |
| 3285 | #endif |
| 3286 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3287 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3288 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
| 3294 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3295 | static int |
| 3296 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3297 | { |
| 3298 | if (errors == NULL) { |
| 3299 | *surrogateescape = 0; |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | if (strcmp(errors, "strict") == 0) { |
| 3304 | *surrogateescape = 0; |
| 3305 | return 0; |
| 3306 | } |
| 3307 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3308 | *surrogateescape = 1; |
| 3309 | return 0; |
| 3310 | } |
| 3311 | PyErr_Format(PyExc_ValueError, |
| 3312 | "only 'strict' and 'surrogateescape' error handlers " |
| 3313 | "are supported, not '%s'", |
| 3314 | errors); |
| 3315 | return -1; |
| 3316 | } |
| 3317 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3318 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3319 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3320 | { |
| 3321 | Py_ssize_t wlen, wlen2; |
| 3322 | wchar_t *wstr; |
| 3323 | PyObject *bytes = NULL; |
| 3324 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3325 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3326 | PyObject *exc; |
| 3327 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3328 | int surrogateescape; |
| 3329 | |
| 3330 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3331 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3332 | |
| 3333 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3334 | if (wstr == NULL) |
| 3335 | return NULL; |
| 3336 | |
| 3337 | wlen2 = wcslen(wstr); |
| 3338 | if (wlen2 != wlen) { |
| 3339 | PyMem_Free(wstr); |
| 3340 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3341 | return NULL; |
| 3342 | } |
| 3343 | |
| 3344 | if (surrogateescape) { |
| 3345 | /* locale encoding with surrogateescape */ |
| 3346 | char *str; |
| 3347 | |
| 3348 | str = _Py_wchar2char(wstr, &error_pos); |
| 3349 | if (str == NULL) { |
| 3350 | if (error_pos == (size_t)-1) { |
| 3351 | PyErr_NoMemory(); |
| 3352 | PyMem_Free(wstr); |
| 3353 | return NULL; |
| 3354 | } |
| 3355 | else { |
| 3356 | goto encode_error; |
| 3357 | } |
| 3358 | } |
| 3359 | PyMem_Free(wstr); |
| 3360 | |
| 3361 | bytes = PyBytes_FromString(str); |
| 3362 | PyMem_Free(str); |
| 3363 | } |
| 3364 | else { |
| 3365 | size_t len, len2; |
| 3366 | |
| 3367 | len = wcstombs(NULL, wstr, 0); |
| 3368 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3369 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3370 | goto encode_error; |
| 3371 | } |
| 3372 | |
| 3373 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3374 | if (bytes == NULL) { |
| 3375 | PyMem_Free(wstr); |
| 3376 | return NULL; |
| 3377 | } |
| 3378 | |
| 3379 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3380 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3381 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3382 | goto encode_error; |
| 3383 | } |
| 3384 | PyMem_Free(wstr); |
| 3385 | } |
| 3386 | return bytes; |
| 3387 | |
| 3388 | encode_error: |
| 3389 | errmsg = strerror(errno); |
| 3390 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3391 | |
| 3392 | if (error_pos == (size_t)-1) |
| 3393 | error_pos = wcstombs_errorpos(wstr); |
| 3394 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3395 | PyMem_Free(wstr); |
| 3396 | Py_XDECREF(bytes); |
| 3397 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3398 | if (errmsg != NULL) { |
| 3399 | size_t errlen; |
| 3400 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3401 | if (wstr != NULL) { |
| 3402 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3403 | PyMem_Free(wstr); |
| 3404 | } else |
| 3405 | errmsg = NULL; |
| 3406 | } |
| 3407 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3408 | reason = PyUnicode_FromString( |
| 3409 | "wcstombs() encountered an unencodable " |
| 3410 | "wide character"); |
| 3411 | if (reason == NULL) |
| 3412 | return NULL; |
| 3413 | |
| 3414 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3415 | "locale", unicode, |
| 3416 | (Py_ssize_t)error_pos, |
| 3417 | (Py_ssize_t)(error_pos+1), |
| 3418 | reason); |
| 3419 | Py_DECREF(reason); |
| 3420 | if (exc != NULL) { |
| 3421 | PyCodec_StrictErrors(exc); |
| 3422 | Py_XDECREF(exc); |
| 3423 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3424 | return NULL; |
| 3425 | } |
| 3426 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3427 | PyObject * |
| 3428 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3429 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3430 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3431 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3432 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3433 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3434 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3435 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3436 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3437 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3438 | the Python codec requires to encode at least its own filename. Use the C |
| 3439 | version of the locale codec until the codec registry is initialized and |
| 3440 | the Python codec is loaded. |
| 3441 | |
| 3442 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3443 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3444 | subinterpreters. */ |
| 3445 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3446 | return PyUnicode_AsEncodedString(unicode, |
| 3447 | Py_FileSystemDefaultEncoding, |
| 3448 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3449 | } |
| 3450 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3451 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3452 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3453 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3456 | PyObject * |
| 3457 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3458 | const char *encoding, |
| 3459 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3460 | { |
| 3461 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3462 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3464 | if (!PyUnicode_Check(unicode)) { |
| 3465 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3466 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3468 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3469 | /* Shortcuts for common default encodings */ |
Victor Stinner | 20b654a | 2013-01-03 01:08:58 +0100 | [diff] [blame] | 3470 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3471 | if ((strcmp(lower, "utf-8") == 0) || |
| 3472 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3473 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3474 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3475 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3476 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3477 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3478 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3479 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3480 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3481 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3482 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3483 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3484 | else if (strcmp(lower, "mbcs") == 0) |
| 3485 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3486 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3487 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3488 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3489 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3490 | |
| 3491 | /* Encode via the codec registry */ |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 3492 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3493 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3494 | return NULL; |
| 3495 | |
| 3496 | /* The normal path */ |
| 3497 | if (PyBytes_Check(v)) |
| 3498 | return v; |
| 3499 | |
| 3500 | /* 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] | 3501 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3502 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3503 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3504 | |
| 3505 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3506 | "encoder %s returned bytearray instead of bytes", |
| 3507 | encoding); |
| 3508 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3509 | Py_DECREF(v); |
| 3510 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3511 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3512 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3513 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3514 | Py_DECREF(v); |
| 3515 | return b; |
| 3516 | } |
| 3517 | |
| 3518 | PyErr_Format(PyExc_TypeError, |
| 3519 | "encoder did not return a bytes object (type=%.400s)", |
| 3520 | Py_TYPE(v)->tp_name); |
| 3521 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3522 | return NULL; |
| 3523 | } |
| 3524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3525 | PyObject * |
| 3526 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3527 | const char *encoding, |
| 3528 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3529 | { |
| 3530 | PyObject *v; |
| 3531 | |
| 3532 | if (!PyUnicode_Check(unicode)) { |
| 3533 | PyErr_BadArgument(); |
| 3534 | goto onError; |
| 3535 | } |
| 3536 | |
| 3537 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3538 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3539 | |
| 3540 | /* Encode via the codec registry */ |
| 3541 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3542 | if (v == NULL) |
| 3543 | goto onError; |
| 3544 | if (!PyUnicode_Check(v)) { |
| 3545 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3546 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3547 | Py_TYPE(v)->tp_name); |
| 3548 | Py_DECREF(v); |
| 3549 | goto onError; |
| 3550 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3551 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3552 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3553 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | return NULL; |
| 3555 | } |
| 3556 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3557 | static size_t |
| 3558 | mbstowcs_errorpos(const char *str, size_t len) |
| 3559 | { |
| 3560 | #ifdef HAVE_MBRTOWC |
| 3561 | const char *start = str; |
| 3562 | mbstate_t mbs; |
| 3563 | size_t converted; |
| 3564 | wchar_t ch; |
| 3565 | |
| 3566 | memset(&mbs, 0, sizeof mbs); |
| 3567 | while (len) |
| 3568 | { |
| 3569 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3570 | if (converted == 0) |
| 3571 | /* Reached end of string */ |
| 3572 | break; |
| 3573 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3574 | /* Conversion error or incomplete character */ |
| 3575 | return str - start; |
| 3576 | } |
| 3577 | else { |
| 3578 | str += converted; |
| 3579 | len -= converted; |
| 3580 | } |
| 3581 | } |
| 3582 | /* failed to find the undecodable byte sequence */ |
| 3583 | return 0; |
| 3584 | #endif |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3588 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3589 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3590 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3591 | { |
| 3592 | wchar_t smallbuf[256]; |
| 3593 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3594 | wchar_t *wstr; |
| 3595 | size_t wlen, wlen2; |
| 3596 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3597 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3598 | size_t error_pos; |
| 3599 | char *errmsg; |
| 3600 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3601 | |
| 3602 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3603 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3604 | |
| 3605 | if (str[len] != '\0' || len != strlen(str)) { |
| 3606 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3607 | return NULL; |
| 3608 | } |
| 3609 | |
| 3610 | if (surrogateescape) |
| 3611 | { |
| 3612 | wstr = _Py_char2wchar(str, &wlen); |
| 3613 | if (wstr == NULL) { |
| 3614 | if (wlen == (size_t)-1) |
| 3615 | PyErr_NoMemory(); |
| 3616 | else |
| 3617 | PyErr_SetFromErrno(PyExc_OSError); |
| 3618 | return NULL; |
| 3619 | } |
| 3620 | |
| 3621 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3622 | PyMem_Free(wstr); |
| 3623 | } |
| 3624 | else { |
| 3625 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3626 | wlen = mbstowcs(NULL, str, 0); |
| 3627 | #else |
| 3628 | wlen = len; |
| 3629 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3630 | if (wlen == (size_t)-1) |
| 3631 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3632 | if (wlen+1 <= smallbuf_len) { |
| 3633 | wstr = smallbuf; |
| 3634 | } |
| 3635 | else { |
| 3636 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3637 | return PyErr_NoMemory(); |
| 3638 | |
| 3639 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3640 | if (!wstr) |
| 3641 | return PyErr_NoMemory(); |
| 3642 | } |
| 3643 | |
| 3644 | /* This shouldn't fail now */ |
| 3645 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3646 | if (wlen2 == (size_t)-1) { |
| 3647 | if (wstr != smallbuf) |
| 3648 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3649 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3650 | } |
| 3651 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3652 | assert(wlen2 == wlen); |
| 3653 | #endif |
| 3654 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3655 | if (wstr != smallbuf) |
| 3656 | PyMem_Free(wstr); |
| 3657 | } |
| 3658 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3659 | |
| 3660 | decode_error: |
| 3661 | errmsg = strerror(errno); |
| 3662 | assert(errmsg != NULL); |
| 3663 | |
| 3664 | error_pos = mbstowcs_errorpos(str, len); |
| 3665 | if (errmsg != NULL) { |
| 3666 | size_t errlen; |
| 3667 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3668 | if (wstr != NULL) { |
| 3669 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3670 | PyMem_Free(wstr); |
| 3671 | } else |
| 3672 | errmsg = NULL; |
| 3673 | } |
| 3674 | if (errmsg == NULL) |
| 3675 | reason = PyUnicode_FromString( |
| 3676 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3677 | if (reason == NULL) |
| 3678 | return NULL; |
| 3679 | |
| 3680 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3681 | "locale", str, len, |
| 3682 | (Py_ssize_t)error_pos, |
| 3683 | (Py_ssize_t)(error_pos+1), |
| 3684 | reason); |
| 3685 | Py_DECREF(reason); |
| 3686 | if (exc != NULL) { |
| 3687 | PyCodec_StrictErrors(exc); |
| 3688 | Py_XDECREF(exc); |
| 3689 | } |
| 3690 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3694 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3695 | { |
| 3696 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3697 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | |
| 3701 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3702 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3703 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3704 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3705 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3706 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3707 | PyObject* |
| 3708 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3709 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3710 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3711 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3712 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3713 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3714 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3715 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3716 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3717 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3718 | the Python codec requires to encode at least its own filename. Use the C |
| 3719 | version of the locale codec until the codec registry is initialized and |
| 3720 | the Python codec is loaded. |
| 3721 | |
| 3722 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3723 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3724 | subinterpreters. */ |
| 3725 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3726 | return PyUnicode_Decode(s, size, |
| 3727 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3728 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3729 | } |
| 3730 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3731 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3732 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3733 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3736 | |
| 3737 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3738 | _PyUnicode_HasNULChars(PyObject* s) |
| 3739 | { |
| 3740 | static PyObject *nul = NULL; |
| 3741 | |
| 3742 | if (nul == NULL) |
| 3743 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3744 | if (nul == NULL) |
| 3745 | return -1; |
| 3746 | return PyUnicode_Contains(s, nul); |
| 3747 | } |
| 3748 | |
| 3749 | |
| 3750 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3751 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3752 | { |
| 3753 | PyObject *output = NULL; |
| 3754 | Py_ssize_t size; |
| 3755 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3756 | if (arg == NULL) { |
| 3757 | Py_DECREF(*(PyObject**)addr); |
| 3758 | return 1; |
| 3759 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3760 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3761 | output = arg; |
| 3762 | Py_INCREF(output); |
| 3763 | } |
| 3764 | else { |
| 3765 | arg = PyUnicode_FromObject(arg); |
| 3766 | if (!arg) |
| 3767 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3768 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3769 | Py_DECREF(arg); |
| 3770 | if (!output) |
| 3771 | return 0; |
| 3772 | if (!PyBytes_Check(output)) { |
| 3773 | Py_DECREF(output); |
| 3774 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3775 | return 0; |
| 3776 | } |
| 3777 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3778 | size = PyBytes_GET_SIZE(output); |
| 3779 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3780 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3781 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3782 | Py_DECREF(output); |
| 3783 | return 0; |
| 3784 | } |
| 3785 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3786 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3790 | int |
| 3791 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3792 | { |
| 3793 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3794 | if (arg == NULL) { |
| 3795 | Py_DECREF(*(PyObject**)addr); |
| 3796 | return 1; |
| 3797 | } |
| 3798 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3799 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3800 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3801 | output = arg; |
| 3802 | Py_INCREF(output); |
| 3803 | } |
| 3804 | else { |
| 3805 | arg = PyBytes_FromObject(arg); |
| 3806 | if (!arg) |
| 3807 | return 0; |
| 3808 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3809 | PyBytes_GET_SIZE(arg)); |
| 3810 | Py_DECREF(arg); |
| 3811 | if (!output) |
| 3812 | return 0; |
| 3813 | if (!PyUnicode_Check(output)) { |
| 3814 | Py_DECREF(output); |
| 3815 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3816 | return 0; |
| 3817 | } |
| 3818 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3819 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3820 | Py_DECREF(output); |
| 3821 | return 0; |
| 3822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3823 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3824 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3825 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3826 | Py_DECREF(output); |
| 3827 | return 0; |
| 3828 | } |
| 3829 | *(PyObject**)addr = output; |
| 3830 | return Py_CLEANUP_SUPPORTED; |
| 3831 | } |
| 3832 | |
| 3833 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3834 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3835 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3836 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3837 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3838 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3839 | if (!PyUnicode_Check(unicode)) { |
| 3840 | PyErr_BadArgument(); |
| 3841 | return NULL; |
| 3842 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3843 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3844 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3845 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3846 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3847 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3848 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3849 | if (bytes == NULL) |
| 3850 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3851 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3852 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3853 | Py_DECREF(bytes); |
| 3854 | return NULL; |
| 3855 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3856 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3857 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3858 | PyBytes_AS_STRING(bytes), |
| 3859 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | Py_DECREF(bytes); |
| 3861 | } |
| 3862 | |
| 3863 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3864 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3865 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3869 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3870 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3872 | } |
| 3873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | Py_UNICODE * |
| 3875 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3876 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | const unsigned char *one_byte; |
| 3878 | #if SIZEOF_WCHAR_T == 4 |
| 3879 | const Py_UCS2 *two_bytes; |
| 3880 | #else |
| 3881 | const Py_UCS4 *four_bytes; |
| 3882 | const Py_UCS4 *ucs4_end; |
| 3883 | Py_ssize_t num_surrogates; |
| 3884 | #endif |
| 3885 | wchar_t *w; |
| 3886 | wchar_t *wchar_end; |
| 3887 | |
| 3888 | if (!PyUnicode_Check(unicode)) { |
| 3889 | PyErr_BadArgument(); |
| 3890 | return NULL; |
| 3891 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3892 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3894 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3895 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3896 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3897 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3898 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3899 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3900 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3901 | num_surrogates = 0; |
| 3902 | |
| 3903 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3904 | if (*four_bytes > 0xFFFF) |
| 3905 | ++num_surrogates; |
| 3906 | } |
| 3907 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3908 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3909 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3910 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | PyErr_NoMemory(); |
| 3912 | return NULL; |
| 3913 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3914 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3916 | w = _PyUnicode_WSTR(unicode); |
| 3917 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3918 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3919 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3920 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3921 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3922 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3923 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3924 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3925 | } |
| 3926 | else |
| 3927 | *w = *four_bytes; |
| 3928 | |
| 3929 | if (w > wchar_end) { |
| 3930 | assert(0 && "Miscalculated string end"); |
| 3931 | } |
| 3932 | } |
| 3933 | *w = 0; |
| 3934 | #else |
| 3935 | /* sizeof(wchar_t) == 4 */ |
| 3936 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3937 | "should share memory already."); |
| 3938 | return NULL; |
| 3939 | #endif |
| 3940 | } |
| 3941 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3942 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3943 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3944 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3945 | PyErr_NoMemory(); |
| 3946 | return NULL; |
| 3947 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3948 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3949 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3950 | w = _PyUnicode_WSTR(unicode); |
| 3951 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3952 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3953 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3954 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3955 | for (; w < wchar_end; ++one_byte, ++w) |
| 3956 | *w = *one_byte; |
| 3957 | /* null-terminate the wstr */ |
| 3958 | *w = 0; |
| 3959 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3960 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3962 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3963 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3964 | *w = *two_bytes; |
| 3965 | /* null-terminate the wstr */ |
| 3966 | *w = 0; |
| 3967 | #else |
| 3968 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3969 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3970 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3971 | Py_FatalError("Impossible unicode object state, wstr " |
| 3972 | "and str should share memory already."); |
| 3973 | return NULL; |
| 3974 | #endif |
| 3975 | } |
| 3976 | else { |
| 3977 | assert(0 && "This should never happen."); |
| 3978 | } |
| 3979 | } |
| 3980 | } |
| 3981 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3982 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3983 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3986 | Py_UNICODE * |
| 3987 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3989 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3993 | Py_ssize_t |
| 3994 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3995 | { |
| 3996 | if (!PyUnicode_Check(unicode)) { |
| 3997 | PyErr_BadArgument(); |
| 3998 | goto onError; |
| 3999 | } |
| 4000 | return PyUnicode_GET_SIZE(unicode); |
| 4001 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4002 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4003 | return -1; |
| 4004 | } |
| 4005 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4006 | Py_ssize_t |
| 4007 | PyUnicode_GetLength(PyObject *unicode) |
| 4008 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4009 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4010 | PyErr_BadArgument(); |
| 4011 | return -1; |
| 4012 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4013 | if (PyUnicode_READY(unicode) == -1) |
| 4014 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4015 | return PyUnicode_GET_LENGTH(unicode); |
| 4016 | } |
| 4017 | |
| 4018 | Py_UCS4 |
| 4019 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4020 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4021 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4022 | PyErr_BadArgument(); |
| 4023 | return (Py_UCS4)-1; |
| 4024 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4025 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4026 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4027 | return (Py_UCS4)-1; |
| 4028 | } |
| 4029 | return PyUnicode_READ_CHAR(unicode, index); |
| 4030 | } |
| 4031 | |
| 4032 | int |
| 4033 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4034 | { |
| 4035 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4036 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4037 | return -1; |
| 4038 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4039 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4040 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4041 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4042 | return -1; |
| 4043 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4044 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4045 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4046 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4047 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4048 | return -1; |
| 4049 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4050 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4051 | index, ch); |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4055 | const char * |
| 4056 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4057 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4058 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4061 | /* create or adjust a UnicodeDecodeError */ |
| 4062 | static void |
| 4063 | make_decode_exception(PyObject **exceptionObject, |
| 4064 | const char *encoding, |
| 4065 | const char *input, Py_ssize_t length, |
| 4066 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4067 | const char *reason) |
| 4068 | { |
| 4069 | if (*exceptionObject == NULL) { |
| 4070 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4071 | encoding, input, length, startpos, endpos, reason); |
| 4072 | } |
| 4073 | else { |
| 4074 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4075 | goto onError; |
| 4076 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4077 | goto onError; |
| 4078 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4079 | goto onError; |
| 4080 | } |
| 4081 | return; |
| 4082 | |
| 4083 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4084 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4087 | /* error handling callback helper: |
| 4088 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4089 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4090 | and adjust various state variables. |
| 4091 | return 0 on success, -1 on error |
| 4092 | */ |
| 4093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4094 | static int |
| 4095 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4096 | const char *encoding, const char *reason, |
| 4097 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4098 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4099 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4100 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4101 | 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] | 4102 | |
| 4103 | PyObject *restuple = NULL; |
| 4104 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4105 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4106 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4107 | Py_ssize_t requiredsize; |
| 4108 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4109 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | int res = -1; |
| 4111 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4112 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4113 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4114 | else |
| 4115 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4116 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4117 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4118 | *errorHandler = PyCodec_LookupError(errors); |
| 4119 | if (*errorHandler == NULL) |
| 4120 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4123 | make_decode_exception(exceptionObject, |
| 4124 | encoding, |
| 4125 | *input, *inend - *input, |
| 4126 | *startinpos, *endinpos, |
| 4127 | reason); |
| 4128 | if (*exceptionObject == NULL) |
| 4129 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 | |
| 4131 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4132 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4133 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4134 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4135 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4136 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4137 | } |
| 4138 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4139 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4140 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4141 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4142 | |
| 4143 | /* Copy back the bytes variables, which might have been modified by the |
| 4144 | callback */ |
| 4145 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4146 | if (!inputobj) |
| 4147 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4148 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4149 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4150 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4151 | *input = PyBytes_AS_STRING(inputobj); |
| 4152 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4153 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4154 | /* we can DECREF safely, as the exception has another reference, |
| 4155 | so the object won't go away. */ |
| 4156 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4157 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4158 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4159 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4160 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4162 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4163 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4164 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4165 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4166 | /* need more space? (at least enough for what we |
| 4167 | have+the replacement+the rest of the string (starting |
| 4168 | at the new input position), so we won't have to check space |
| 4169 | when there are no errors in the rest of the string) */ |
| 4170 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 4171 | requiredsize = *outpos; |
| 4172 | if (requiredsize > PY_SSIZE_T_MAX - replen) |
| 4173 | goto overflow; |
| 4174 | requiredsize += replen; |
| 4175 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4176 | goto overflow; |
| 4177 | requiredsize += insize - newpos; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4178 | if (requiredsize > outsize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 4179 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4180 | requiredsize = 2*outsize; |
| 4181 | if (unicode_resize(output, requiredsize) < 0) |
| 4182 | goto onError; |
| 4183 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4184 | if (unicode_widen(output, *outpos, |
| 4185 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4186 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4187 | _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4188 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4189 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4190 | else { |
| 4191 | wchar_t *repwstr; |
| 4192 | Py_ssize_t repwlen; |
| 4193 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4194 | if (repwstr == NULL) |
| 4195 | goto onError; |
| 4196 | /* need more space? (at least enough for what we |
| 4197 | have+the replacement+the rest of the string (starting |
| 4198 | at the new input position), so we won't have to check space |
| 4199 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 4200 | requiredsize = *outpos; |
| 4201 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4202 | goto overflow; |
| 4203 | requiredsize += repwlen; |
| 4204 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4205 | goto overflow; |
| 4206 | requiredsize += insize - newpos; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4207 | if (requiredsize > outsize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 4208 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4209 | requiredsize = 2*outsize; |
| 4210 | if (unicode_resize(output, requiredsize) < 0) |
| 4211 | goto onError; |
| 4212 | } |
| 4213 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4214 | *outpos += repwlen; |
| 4215 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4216 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4217 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4218 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4219 | /* we made it! */ |
| 4220 | res = 0; |
| 4221 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4222 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4223 | Py_XDECREF(restuple); |
| 4224 | return res; |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 4225 | |
| 4226 | overflow: |
| 4227 | PyErr_SetString(PyExc_OverflowError, |
| 4228 | "decoded result is too long for a Python string"); |
| 4229 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4232 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4233 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4234 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4235 | |
| 4236 | /* Three simple macros defining base-64. */ |
| 4237 | |
| 4238 | /* Is c a base-64 character? */ |
| 4239 | |
| 4240 | #define IS_BASE64(c) \ |
| 4241 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4242 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4243 | ((c) >= '0' && (c) <= '9') || \ |
| 4244 | (c) == '+' || (c) == '/') |
| 4245 | |
| 4246 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4247 | |
| 4248 | #define FROM_BASE64(c) \ |
| 4249 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4250 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4251 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4252 | (c) == '+' ? 62 : 63) |
| 4253 | |
| 4254 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4255 | |
| 4256 | #define TO_BASE64(n) \ |
| 4257 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4258 | |
| 4259 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4260 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4261 | * byte not decoding to itself is the + which begins a base64 |
| 4262 | * string. */ |
| 4263 | |
| 4264 | #define DECODE_DIRECT(c) \ |
| 4265 | ((c) <= 127 && (c) != '+') |
| 4266 | |
| 4267 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4268 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4269 | * the above). See RFC2152. This array identifies these different |
| 4270 | * sets: |
| 4271 | * 0 : "Set D" |
| 4272 | * alphanumeric and '(),-./:? |
| 4273 | * 1 : "Set O" |
| 4274 | * !"#$%&*;<=>@[]^_`{|} |
| 4275 | * 2 : "whitespace" |
| 4276 | * ht nl cr sp |
| 4277 | * 3 : special (must be base64 encoded) |
| 4278 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4279 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4280 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4281 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | char utf7_category[128] = { |
| 4283 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4284 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4285 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4286 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4287 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4288 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4289 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4290 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4291 | /* @ A B C D E F G H I J K L M N O */ |
| 4292 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4293 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4294 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4295 | /* ` a b c d e f g h i j k l m n o */ |
| 4296 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4297 | /* p q r s t u v w x y z { | } ~ del */ |
| 4298 | 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] | 4299 | }; |
| 4300 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4301 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4302 | * answer depends on whether we are encoding set O as itself, and also |
| 4303 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4304 | * clear that the answers to these questions vary between |
| 4305 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4306 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4307 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4308 | ((c) < 128 && (c) > 0 && \ |
| 4309 | ((utf7_category[(c)] == 0) || \ |
| 4310 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4311 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4312 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4313 | PyObject * |
| 4314 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4315 | Py_ssize_t size, |
| 4316 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4317 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4318 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4319 | } |
| 4320 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4321 | /* The decoder. The only state we preserve is our read position, |
| 4322 | * i.e. how many characters we have consumed. So if we end in the |
| 4323 | * middle of a shift sequence we have to back off the read position |
| 4324 | * and the output to the beginning of the sequence, otherwise we lose |
| 4325 | * all the shift state (seen bits, number of bits seen, high |
| 4326 | * surrogate). */ |
| 4327 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4328 | PyObject * |
| 4329 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4330 | Py_ssize_t size, |
| 4331 | const char *errors, |
| 4332 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4333 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4335 | Py_ssize_t startinpos; |
| 4336 | Py_ssize_t endinpos; |
| 4337 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4338 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4339 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4340 | const char *errmsg = ""; |
| 4341 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4342 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4343 | unsigned int base64bits = 0; |
| 4344 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4345 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4346 | PyObject *errorHandler = NULL; |
| 4347 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4348 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4349 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4350 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4351 | if (!unicode) |
| 4352 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4353 | if (size == 0) { |
| 4354 | if (consumed) |
| 4355 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4356 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4357 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4358 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4359 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4360 | e = s + size; |
| 4361 | |
| 4362 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4363 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4364 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4365 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4366 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | if (inShift) { /* in a base-64 section */ |
| 4368 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4369 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4370 | base64bits += 6; |
| 4371 | s++; |
| 4372 | if (base64bits >= 16) { |
| 4373 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4374 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4375 | base64bits -= 16; |
| 4376 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4377 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4378 | if (surrogate) { |
| 4379 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4380 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4381 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4382 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4383 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4384 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4385 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | } |
| 4387 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4388 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4389 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4390 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4393 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4394 | /* first surrogate */ |
| 4395 | surrogate = outCh; |
| 4396 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4397 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4398 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4399 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | } |
| 4403 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4404 | inShift = 0; |
| 4405 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4407 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4408 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4409 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4411 | if (base64bits > 0) { /* left-over bits */ |
| 4412 | if (base64bits >= 6) { |
| 4413 | /* We've seen at least one base-64 character */ |
| 4414 | errmsg = "partial character in shift sequence"; |
| 4415 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4417 | else { |
| 4418 | /* Some bits remain; they should be zero */ |
| 4419 | if (base64buffer != 0) { |
| 4420 | errmsg = "non-zero padding bits in shift sequence"; |
| 4421 | goto utf7Error; |
| 4422 | } |
| 4423 | } |
| 4424 | } |
| 4425 | if (ch != '-') { |
| 4426 | /* '-' is absorbed; other terminating |
| 4427 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4428 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4429 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4430 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | } |
| 4432 | } |
| 4433 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4435 | s++; /* consume '+' */ |
| 4436 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4437 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4438 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4439 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | } |
| 4441 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4442 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4443 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4444 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4445 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4446 | } |
| 4447 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4448 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4449 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4450 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4451 | s++; |
| 4452 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4453 | else { |
| 4454 | startinpos = s-starts; |
| 4455 | s++; |
| 4456 | errmsg = "unexpected special character"; |
| 4457 | goto utf7Error; |
| 4458 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4459 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | endinpos = s-starts; |
| 4462 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4463 | errors, &errorHandler, |
| 4464 | "utf7", errmsg, |
| 4465 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4466 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4467 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4470 | /* end of string */ |
| 4471 | |
| 4472 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4473 | /* if we're in an inconsistent state, that's an error */ |
| 4474 | if (surrogate || |
| 4475 | (base64bits >= 6) || |
| 4476 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4477 | endinpos = size; |
| 4478 | if (unicode_decode_call_errorhandler( |
| 4479 | errors, &errorHandler, |
| 4480 | "utf7", "unterminated shift sequence", |
| 4481 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4482 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4483 | goto onError; |
| 4484 | if (s < e) |
| 4485 | goto restart; |
| 4486 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4487 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4488 | |
| 4489 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4490 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4491 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4492 | *consumed = startinpos; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4493 | if (outpos != shiftOutStart && |
| 4494 | PyUnicode_MAX_CHAR_VALUE(unicode) > 127) { |
| 4495 | PyObject *result = PyUnicode_FromKindAndData( |
| 4496 | PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4497 | shiftOutStart); |
| 4498 | Py_DECREF(unicode); |
| 4499 | unicode = result; |
| 4500 | } |
| 4501 | outpos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4502 | } |
| 4503 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4504 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4505 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4506 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4507 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4508 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4509 | goto onError; |
| 4510 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4511 | Py_XDECREF(errorHandler); |
| 4512 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4513 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4515 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | Py_XDECREF(errorHandler); |
| 4517 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4518 | Py_DECREF(unicode); |
| 4519 | return NULL; |
| 4520 | } |
| 4521 | |
| 4522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4523 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4524 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4525 | int base64SetO, |
| 4526 | int base64WhiteSpace, |
| 4527 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4528 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4529 | int kind; |
| 4530 | void *data; |
| 4531 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4532 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4533 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4534 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4535 | unsigned int base64bits = 0; |
| 4536 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4537 | char * out; |
| 4538 | char * start; |
| 4539 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4540 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4541 | return NULL; |
| 4542 | kind = PyUnicode_KIND(str); |
| 4543 | data = PyUnicode_DATA(str); |
| 4544 | len = PyUnicode_GET_LENGTH(str); |
| 4545 | |
| 4546 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4547 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4548 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4549 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4550 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4551 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4552 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4553 | if (v == NULL) |
| 4554 | return NULL; |
| 4555 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4556 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4557 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4558 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4559 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4560 | if (inShift) { |
| 4561 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4562 | /* shifting out */ |
| 4563 | if (base64bits) { /* output remaining bits */ |
| 4564 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4565 | base64buffer = 0; |
| 4566 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4567 | } |
| 4568 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4569 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4570 | so no '-' is required, except if the character is itself a '-' */ |
| 4571 | if (IS_BASE64(ch) || ch == '-') { |
| 4572 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4573 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4574 | *out++ = (char) ch; |
| 4575 | } |
| 4576 | else { |
| 4577 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4578 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4579 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4580 | else { /* not in a shift sequence */ |
| 4581 | if (ch == '+') { |
| 4582 | *out++ = '+'; |
| 4583 | *out++ = '-'; |
| 4584 | } |
| 4585 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4586 | *out++ = (char) ch; |
| 4587 | } |
| 4588 | else { |
| 4589 | *out++ = '+'; |
| 4590 | inShift = 1; |
| 4591 | goto encode_char; |
| 4592 | } |
| 4593 | } |
| 4594 | continue; |
| 4595 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4596 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4597 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4598 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4599 | /* code first surrogate */ |
| 4600 | base64bits += 16; |
| 4601 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4602 | while (base64bits >= 6) { |
| 4603 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4604 | base64bits -= 6; |
| 4605 | } |
| 4606 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4607 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4608 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4609 | base64bits += 16; |
| 4610 | base64buffer = (base64buffer << 16) | ch; |
| 4611 | while (base64bits >= 6) { |
| 4612 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4613 | base64bits -= 6; |
| 4614 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4615 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4616 | if (base64bits) |
| 4617 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4618 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4619 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4620 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4621 | return NULL; |
| 4622 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4623 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4624 | PyObject * |
| 4625 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4626 | Py_ssize_t size, |
| 4627 | int base64SetO, |
| 4628 | int base64WhiteSpace, |
| 4629 | const char *errors) |
| 4630 | { |
| 4631 | PyObject *result; |
| 4632 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4633 | if (tmp == NULL) |
| 4634 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4635 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4636 | base64WhiteSpace, errors); |
| 4637 | Py_DECREF(tmp); |
| 4638 | return result; |
| 4639 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4640 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4641 | #undef IS_BASE64 |
| 4642 | #undef FROM_BASE64 |
| 4643 | #undef TO_BASE64 |
| 4644 | #undef DECODE_DIRECT |
| 4645 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4647 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4648 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4649 | PyObject * |
| 4650 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4651 | Py_ssize_t size, |
| 4652 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4653 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4654 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4655 | } |
| 4656 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4657 | #include "stringlib/asciilib.h" |
| 4658 | #include "stringlib/codecs.h" |
| 4659 | #include "stringlib/undef.h" |
| 4660 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4661 | #include "stringlib/ucs1lib.h" |
| 4662 | #include "stringlib/codecs.h" |
| 4663 | #include "stringlib/undef.h" |
| 4664 | |
| 4665 | #include "stringlib/ucs2lib.h" |
| 4666 | #include "stringlib/codecs.h" |
| 4667 | #include "stringlib/undef.h" |
| 4668 | |
| 4669 | #include "stringlib/ucs4lib.h" |
| 4670 | #include "stringlib/codecs.h" |
| 4671 | #include "stringlib/undef.h" |
| 4672 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4673 | /* Mask to quickly check whether a C 'long' contains a |
| 4674 | non-ASCII, UTF8-encoded char. */ |
| 4675 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4676 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4677 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4678 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4679 | #else |
| 4680 | # error C 'long' size should be either 4 or 8! |
| 4681 | #endif |
| 4682 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4683 | static Py_ssize_t |
| 4684 | ascii_decode(const char *start, const char *end, Py_UCS1 *dest) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4685 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4686 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4687 | const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4688 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4689 | /* |
| 4690 | * Issue #17237: m68k is a bit different from most architectures in |
| 4691 | * that objects do not use "natural alignment" - for example, int and |
| 4692 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4693 | * won't work; also, tests have shown that skipping the "optimised |
| 4694 | * version" will even speed up m68k. |
| 4695 | */ |
| 4696 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4697 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4698 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4699 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4700 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4701 | an explanation. */ |
| 4702 | /* Help register allocation */ |
| 4703 | register const char *_p = p; |
| 4704 | register Py_UCS1 * q = dest; |
| 4705 | while (_p < aligned_end) { |
| 4706 | unsigned long value = *(const unsigned long *) _p; |
| 4707 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4708 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4709 | *((unsigned long *)q) = value; |
| 4710 | _p += SIZEOF_LONG; |
| 4711 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4712 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4713 | p = _p; |
| 4714 | while (p < end) { |
| 4715 | if ((unsigned char)*p & 0x80) |
| 4716 | break; |
| 4717 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4719 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4720 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4721 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4722 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4723 | while (p < end) { |
| 4724 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4725 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4726 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4727 | /* Help register allocation */ |
| 4728 | register const char *_p = p; |
| 4729 | while (_p < aligned_end) { |
| 4730 | unsigned long value = *(unsigned long *) _p; |
| 4731 | if (value & ASCII_CHAR_MASK) |
| 4732 | break; |
| 4733 | _p += SIZEOF_LONG; |
| 4734 | } |
| 4735 | p = _p; |
| 4736 | if (_p == end) |
| 4737 | break; |
| 4738 | } |
| 4739 | if ((unsigned char)*p & 0x80) |
| 4740 | break; |
| 4741 | ++p; |
| 4742 | } |
| 4743 | memcpy(dest, start, p - start); |
| 4744 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4745 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4746 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4747 | PyObject * |
| 4748 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4749 | Py_ssize_t size, |
| 4750 | const char *errors, |
| 4751 | Py_ssize_t *consumed) |
| 4752 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4753 | PyObject *unicode; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4754 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | const char *end = s + size; |
| 4756 | Py_ssize_t outpos; |
| 4757 | |
| 4758 | Py_ssize_t startinpos; |
| 4759 | Py_ssize_t endinpos; |
| 4760 | const char *errmsg = ""; |
| 4761 | PyObject *errorHandler = NULL; |
| 4762 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4763 | |
| 4764 | if (size == 0) { |
| 4765 | if (consumed) |
| 4766 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4767 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4770 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4771 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4772 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4773 | *consumed = 1; |
| 4774 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4775 | } |
| 4776 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4777 | unicode = PyUnicode_New(size, 127); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4778 | if (!unicode) |
| 4779 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4780 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4781 | outpos = ascii_decode(s, end, PyUnicode_1BYTE_DATA(unicode)); |
| 4782 | s += outpos; |
| 4783 | while (s < end) { |
| 4784 | Py_UCS4 ch; |
| 4785 | int kind = PyUnicode_KIND(unicode); |
| 4786 | if (kind == PyUnicode_1BYTE_KIND) { |
| 4787 | if (PyUnicode_IS_ASCII(unicode)) |
| 4788 | ch = asciilib_utf8_decode(&s, end, |
| 4789 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4790 | else |
| 4791 | ch = ucs1lib_utf8_decode(&s, end, |
| 4792 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4793 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 4794 | ch = ucs2lib_utf8_decode(&s, end, |
| 4795 | PyUnicode_2BYTE_DATA(unicode), &outpos); |
| 4796 | } else { |
| 4797 | assert(kind == PyUnicode_4BYTE_KIND); |
| 4798 | ch = ucs4lib_utf8_decode(&s, end, |
| 4799 | PyUnicode_4BYTE_DATA(unicode), &outpos); |
| 4800 | } |
| 4801 | |
| 4802 | switch (ch) { |
| 4803 | case 0: |
| 4804 | if (s == end || consumed) |
| 4805 | goto End; |
| 4806 | errmsg = "unexpected end of data"; |
| 4807 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4808 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4809 | break; |
| 4810 | case 1: |
| 4811 | errmsg = "invalid start byte"; |
| 4812 | startinpos = s - starts; |
| 4813 | endinpos = startinpos + 1; |
| 4814 | break; |
| 4815 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4816 | case 3: |
| 4817 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4818 | errmsg = "invalid continuation byte"; |
| 4819 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4820 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4821 | break; |
| 4822 | default: |
| 4823 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4824 | goto onError; |
| 4825 | continue; |
| 4826 | } |
| 4827 | |
| 4828 | if (unicode_decode_call_errorhandler( |
| 4829 | errors, &errorHandler, |
| 4830 | "utf-8", errmsg, |
| 4831 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4832 | &unicode, &outpos)) |
| 4833 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4834 | } |
| 4835 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4836 | End: |
| 4837 | if (unicode_resize(&unicode, outpos) < 0) |
| 4838 | goto onError; |
| 4839 | |
| 4840 | if (consumed) |
| 4841 | *consumed = s - starts; |
| 4842 | |
| 4843 | Py_XDECREF(errorHandler); |
| 4844 | Py_XDECREF(exc); |
| 4845 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4846 | return unicode; |
| 4847 | |
| 4848 | onError: |
| 4849 | Py_XDECREF(errorHandler); |
| 4850 | Py_XDECREF(exc); |
| 4851 | Py_XDECREF(unicode); |
| 4852 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4853 | } |
| 4854 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 | #ifdef __APPLE__ |
| 4856 | |
| 4857 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 27b1ca2 | 2012-12-03 12:47:59 +0100 | [diff] [blame] | 4858 | used to decode the command line arguments on Mac OS X. |
| 4859 | |
| 4860 | Return a pointer to a newly allocated wide character string (use |
| 4861 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4862 | |
| 4863 | wchar_t* |
| 4864 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4865 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4866 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4867 | wchar_t *unicode; |
| 4868 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4869 | |
| 4870 | /* Note: size will always be longer than the resulting Unicode |
| 4871 | character count */ |
Victor Stinner | 27b1ca2 | 2012-12-03 12:47:59 +0100 | [diff] [blame] | 4872 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4873 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4874 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4875 | if (!unicode) |
| 4876 | return NULL; |
| 4877 | |
| 4878 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4879 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4880 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4881 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4882 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4883 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4884 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4885 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4886 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4887 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4888 | if (ch > 0xFF) { |
| 4889 | #if SIZEOF_WCHAR_T == 4 |
| 4890 | assert(0); |
| 4891 | #else |
| 4892 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4893 | /* compute and append the two surrogates: */ |
| 4894 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4895 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4896 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4897 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4898 | else { |
| 4899 | if (!ch && s == e) |
| 4900 | break; |
| 4901 | /* surrogateescape */ |
| 4902 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4903 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4904 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4905 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4906 | return unicode; |
| 4907 | } |
| 4908 | |
| 4909 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4911 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4912 | |
| 4913 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4914 | and allocate exactly as much space needed at the end. Else allocate the |
| 4915 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4916 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4917 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4918 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4919 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4921 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4922 | void *data; |
| 4923 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4925 | if (!PyUnicode_Check(unicode)) { |
| 4926 | PyErr_BadArgument(); |
| 4927 | return NULL; |
| 4928 | } |
| 4929 | |
| 4930 | if (PyUnicode_READY(unicode) == -1) |
| 4931 | return NULL; |
| 4932 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4933 | if (PyUnicode_UTF8(unicode)) |
| 4934 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4935 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4936 | |
| 4937 | kind = PyUnicode_KIND(unicode); |
| 4938 | data = PyUnicode_DATA(unicode); |
| 4939 | size = PyUnicode_GET_LENGTH(unicode); |
| 4940 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4941 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4942 | default: |
| 4943 | assert(0); |
| 4944 | case PyUnicode_1BYTE_KIND: |
| 4945 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4946 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4947 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4948 | case PyUnicode_2BYTE_KIND: |
| 4949 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4950 | case PyUnicode_4BYTE_KIND: |
| 4951 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4952 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4955 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4956 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4957 | Py_ssize_t size, |
| 4958 | const char *errors) |
| 4959 | { |
| 4960 | PyObject *v, *unicode; |
| 4961 | |
| 4962 | unicode = PyUnicode_FromUnicode(s, size); |
| 4963 | if (unicode == NULL) |
| 4964 | return NULL; |
| 4965 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4966 | Py_DECREF(unicode); |
| 4967 | return v; |
| 4968 | } |
| 4969 | |
| 4970 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4971 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4972 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4973 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4976 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4977 | |
| 4978 | PyObject * |
| 4979 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4980 | Py_ssize_t size, |
| 4981 | const char *errors, |
| 4982 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | { |
| 4984 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4985 | } |
| 4986 | |
| 4987 | PyObject * |
| 4988 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4989 | Py_ssize_t size, |
| 4990 | const char *errors, |
| 4991 | int *byteorder, |
| 4992 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 | { |
| 4994 | const char *starts = s; |
| 4995 | Py_ssize_t startinpos; |
| 4996 | Py_ssize_t endinpos; |
| 4997 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4998 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4999 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5000 | int bo = 0; /* assume native ordering by default */ |
| 5001 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | /* Offsets from q for retrieving bytes in the right order. */ |
| 5003 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5004 | int iorder[] = {0, 1, 2, 3}; |
| 5005 | #else |
| 5006 | int iorder[] = {3, 2, 1, 0}; |
| 5007 | #endif |
| 5008 | PyObject *errorHandler = NULL; |
| 5009 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5010 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5011 | q = (unsigned char *)s; |
| 5012 | e = q + size; |
| 5013 | |
| 5014 | if (byteorder) |
| 5015 | bo = *byteorder; |
| 5016 | |
| 5017 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5018 | byte order setting accordingly. In native mode, the leading BOM |
| 5019 | mark is skipped, in all other modes, it is copied to the output |
| 5020 | stream as-is (giving a ZWNBSP character). */ |
| 5021 | if (bo == 0) { |
| 5022 | if (size >= 4) { |
| 5023 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5024 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5025 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | if (bom == 0x0000FEFF) { |
| 5027 | q += 4; |
| 5028 | bo = -1; |
| 5029 | } |
| 5030 | else if (bom == 0xFFFE0000) { |
| 5031 | q += 4; |
| 5032 | bo = 1; |
| 5033 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5034 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5035 | if (bom == 0x0000FEFF) { |
| 5036 | q += 4; |
| 5037 | bo = 1; |
| 5038 | } |
| 5039 | else if (bom == 0xFFFE0000) { |
| 5040 | q += 4; |
| 5041 | bo = -1; |
| 5042 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5043 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5044 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | if (bo == -1) { |
| 5048 | /* force LE */ |
| 5049 | iorder[0] = 0; |
| 5050 | iorder[1] = 1; |
| 5051 | iorder[2] = 2; |
| 5052 | iorder[3] = 3; |
| 5053 | } |
| 5054 | else if (bo == 1) { |
| 5055 | /* force BE */ |
| 5056 | iorder[0] = 3; |
| 5057 | iorder[1] = 2; |
| 5058 | iorder[2] = 1; |
| 5059 | iorder[3] = 0; |
| 5060 | } |
| 5061 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5062 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5063 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5064 | if (!unicode) |
| 5065 | return NULL; |
| 5066 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5067 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5068 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5069 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5071 | Py_UCS4 ch; |
| 5072 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5073 | if (e-q<4) { |
| 5074 | if (consumed) |
| 5075 | break; |
| 5076 | errmsg = "truncated data"; |
| 5077 | startinpos = ((const char *)q)-starts; |
| 5078 | endinpos = ((const char *)e)-starts; |
| 5079 | goto utf32Error; |
| 5080 | /* The remaining input chars are ignored if the callback |
| 5081 | chooses to skip the input */ |
| 5082 | } |
| 5083 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5084 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5085 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5086 | if (ch >= 0x110000) |
| 5087 | { |
| 5088 | errmsg = "codepoint not in range(0x110000)"; |
| 5089 | startinpos = ((const char *)q)-starts; |
| 5090 | endinpos = startinpos+4; |
| 5091 | goto utf32Error; |
| 5092 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5093 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5094 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | q += 4; |
| 5096 | continue; |
| 5097 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5098 | if (unicode_decode_call_errorhandler( |
| 5099 | errors, &errorHandler, |
| 5100 | "utf32", errmsg, |
| 5101 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5102 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | if (byteorder) |
| 5107 | *byteorder = bo; |
| 5108 | |
| 5109 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5110 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5111 | |
| 5112 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5113 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5114 | goto onError; |
| 5115 | |
| 5116 | Py_XDECREF(errorHandler); |
| 5117 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5118 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5120 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5121 | Py_DECREF(unicode); |
| 5122 | Py_XDECREF(errorHandler); |
| 5123 | Py_XDECREF(exc); |
| 5124 | return NULL; |
| 5125 | } |
| 5126 | |
| 5127 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5128 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5129 | const char *errors, |
| 5130 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5131 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5132 | int kind; |
| 5133 | void *data; |
| 5134 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5135 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5137 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5139 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5140 | int iorder[] = {0, 1, 2, 3}; |
| 5141 | #else |
| 5142 | int iorder[] = {3, 2, 1, 0}; |
| 5143 | #endif |
| 5144 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5145 | #define STORECHAR(CH) \ |
| 5146 | do { \ |
| 5147 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5148 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5149 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5150 | p[iorder[0]] = (CH) & 0xff; \ |
| 5151 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5152 | } while(0) |
| 5153 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5154 | if (!PyUnicode_Check(str)) { |
| 5155 | PyErr_BadArgument(); |
| 5156 | return NULL; |
| 5157 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5158 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5159 | return NULL; |
| 5160 | kind = PyUnicode_KIND(str); |
| 5161 | data = PyUnicode_DATA(str); |
| 5162 | len = PyUnicode_GET_LENGTH(str); |
| 5163 | |
| 5164 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5165 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5167 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5168 | if (v == NULL) |
| 5169 | return NULL; |
| 5170 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5171 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5172 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5173 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5174 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5175 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5176 | |
| 5177 | if (byteorder == -1) { |
| 5178 | /* force LE */ |
| 5179 | iorder[0] = 0; |
| 5180 | iorder[1] = 1; |
| 5181 | iorder[2] = 2; |
| 5182 | iorder[3] = 3; |
| 5183 | } |
| 5184 | else if (byteorder == 1) { |
| 5185 | /* force BE */ |
| 5186 | iorder[0] = 3; |
| 5187 | iorder[1] = 2; |
| 5188 | iorder[2] = 1; |
| 5189 | iorder[3] = 0; |
| 5190 | } |
| 5191 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5192 | for (i = 0; i < len; i++) |
| 5193 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5194 | |
| 5195 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5196 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5197 | #undef STORECHAR |
| 5198 | } |
| 5199 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5200 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5201 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5202 | Py_ssize_t size, |
| 5203 | const char *errors, |
| 5204 | int byteorder) |
| 5205 | { |
| 5206 | PyObject *result; |
| 5207 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5208 | if (tmp == NULL) |
| 5209 | return NULL; |
| 5210 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5211 | Py_DECREF(tmp); |
| 5212 | return result; |
| 5213 | } |
| 5214 | |
| 5215 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5216 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5217 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5218 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5222 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5223 | PyObject * |
| 5224 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5225 | Py_ssize_t size, |
| 5226 | const char *errors, |
| 5227 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5228 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5229 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5230 | } |
| 5231 | |
| 5232 | PyObject * |
| 5233 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5234 | Py_ssize_t size, |
| 5235 | const char *errors, |
| 5236 | int *byteorder, |
| 5237 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5238 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5239 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5240 | Py_ssize_t startinpos; |
| 5241 | Py_ssize_t endinpos; |
| 5242 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5243 | PyObject *unicode; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5244 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5245 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5246 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5247 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5248 | PyObject *errorHandler = NULL; |
| 5249 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5251 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5252 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | |
| 5254 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5255 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5257 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5258 | byte order setting accordingly. In native mode, the leading BOM |
| 5259 | mark is skipped, in all other modes, it is copied to the output |
| 5260 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5261 | if (bo == 0 && size >= 2) { |
| 5262 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5263 | if (bom == 0xFEFF) { |
| 5264 | q += 2; |
| 5265 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5267 | else if (bom == 0xFFFE) { |
| 5268 | q += 2; |
| 5269 | bo = 1; |
| 5270 | } |
| 5271 | if (byteorder) |
| 5272 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5273 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5275 | if (q == e) { |
| 5276 | if (consumed) |
| 5277 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5278 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5279 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5280 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5281 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5282 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5283 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5284 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5285 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5286 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5287 | /* Note: size will always be longer than the resulting Unicode |
| 5288 | character count */ |
| 5289 | unicode = PyUnicode_New((e - q + 1) / 2, 127); |
| 5290 | if (!unicode) |
| 5291 | return NULL; |
| 5292 | |
| 5293 | outpos = 0; |
| 5294 | while (1) { |
| 5295 | Py_UCS4 ch = 0; |
| 5296 | if (e - q >= 2) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5297 | int kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5298 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5299 | if (PyUnicode_IS_ASCII(unicode)) |
| 5300 | ch = asciilib_utf16_decode(&q, e, |
| 5301 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5302 | native_ordering); |
| 5303 | else |
| 5304 | ch = ucs1lib_utf16_decode(&q, e, |
| 5305 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5306 | native_ordering); |
| 5307 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5308 | ch = ucs2lib_utf16_decode(&q, e, |
| 5309 | PyUnicode_2BYTE_DATA(unicode), &outpos, |
| 5310 | native_ordering); |
| 5311 | } else { |
| 5312 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5313 | ch = ucs4lib_utf16_decode(&q, e, |
| 5314 | PyUnicode_4BYTE_DATA(unicode), &outpos, |
| 5315 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5316 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5317 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5318 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5319 | switch (ch) |
| 5320 | { |
| 5321 | case 0: |
| 5322 | /* remaining byte at the end? (size should be even) */ |
| 5323 | if (q == e || consumed) |
| 5324 | goto End; |
| 5325 | errmsg = "truncated data"; |
| 5326 | startinpos = ((const char *)q) - starts; |
| 5327 | endinpos = ((const char *)e) - starts; |
| 5328 | break; |
| 5329 | /* The remaining input chars are ignored if the callback |
| 5330 | chooses to skip the input */ |
| 5331 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5332 | q -= 2; |
| 5333 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5334 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5335 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5336 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5337 | endinpos = ((const char *)e) - starts; |
| 5338 | break; |
| 5339 | case 2: |
| 5340 | errmsg = "illegal encoding"; |
| 5341 | startinpos = ((const char *)q) - 2 - starts; |
| 5342 | endinpos = startinpos + 2; |
| 5343 | break; |
| 5344 | case 3: |
| 5345 | errmsg = "illegal UTF-16 surrogate"; |
| 5346 | startinpos = ((const char *)q) - 4 - starts; |
| 5347 | endinpos = startinpos + 2; |
| 5348 | break; |
| 5349 | default: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5350 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5351 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5352 | continue; |
| 5353 | } |
| 5354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5355 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5356 | errors, |
| 5357 | &errorHandler, |
| 5358 | "utf16", errmsg, |
| 5359 | &starts, |
| 5360 | (const char **)&e, |
| 5361 | &startinpos, |
| 5362 | &endinpos, |
| 5363 | &exc, |
| 5364 | (const char **)&q, |
| 5365 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5366 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5370 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5371 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5375 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | goto onError; |
| 5377 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5378 | Py_XDECREF(errorHandler); |
| 5379 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5380 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5382 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5384 | Py_XDECREF(errorHandler); |
| 5385 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | return NULL; |
| 5387 | } |
| 5388 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5389 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5390 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5391 | const char *errors, |
| 5392 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5394 | enum PyUnicode_Kind kind; |
| 5395 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5396 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5397 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5398 | unsigned short *out; |
| 5399 | Py_ssize_t bytesize; |
| 5400 | Py_ssize_t pairs; |
| 5401 | #ifdef WORDS_BIGENDIAN |
| 5402 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5403 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5404 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5405 | #endif |
| 5406 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5407 | if (!PyUnicode_Check(str)) { |
| 5408 | PyErr_BadArgument(); |
| 5409 | return NULL; |
| 5410 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5411 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5412 | return NULL; |
| 5413 | kind = PyUnicode_KIND(str); |
| 5414 | data = PyUnicode_DATA(str); |
| 5415 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5416 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5417 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5418 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5419 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5420 | const Py_UCS4 *end = in + len; |
| 5421 | while (in < end) |
| 5422 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5423 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5424 | } |
| 5425 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5426 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5427 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5428 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | if (v == NULL) |
| 5430 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5432 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5433 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5434 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5436 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5437 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5438 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5440 | switch (kind) { |
| 5441 | case PyUnicode_1BYTE_KIND: { |
| 5442 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5443 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5444 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5445 | case PyUnicode_2BYTE_KIND: { |
| 5446 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5447 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5448 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5449 | case PyUnicode_4BYTE_KIND: { |
| 5450 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5451 | break; |
| 5452 | } |
| 5453 | default: |
| 5454 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5455 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5456 | |
| 5457 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5458 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5461 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5462 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5463 | Py_ssize_t size, |
| 5464 | const char *errors, |
| 5465 | int byteorder) |
| 5466 | { |
| 5467 | PyObject *result; |
| 5468 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5469 | if (tmp == NULL) |
| 5470 | return NULL; |
| 5471 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5472 | Py_DECREF(tmp); |
| 5473 | return result; |
| 5474 | } |
| 5475 | |
| 5476 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5477 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5479 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5484 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5485 | if all the escapes in the string make it still a valid ASCII string. |
| 5486 | Returns -1 if any escapes were found which cause the string to |
| 5487 | pop out of ASCII range. Otherwise returns the length of the |
| 5488 | required buffer to hold the string. |
| 5489 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5490 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5491 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5492 | { |
| 5493 | const unsigned char *p = (const unsigned char *)s; |
| 5494 | const unsigned char *end = p + size; |
| 5495 | Py_ssize_t length = 0; |
| 5496 | |
| 5497 | if (size < 0) |
| 5498 | return -1; |
| 5499 | |
| 5500 | for (; p < end; ++p) { |
| 5501 | if (*p > 127) { |
| 5502 | /* Non-ASCII */ |
| 5503 | return -1; |
| 5504 | } |
| 5505 | else if (*p != '\\') { |
| 5506 | /* Normal character */ |
| 5507 | ++length; |
| 5508 | } |
| 5509 | else { |
| 5510 | /* Backslash-escape, check next char */ |
| 5511 | ++p; |
| 5512 | /* Escape sequence reaches till end of string or |
| 5513 | non-ASCII follow-up. */ |
| 5514 | if (p >= end || *p > 127) |
| 5515 | return -1; |
| 5516 | switch (*p) { |
| 5517 | case '\n': |
| 5518 | /* backslash + \n result in zero characters */ |
| 5519 | break; |
| 5520 | case '\\': case '\'': case '\"': |
| 5521 | case 'b': case 'f': case 't': |
| 5522 | case 'n': case 'r': case 'v': case 'a': |
| 5523 | ++length; |
| 5524 | break; |
| 5525 | case '0': case '1': case '2': case '3': |
| 5526 | case '4': case '5': case '6': case '7': |
| 5527 | case 'x': case 'u': case 'U': case 'N': |
| 5528 | /* these do not guarantee ASCII characters */ |
| 5529 | return -1; |
| 5530 | default: |
| 5531 | /* count the backslash + the other character */ |
| 5532 | length += 2; |
| 5533 | } |
| 5534 | } |
| 5535 | } |
| 5536 | return length; |
| 5537 | } |
| 5538 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5539 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5541 | PyObject * |
| 5542 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5543 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5544 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5546 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5547 | Py_ssize_t startinpos; |
| 5548 | Py_ssize_t endinpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5549 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5550 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5551 | char* message; |
| 5552 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5553 | PyObject *errorHandler = NULL; |
| 5554 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5555 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5556 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5557 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5558 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5559 | |
| 5560 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5561 | either the string is pure ASCII with named escapes like \n, etc. |
| 5562 | and we determined it's exact size (common case) |
| 5563 | or it contains \x, \u, ... escape sequences. then we create a |
| 5564 | 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] | 5565 | if (len >= 0) { |
| 5566 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5567 | if (!v) |
| 5568 | goto onError; |
| 5569 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5570 | } |
| 5571 | else { |
| 5572 | /* Escaped strings will always be longer than the resulting |
| 5573 | Unicode string, so we start with size here and then reduce the |
| 5574 | length after conversion to the true value. |
| 5575 | (but if the error callback returns a long replacement string |
| 5576 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5577 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5578 | if (!v) |
| 5579 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5580 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5581 | } |
| 5582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5584 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5585 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5587 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | while (s < end) { |
| 5589 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5590 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5591 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5593 | /* The only case in which i == ascii_length is a backslash |
| 5594 | followed by a newline. */ |
| 5595 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5598 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5599 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5600 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | continue; |
| 5602 | } |
| 5603 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5604 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | /* \ - Escapes */ |
| 5606 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5607 | c = *s++; |
| 5608 | if (s > end) |
| 5609 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5610 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5611 | /* The only case in which i == ascii_length is a backslash |
| 5612 | followed by a newline. */ |
| 5613 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5614 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5615 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5618 | #define WRITECHAR(ch) \ |
| 5619 | do { \ |
| 5620 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5621 | goto onError; \ |
| 5622 | }while(0) |
| 5623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5625 | case '\\': WRITECHAR('\\'); break; |
| 5626 | case '\'': WRITECHAR('\''); break; |
| 5627 | case '\"': WRITECHAR('\"'); break; |
| 5628 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5629 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5630 | case 'f': WRITECHAR('\014'); break; |
| 5631 | case 't': WRITECHAR('\t'); break; |
| 5632 | case 'n': WRITECHAR('\n'); break; |
| 5633 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5634 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5635 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5636 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5637 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5638 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5639 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | case '0': case '1': case '2': case '3': |
| 5641 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5642 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5643 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5644 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5645 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5646 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5648 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | break; |
| 5650 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | /* hex escapes */ |
| 5652 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5654 | digits = 2; |
| 5655 | message = "truncated \\xXX escape"; |
| 5656 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5660 | digits = 4; |
| 5661 | message = "truncated \\uXXXX escape"; |
| 5662 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5665 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5666 | digits = 8; |
| 5667 | message = "truncated \\UXXXXXXXX escape"; |
| 5668 | hexescape: |
| 5669 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5670 | if (end - s < digits) { |
| 5671 | /* count only hex digits */ |
| 5672 | for (; s < end; ++s) { |
| 5673 | c = (unsigned char)*s; |
| 5674 | if (!Py_ISXDIGIT(c)) |
| 5675 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5676 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5677 | goto error; |
| 5678 | } |
| 5679 | for (; digits--; ++s) { |
| 5680 | c = (unsigned char)*s; |
| 5681 | if (!Py_ISXDIGIT(c)) |
| 5682 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5683 | chr = (chr<<4) & ~0xF; |
| 5684 | if (c >= '0' && c <= '9') |
| 5685 | chr += c - '0'; |
| 5686 | else if (c >= 'a' && c <= 'f') |
| 5687 | chr += 10 + c - 'a'; |
| 5688 | else |
| 5689 | chr += 10 + c - 'A'; |
| 5690 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5691 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5692 | /* _decoding_error will have already written into the |
| 5693 | target buffer. */ |
| 5694 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5695 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5696 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5697 | message = "illegal Unicode character"; |
| 5698 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5699 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5700 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5701 | break; |
| 5702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5703 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5704 | case 'N': |
| 5705 | message = "malformed \\N character escape"; |
| 5706 | if (ucnhash_CAPI == NULL) { |
| 5707 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5708 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5709 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5710 | if (ucnhash_CAPI == NULL) |
| 5711 | goto ucnhashError; |
| 5712 | } |
| 5713 | if (*s == '{') { |
| 5714 | const char *start = s+1; |
| 5715 | /* look for the closing brace */ |
| 5716 | while (*s != '}' && s < end) |
| 5717 | s++; |
| 5718 | if (s > start && s < end && *s == '}') { |
| 5719 | /* found a name. look it up in the unicode database */ |
| 5720 | message = "unknown Unicode character name"; |
| 5721 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5722 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5723 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5724 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5725 | goto store; |
| 5726 | } |
| 5727 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5728 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5729 | |
| 5730 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5731 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5732 | message = "\\ at end of string"; |
| 5733 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5734 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5735 | } |
| 5736 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5737 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5738 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5739 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5740 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5742 | continue; |
| 5743 | |
| 5744 | error: |
| 5745 | endinpos = s-starts; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5746 | if (unicode_decode_call_errorhandler( |
| 5747 | errors, &errorHandler, |
| 5748 | "unicodeescape", message, |
| 5749 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5750 | &v, &i)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5751 | goto onError; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5752 | len = PyUnicode_GET_LENGTH(v); |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5753 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5755 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5756 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5757 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5758 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5759 | Py_XDECREF(errorHandler); |
| 5760 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5761 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5763 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5764 | PyErr_SetString( |
| 5765 | PyExc_UnicodeError, |
| 5766 | "\\N escapes not supported (can't load unicodedata module)" |
| 5767 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5768 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5769 | Py_XDECREF(errorHandler); |
| 5770 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5771 | return NULL; |
| 5772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5773 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5774 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | Py_XDECREF(errorHandler); |
| 5776 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | return NULL; |
| 5778 | } |
| 5779 | |
| 5780 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5781 | |
| 5782 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5783 | appropriate. |
| 5784 | |
| 5785 | */ |
| 5786 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5787 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5788 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5790 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5791 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5793 | int kind; |
| 5794 | void *data; |
| 5795 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5797 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5798 | escape. |
| 5799 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5800 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5801 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5802 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5803 | */ |
| 5804 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5805 | if (!PyUnicode_Check(unicode)) { |
| 5806 | PyErr_BadArgument(); |
| 5807 | return NULL; |
| 5808 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5809 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5810 | return NULL; |
| 5811 | len = PyUnicode_GET_LENGTH(unicode); |
| 5812 | kind = PyUnicode_KIND(unicode); |
| 5813 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5814 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5815 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5816 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5817 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5818 | } |
| 5819 | |
| 5820 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5821 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5822 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5823 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5824 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5825 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5826 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5828 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5829 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | if (repr == NULL) |
| 5831 | return NULL; |
| 5832 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5833 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5835 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5836 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5837 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5838 | /* Escape backslashes */ |
| 5839 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5840 | *p++ = '\\'; |
| 5841 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5842 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5843 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5844 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5845 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5846 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5847 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5848 | *p++ = '\\'; |
| 5849 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5850 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5851 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5852 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5853 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5854 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5855 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5856 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5857 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5858 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5859 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5862 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | *p++ = '\\'; |
| 5864 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5865 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5866 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5867 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5868 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5870 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5871 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5872 | else if (ch == '\t') { |
| 5873 | *p++ = '\\'; |
| 5874 | *p++ = 't'; |
| 5875 | } |
| 5876 | else if (ch == '\n') { |
| 5877 | *p++ = '\\'; |
| 5878 | *p++ = 'n'; |
| 5879 | } |
| 5880 | else if (ch == '\r') { |
| 5881 | *p++ = '\\'; |
| 5882 | *p++ = 'r'; |
| 5883 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5884 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5885 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5886 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5888 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5889 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5890 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5891 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | /* Copy everything else as-is */ |
| 5894 | else |
| 5895 | *p++ = (char) ch; |
| 5896 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5898 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5899 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5900 | return NULL; |
| 5901 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5906 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5908 | PyObject *result; |
| 5909 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5910 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5912 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5913 | Py_DECREF(tmp); |
| 5914 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5919 | PyObject * |
| 5920 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5921 | Py_ssize_t size, |
| 5922 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5924 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5925 | Py_ssize_t startinpos; |
| 5926 | Py_ssize_t endinpos; |
| 5927 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5928 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | const char *end; |
| 5930 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5931 | PyObject *errorHandler = NULL; |
| 5932 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5933 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | /* Escaped strings will always be longer than the resulting |
| 5935 | 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] | 5936 | length after conversion to the true value. (But decoding error |
| 5937 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5938 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5942 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5943 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | end = s + size; |
| 5945 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5946 | unsigned char c; |
| 5947 | Py_UCS4 x; |
| 5948 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5949 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5952 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5953 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5954 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5956 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5957 | startinpos = s-starts; |
| 5958 | |
| 5959 | /* \u-escapes are only interpreted iff the number of leading |
| 5960 | backslashes if odd */ |
| 5961 | bs = s; |
| 5962 | for (;s < end;) { |
| 5963 | if (*s != '\\') |
| 5964 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5965 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5966 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5967 | } |
| 5968 | if (((s - bs) & 1) == 0 || |
| 5969 | s >= end || |
| 5970 | (*s != 'u' && *s != 'U')) { |
| 5971 | continue; |
| 5972 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5973 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5974 | count = *s=='u' ? 4 : 8; |
| 5975 | s++; |
| 5976 | |
| 5977 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5978 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5979 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5980 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5981 | endinpos = s-starts; |
| 5982 | if (unicode_decode_call_errorhandler( |
| 5983 | errors, &errorHandler, |
| 5984 | "rawunicodeescape", "truncated \\uXXXX", |
| 5985 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5986 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5987 | goto onError; |
| 5988 | goto nextByte; |
| 5989 | } |
| 5990 | x = (x<<4) & ~0xF; |
| 5991 | if (c >= '0' && c <= '9') |
| 5992 | x += c - '0'; |
| 5993 | else if (c >= 'a' && c <= 'f') |
| 5994 | x += 10 + c - 'a'; |
| 5995 | else |
| 5996 | x += 10 + c - 'A'; |
| 5997 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5998 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5999 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 6000 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6001 | } else { |
| 6002 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6003 | if (unicode_decode_call_errorhandler( |
| 6004 | errors, &errorHandler, |
| 6005 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6006 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6007 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6008 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6009 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | nextByte: |
| 6011 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6013 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6014 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6015 | Py_XDECREF(errorHandler); |
| 6016 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6017 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6021 | Py_XDECREF(errorHandler); |
| 6022 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | return NULL; |
| 6024 | } |
| 6025 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6026 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6027 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6028 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6030 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | char *p; |
| 6032 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6033 | Py_ssize_t expandsize, pos; |
| 6034 | int kind; |
| 6035 | void *data; |
| 6036 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6038 | if (!PyUnicode_Check(unicode)) { |
| 6039 | PyErr_BadArgument(); |
| 6040 | return NULL; |
| 6041 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6042 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6043 | return NULL; |
| 6044 | kind = PyUnicode_KIND(unicode); |
| 6045 | data = PyUnicode_DATA(unicode); |
| 6046 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6047 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6048 | bytes, and 1 byte characters 4. */ |
| 6049 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6050 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6051 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6053 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6054 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | if (repr == NULL) |
| 6056 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6057 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6058 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6060 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6061 | for (pos = 0; pos < len; pos++) { |
| 6062 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6064 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6065 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6066 | *p++ = '\\'; |
| 6067 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6068 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6069 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6070 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6071 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6072 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6073 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6074 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6075 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6076 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6078 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | *p++ = '\\'; |
| 6080 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6081 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6082 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6083 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6084 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | /* Copy everything else as-is */ |
| 6087 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | *p++ = (char) ch; |
| 6089 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6090 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6091 | assert(p > q); |
| 6092 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6093 | return NULL; |
| 6094 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6097 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6098 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6099 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6101 | PyObject *result; |
| 6102 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6103 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6104 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6105 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6106 | Py_DECREF(tmp); |
| 6107 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | } |
| 6109 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6110 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6111 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6112 | PyObject * |
| 6113 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6114 | Py_ssize_t size, |
| 6115 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6116 | { |
| 6117 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6118 | Py_ssize_t startinpos; |
| 6119 | Py_ssize_t endinpos; |
| 6120 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6121 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6122 | const char *end; |
| 6123 | const char *reason; |
| 6124 | PyObject *errorHandler = NULL; |
| 6125 | PyObject *exc = NULL; |
| 6126 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6127 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6128 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6129 | 1)) |
| 6130 | return NULL; |
| 6131 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6132 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6133 | 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] | 6134 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6135 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6136 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6137 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6138 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6139 | end = s + size; |
| 6140 | |
| 6141 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6142 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6143 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6144 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6145 | endinpos = end-starts; |
| 6146 | reason = "truncated input"; |
| 6147 | goto error; |
| 6148 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6149 | /* We copy the raw representation one byte at a time because the |
| 6150 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6151 | ((char *) &uch)[0] = s[0]; |
| 6152 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6153 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6154 | ((char *) &uch)[2] = s[2]; |
| 6155 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6156 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6157 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6158 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6159 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6160 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6161 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6162 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6163 | reason = "illegal code point (> 0x10FFFF)"; |
| 6164 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6165 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6166 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6167 | s += Py_UNICODE_SIZE; |
| 6168 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6169 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6170 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6171 | Py_UNICODE uch2; |
| 6172 | ((char *) &uch2)[0] = s[0]; |
| 6173 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6174 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6175 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6176 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6177 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6178 | } |
| 6179 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6180 | #endif |
| 6181 | |
| 6182 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6183 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6184 | continue; |
| 6185 | |
| 6186 | error: |
| 6187 | startinpos = s - starts; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6188 | if (unicode_decode_call_errorhandler( |
| 6189 | errors, &errorHandler, |
| 6190 | "unicode_internal", reason, |
| 6191 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6192 | &v, &outpos)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6193 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6196 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6197 | goto onError; |
| 6198 | Py_XDECREF(errorHandler); |
| 6199 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6200 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6203 | Py_XDECREF(v); |
| 6204 | Py_XDECREF(errorHandler); |
| 6205 | Py_XDECREF(exc); |
| 6206 | return NULL; |
| 6207 | } |
| 6208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6210 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6211 | PyObject * |
| 6212 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6213 | Py_ssize_t size, |
| 6214 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6217 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6220 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6221 | static void |
| 6222 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6223 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6224 | PyObject *unicode, |
| 6225 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6226 | const char *reason) |
| 6227 | { |
| 6228 | if (*exceptionObject == NULL) { |
| 6229 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6230 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6231 | encoding, unicode, startpos, endpos, reason); |
| 6232 | } |
| 6233 | else { |
| 6234 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6235 | goto onError; |
| 6236 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6237 | goto onError; |
| 6238 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6239 | goto onError; |
| 6240 | return; |
| 6241 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6242 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6243 | } |
| 6244 | } |
| 6245 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6246 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6247 | static void |
| 6248 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6249 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6250 | PyObject *unicode, |
| 6251 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6252 | const char *reason) |
| 6253 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6254 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6255 | encoding, unicode, startpos, endpos, reason); |
| 6256 | if (*exceptionObject != NULL) |
| 6257 | PyCodec_StrictErrors(*exceptionObject); |
| 6258 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6259 | |
| 6260 | /* error handling callback helper: |
| 6261 | build arguments, call the callback and check the arguments, |
| 6262 | put the result into newpos and return the replacement string, which |
| 6263 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6264 | static PyObject * |
| 6265 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6266 | PyObject **errorHandler, |
| 6267 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6268 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6269 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6270 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6271 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6272 | 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] | 6273 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6274 | PyObject *restuple; |
| 6275 | PyObject *resunicode; |
| 6276 | |
| 6277 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6279 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6283 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6284 | return NULL; |
| 6285 | len = PyUnicode_GET_LENGTH(unicode); |
| 6286 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6287 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6288 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6289 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6290 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6291 | |
| 6292 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6294 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6295 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6296 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6297 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | Py_DECREF(restuple); |
| 6299 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6300 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6301 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6302 | &resunicode, newpos)) { |
| 6303 | Py_DECREF(restuple); |
| 6304 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6305 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6306 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6307 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6308 | Py_DECREF(restuple); |
| 6309 | return NULL; |
| 6310 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6311 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6312 | *newpos = len + *newpos; |
| 6313 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6314 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6315 | Py_DECREF(restuple); |
| 6316 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6317 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6318 | Py_INCREF(resunicode); |
| 6319 | Py_DECREF(restuple); |
| 6320 | return resunicode; |
| 6321 | } |
| 6322 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6323 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6324 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6325 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6326 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6327 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6328 | /* input state */ |
| 6329 | Py_ssize_t pos=0, size; |
| 6330 | int kind; |
| 6331 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6332 | /* output object */ |
| 6333 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6334 | /* pointer into the output */ |
| 6335 | char *str; |
| 6336 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6337 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6338 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6339 | 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] | 6340 | PyObject *errorHandler = NULL; |
| 6341 | PyObject *exc = NULL; |
| 6342 | /* the following variable is used for caching string comparisons |
| 6343 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6344 | int known_errorHandler = -1; |
| 6345 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6346 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6347 | return NULL; |
| 6348 | size = PyUnicode_GET_LENGTH(unicode); |
| 6349 | kind = PyUnicode_KIND(unicode); |
| 6350 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6351 | /* allocate enough for a simple encoding without |
| 6352 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6353 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6354 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6355 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6356 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6357 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6358 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6359 | ressize = size; |
| 6360 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6361 | while (pos < size) { |
| 6362 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6363 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 | /* can we encode this? */ |
| 6365 | if (c<limit) { |
| 6366 | /* no overflow check, because we know that the space is enough */ |
| 6367 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6368 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6369 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6370 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | Py_ssize_t requiredsize; |
| 6372 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6373 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6375 | Py_ssize_t collstart = pos; |
| 6376 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | /* find all unecodable characters */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6378 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | ++collend; |
| 6380 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6381 | if (known_errorHandler==-1) { |
| 6382 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6383 | known_errorHandler = 1; |
| 6384 | else if (!strcmp(errors, "replace")) |
| 6385 | known_errorHandler = 2; |
| 6386 | else if (!strcmp(errors, "ignore")) |
| 6387 | known_errorHandler = 3; |
| 6388 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6389 | known_errorHandler = 4; |
| 6390 | else |
| 6391 | known_errorHandler = 0; |
| 6392 | } |
| 6393 | switch (known_errorHandler) { |
| 6394 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6395 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | goto onError; |
| 6397 | case 2: /* replace */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6398 | while (collstart++ < collend) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | *str++ = '?'; /* fall through */ |
| 6400 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6401 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | break; |
| 6403 | case 4: /* xmlcharrefreplace */ |
| 6404 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6405 | requiredsize = respos; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6406 | /* determine replacement size */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6407 | for (i = collstart; i < collend; ++i) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6408 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6409 | Py_ssize_t incr; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6410 | if (ch < 10) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6411 | incr = 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6412 | else if (ch < 100) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6413 | incr = 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6414 | else if (ch < 1000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6415 | incr = 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6416 | else if (ch < 10000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6417 | incr = 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6418 | else if (ch < 100000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6419 | incr = 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6420 | else if (ch < 1000000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6421 | incr = 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6422 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6423 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6424 | incr = 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6425 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6426 | if (requiredsize > PY_SSIZE_T_MAX - incr) |
| 6427 | goto overflow; |
| 6428 | requiredsize += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6430 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6431 | goto overflow; |
| 6432 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6433 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6434 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | requiredsize = 2*ressize; |
| 6436 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6437 | goto onError; |
| 6438 | str = PyBytes_AS_STRING(res) + respos; |
| 6439 | ressize = requiredsize; |
| 6440 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6441 | /* generate replacement */ |
| 6442 | for (i = collstart; i < collend; ++i) { |
| 6443 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6444 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6445 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | break; |
| 6447 | default: |
| 6448 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6449 | encoding, reason, unicode, &exc, |
| 6450 | collstart, collend, &newpos); |
| 6451 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6452 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6454 | if (PyBytes_Check(repunicode)) { |
| 6455 | /* Directly copy bytes result to output. */ |
| 6456 | repsize = PyBytes_Size(repunicode); |
| 6457 | if (repsize > 1) { |
| 6458 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6459 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6460 | if (ressize > PY_SSIZE_T_MAX - repsize - 1) { |
| 6461 | Py_DECREF(repunicode); |
| 6462 | goto overflow; |
| 6463 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6464 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6465 | Py_DECREF(repunicode); |
| 6466 | goto onError; |
| 6467 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6468 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6469 | ressize += repsize-1; |
| 6470 | } |
| 6471 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6472 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6473 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6474 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6475 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6476 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | /* need more space? (at least enough for what we |
| 6478 | have+the replacement+the rest of the string, so |
| 6479 | we won't have to check space for encodable characters) */ |
| 6480 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6481 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6482 | requiredsize = respos; |
| 6483 | if (requiredsize > PY_SSIZE_T_MAX - repsize) |
| 6484 | goto overflow; |
| 6485 | requiredsize += repsize; |
| 6486 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6487 | goto overflow; |
| 6488 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6490 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6491 | requiredsize = 2*ressize; |
| 6492 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6493 | Py_DECREF(repunicode); |
| 6494 | goto onError; |
| 6495 | } |
| 6496 | str = PyBytes_AS_STRING(res) + respos; |
| 6497 | ressize = requiredsize; |
| 6498 | } |
| 6499 | /* check if there is anything unencodable in the replacement |
| 6500 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6501 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6502 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6503 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6504 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6505 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6506 | Py_DECREF(repunicode); |
| 6507 | goto onError; |
| 6508 | } |
| 6509 | *str = (char)c; |
| 6510 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6511 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6512 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6513 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6514 | } |
| 6515 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6516 | /* Resize if we allocated to much */ |
| 6517 | size = str - PyBytes_AS_STRING(res); |
| 6518 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6519 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6520 | if (_PyBytes_Resize(&res, size) < 0) |
| 6521 | goto onError; |
| 6522 | } |
| 6523 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6524 | Py_XDECREF(errorHandler); |
| 6525 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6526 | return res; |
| 6527 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6528 | overflow: |
| 6529 | PyErr_SetString(PyExc_OverflowError, |
| 6530 | "encoded result is too long for a Python string"); |
| 6531 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6532 | onError: |
| 6533 | Py_XDECREF(res); |
| 6534 | Py_XDECREF(errorHandler); |
| 6535 | Py_XDECREF(exc); |
| 6536 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6537 | } |
| 6538 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6540 | PyObject * |
| 6541 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6542 | Py_ssize_t size, |
| 6543 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6545 | PyObject *result; |
| 6546 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6547 | if (unicode == NULL) |
| 6548 | return NULL; |
| 6549 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6550 | Py_DECREF(unicode); |
| 6551 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6554 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6555 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | { |
| 6557 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 | PyErr_BadArgument(); |
| 6559 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6561 | if (PyUnicode_READY(unicode) == -1) |
| 6562 | return NULL; |
| 6563 | /* Fast path: if it is a one-byte string, construct |
| 6564 | bytes object directly. */ |
| 6565 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6566 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6567 | PyUnicode_GET_LENGTH(unicode)); |
| 6568 | /* Non-Latin-1 characters present. Defer to above function to |
| 6569 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6570 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6571 | } |
| 6572 | |
| 6573 | PyObject* |
| 6574 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6575 | { |
| 6576 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6580 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6581 | PyObject * |
| 6582 | PyUnicode_DecodeASCII(const char *s, |
| 6583 | Py_ssize_t size, |
| 6584 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6586 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6587 | PyObject *unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6588 | int kind; |
| 6589 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6590 | Py_ssize_t startinpos; |
| 6591 | Py_ssize_t endinpos; |
| 6592 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6593 | const char *e; |
| 6594 | PyObject *errorHandler = NULL; |
| 6595 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6598 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6601 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6602 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6603 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6604 | unicode = PyUnicode_New(size, 127); |
| 6605 | if (unicode == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6606 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6607 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6608 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6609 | data = PyUnicode_1BYTE_DATA(unicode); |
| 6610 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
| 6611 | if (outpos == size) |
| 6612 | return unicode; |
| 6613 | |
| 6614 | s += outpos; |
| 6615 | kind = PyUnicode_1BYTE_KIND; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6616 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6617 | register unsigned char c = (unsigned char)*s; |
| 6618 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6619 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6620 | ++s; |
| 6621 | } |
| 6622 | else { |
| 6623 | startinpos = s-starts; |
| 6624 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6625 | if (unicode_decode_call_errorhandler( |
| 6626 | errors, &errorHandler, |
| 6627 | "ascii", "ordinal not in range(128)", |
| 6628 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6629 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6631 | kind = PyUnicode_KIND(unicode); |
| 6632 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6635 | if (unicode_resize(&unicode, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6636 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6637 | Py_XDECREF(errorHandler); |
| 6638 | Py_XDECREF(exc); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6639 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 6640 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6641 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6642 | onError: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6643 | Py_XDECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6644 | Py_XDECREF(errorHandler); |
| 6645 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | return NULL; |
| 6647 | } |
| 6648 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6649 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6650 | PyObject * |
| 6651 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6652 | Py_ssize_t size, |
| 6653 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6654 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6655 | PyObject *result; |
| 6656 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6657 | if (unicode == NULL) |
| 6658 | return NULL; |
| 6659 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6660 | Py_DECREF(unicode); |
| 6661 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6664 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6665 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | { |
| 6667 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6668 | PyErr_BadArgument(); |
| 6669 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6671 | if (PyUnicode_READY(unicode) == -1) |
| 6672 | return NULL; |
| 6673 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6674 | directly. Else defer to above function to raise the exception. */ |
| 6675 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6676 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6677 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6678 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | PyObject * |
| 6682 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6683 | { |
| 6684 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6685 | } |
| 6686 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6687 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6688 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6689 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6690 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6691 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6692 | #define NEED_RETRY |
| 6693 | #endif |
| 6694 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6695 | #ifndef WC_ERR_INVALID_CHARS |
| 6696 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6697 | #endif |
| 6698 | |
| 6699 | static char* |
| 6700 | code_page_name(UINT code_page, PyObject **obj) |
| 6701 | { |
| 6702 | *obj = NULL; |
| 6703 | if (code_page == CP_ACP) |
| 6704 | return "mbcs"; |
| 6705 | if (code_page == CP_UTF7) |
| 6706 | return "CP_UTF7"; |
| 6707 | if (code_page == CP_UTF8) |
| 6708 | return "CP_UTF8"; |
| 6709 | |
| 6710 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6711 | if (*obj == NULL) |
| 6712 | return NULL; |
| 6713 | return PyBytes_AS_STRING(*obj); |
| 6714 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6715 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6716 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6717 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6718 | { |
| 6719 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6720 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6721 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6722 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6723 | return 0; |
| 6724 | |
| 6725 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6726 | if (prev == curr) |
| 6727 | return 1; |
| 6728 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6729 | as it assumes an incomplete character consists of a single |
| 6730 | byte. */ |
| 6731 | if (curr - prev == 2) |
| 6732 | return 1; |
| 6733 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6734 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6735 | return 0; |
| 6736 | } |
| 6737 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6738 | static DWORD |
| 6739 | decode_code_page_flags(UINT code_page) |
| 6740 | { |
| 6741 | if (code_page == CP_UTF7) { |
| 6742 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6743 | return 0; |
| 6744 | } |
| 6745 | else |
| 6746 | return MB_ERR_INVALID_CHARS; |
| 6747 | } |
| 6748 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6749 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6750 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6751 | * mode. |
| 6752 | * |
| 6753 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6754 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6755 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6756 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6757 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6758 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6759 | const char *in, |
| 6760 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6761 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6762 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6763 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6764 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6765 | |
| 6766 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6767 | assert(insize > 0); |
| 6768 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6769 | if (outsize <= 0) |
| 6770 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6771 | |
| 6772 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6773 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6774 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6775 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | if (*v == NULL) |
| 6777 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6778 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6779 | } |
| 6780 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6782 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6783 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6785 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6789 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6790 | if (outsize <= 0) |
| 6791 | goto error; |
| 6792 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6793 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6794 | error: |
| 6795 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6796 | return -2; |
| 6797 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6798 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6801 | /* |
| 6802 | * Decode a byte string from a code page into unicode object with an error |
| 6803 | * handler. |
| 6804 | * |
| 6805 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6806 | * UnicodeDecodeError exception and returns -1 on error. |
| 6807 | */ |
| 6808 | static int |
| 6809 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6810 | PyObject **v, |
| 6811 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6812 | const char *errors) |
| 6813 | { |
| 6814 | const char *startin = in; |
| 6815 | const char *endin = in + size; |
| 6816 | const DWORD flags = decode_code_page_flags(code_page); |
| 6817 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6818 | 2000 English version of the message. */ |
| 6819 | const char *reason = "No mapping for the Unicode character exists " |
| 6820 | "in the target code page."; |
| 6821 | /* each step cannot decode more than 1 character, but a character can be |
| 6822 | represented as a surrogate pair */ |
| 6823 | wchar_t buffer[2], *startout, *out; |
| 6824 | int insize, outsize; |
| 6825 | PyObject *errorHandler = NULL; |
| 6826 | PyObject *exc = NULL; |
| 6827 | PyObject *encoding_obj = NULL; |
| 6828 | char *encoding; |
| 6829 | DWORD err; |
| 6830 | int ret = -1; |
| 6831 | |
| 6832 | assert(size > 0); |
| 6833 | |
| 6834 | encoding = code_page_name(code_page, &encoding_obj); |
| 6835 | if (encoding == NULL) |
| 6836 | return -1; |
| 6837 | |
| 6838 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6839 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6840 | UnicodeDecodeError. */ |
| 6841 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6842 | if (exc != NULL) { |
| 6843 | PyCodec_StrictErrors(exc); |
| 6844 | Py_CLEAR(exc); |
| 6845 | } |
| 6846 | goto error; |
| 6847 | } |
| 6848 | |
| 6849 | if (*v == NULL) { |
| 6850 | /* Create unicode object */ |
| 6851 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6852 | PyErr_NoMemory(); |
| 6853 | goto error; |
| 6854 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6855 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6856 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6857 | if (*v == NULL) |
| 6858 | goto error; |
| 6859 | startout = PyUnicode_AS_UNICODE(*v); |
| 6860 | } |
| 6861 | else { |
| 6862 | /* Extend unicode object */ |
| 6863 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6864 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6865 | PyErr_NoMemory(); |
| 6866 | goto error; |
| 6867 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6868 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6869 | goto error; |
| 6870 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6871 | } |
| 6872 | |
| 6873 | /* Decode the byte string character per character */ |
| 6874 | out = startout; |
| 6875 | while (in < endin) |
| 6876 | { |
| 6877 | /* Decode a character */ |
| 6878 | insize = 1; |
| 6879 | do |
| 6880 | { |
| 6881 | outsize = MultiByteToWideChar(code_page, flags, |
| 6882 | in, insize, |
| 6883 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6884 | if (outsize > 0) |
| 6885 | break; |
| 6886 | err = GetLastError(); |
| 6887 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6888 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6889 | { |
| 6890 | PyErr_SetFromWindowsErr(0); |
| 6891 | goto error; |
| 6892 | } |
| 6893 | insize++; |
| 6894 | } |
| 6895 | /* 4=maximum length of a UTF-8 sequence */ |
| 6896 | while (insize <= 4 && (in + insize) <= endin); |
| 6897 | |
| 6898 | if (outsize <= 0) { |
| 6899 | Py_ssize_t startinpos, endinpos, outpos; |
| 6900 | |
| 6901 | startinpos = in - startin; |
| 6902 | endinpos = startinpos + 1; |
| 6903 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 6904 | if (unicode_decode_call_errorhandler( |
| 6905 | errors, &errorHandler, |
| 6906 | encoding, reason, |
| 6907 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6908 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6909 | { |
| 6910 | goto error; |
| 6911 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6912 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | } |
| 6914 | else { |
| 6915 | in += insize; |
| 6916 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6917 | out += outsize; |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | /* write a NUL character at the end */ |
| 6922 | *out = 0; |
| 6923 | |
| 6924 | /* Extend unicode object */ |
| 6925 | outsize = out - startout; |
| 6926 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6927 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6929 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6930 | |
| 6931 | error: |
| 6932 | Py_XDECREF(encoding_obj); |
| 6933 | Py_XDECREF(errorHandler); |
| 6934 | Py_XDECREF(exc); |
| 6935 | return ret; |
| 6936 | } |
| 6937 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6938 | static PyObject * |
| 6939 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6940 | const char *s, Py_ssize_t size, |
| 6941 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6942 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6943 | PyObject *v = NULL; |
| 6944 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6946 | if (code_page < 0) { |
| 6947 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6948 | return NULL; |
| 6949 | } |
| 6950 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6952 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6953 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6954 | do |
| 6955 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6957 | if (size > INT_MAX) { |
| 6958 | chunk_size = INT_MAX; |
| 6959 | final = 0; |
| 6960 | done = 0; |
| 6961 | } |
| 6962 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6963 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6964 | { |
| 6965 | chunk_size = (int)size; |
| 6966 | final = (consumed == NULL); |
| 6967 | done = 1; |
| 6968 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6970 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6971 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6972 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6973 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6974 | if (chunk_size == 0 && done) { |
| 6975 | if (v != NULL) |
| 6976 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6977 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6978 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6979 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6980 | |
| 6981 | converted = decode_code_page_strict(code_page, &v, |
| 6982 | s, chunk_size); |
| 6983 | if (converted == -2) |
| 6984 | converted = decode_code_page_errors(code_page, &v, |
| 6985 | s, chunk_size, |
| 6986 | errors); |
| 6987 | assert(converted != 0); |
| 6988 | |
| 6989 | if (converted < 0) { |
| 6990 | Py_XDECREF(v); |
| 6991 | return NULL; |
| 6992 | } |
| 6993 | |
| 6994 | if (consumed) |
| 6995 | *consumed += converted; |
| 6996 | |
| 6997 | s += converted; |
| 6998 | size -= converted; |
| 6999 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7001 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7002 | } |
| 7003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7004 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7005 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7006 | const char *s, |
| 7007 | Py_ssize_t size, |
| 7008 | const char *errors, |
| 7009 | Py_ssize_t *consumed) |
| 7010 | { |
| 7011 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7012 | } |
| 7013 | |
| 7014 | PyObject * |
| 7015 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7016 | Py_ssize_t size, |
| 7017 | const char *errors, |
| 7018 | Py_ssize_t *consumed) |
| 7019 | { |
| 7020 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7021 | } |
| 7022 | |
| 7023 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7024 | PyUnicode_DecodeMBCS(const char *s, |
| 7025 | Py_ssize_t size, |
| 7026 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7027 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7028 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7029 | } |
| 7030 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7031 | static DWORD |
| 7032 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7033 | { |
| 7034 | if (code_page == CP_UTF8) { |
| 7035 | if (winver.dwMajorVersion >= 6) |
| 7036 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7037 | and later */ |
| 7038 | return WC_ERR_INVALID_CHARS; |
| 7039 | else |
| 7040 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7041 | return 0; |
| 7042 | } |
| 7043 | else if (code_page == CP_UTF7) { |
| 7044 | /* CP_UTF7 only supports flags=0 */ |
| 7045 | return 0; |
| 7046 | } |
| 7047 | else { |
| 7048 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7049 | return 0; |
| 7050 | else |
| 7051 | return WC_NO_BEST_FIT_CHARS; |
| 7052 | } |
| 7053 | } |
| 7054 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7055 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7056 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7057 | * mode. |
| 7058 | * |
| 7059 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7060 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7062 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7063 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7064 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7065 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7066 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7067 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7068 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7069 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7070 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7071 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7072 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7073 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7074 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7075 | /* Create a substring so that we can get the UTF-16 representation |
| 7076 | of just the slice under consideration. */ |
| 7077 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7078 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7079 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7080 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7081 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7082 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7083 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7084 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7085 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7086 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7087 | if (substring == NULL) |
| 7088 | return -1; |
| 7089 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7090 | if (p == NULL) { |
| 7091 | Py_DECREF(substring); |
| 7092 | return -1; |
| 7093 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7094 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7095 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7096 | outsize = WideCharToMultiByte(code_page, flags, |
| 7097 | p, size, |
| 7098 | NULL, 0, |
| 7099 | NULL, pusedDefaultChar); |
| 7100 | if (outsize <= 0) |
| 7101 | goto error; |
| 7102 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7103 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7104 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7106 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7107 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7108 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7109 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7111 | if (*outbytes == NULL) { |
| 7112 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7113 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7114 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7115 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7116 | } |
| 7117 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7118 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7119 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7120 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7121 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7122 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7123 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7124 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7125 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7126 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7127 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7128 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7129 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
| 7132 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7133 | outsize = WideCharToMultiByte(code_page, flags, |
| 7134 | p, size, |
| 7135 | out, outsize, |
| 7136 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7137 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7138 | if (outsize <= 0) |
| 7139 | goto error; |
| 7140 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7141 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7142 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7143 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7144 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7145 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7146 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7147 | return -2; |
| 7148 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7149 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7150 | } |
| 7151 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7152 | /* |
| 7153 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7154 | * error handler. |
| 7155 | * |
| 7156 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7157 | * -1 on other error. |
| 7158 | */ |
| 7159 | static int |
| 7160 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7161 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7162 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7163 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7164 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7165 | Py_ssize_t pos = unicode_offset; |
| 7166 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7167 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7168 | 2000 English version of the message. */ |
| 7169 | const char *reason = "invalid character"; |
| 7170 | /* 4=maximum length of a UTF-8 sequence */ |
| 7171 | char buffer[4]; |
| 7172 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7173 | Py_ssize_t outsize; |
| 7174 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7175 | PyObject *errorHandler = NULL; |
| 7176 | PyObject *exc = NULL; |
| 7177 | PyObject *encoding_obj = NULL; |
| 7178 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7179 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | PyObject *rep; |
| 7181 | int ret = -1; |
| 7182 | |
| 7183 | assert(insize > 0); |
| 7184 | |
| 7185 | encoding = code_page_name(code_page, &encoding_obj); |
| 7186 | if (encoding == NULL) |
| 7187 | return -1; |
| 7188 | |
| 7189 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7190 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7191 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7192 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7193 | if (exc != NULL) { |
| 7194 | PyCodec_StrictErrors(exc); |
| 7195 | Py_DECREF(exc); |
| 7196 | } |
| 7197 | Py_XDECREF(encoding_obj); |
| 7198 | return -1; |
| 7199 | } |
| 7200 | |
| 7201 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7202 | pusedDefaultChar = &usedDefaultChar; |
| 7203 | else |
| 7204 | pusedDefaultChar = NULL; |
| 7205 | |
| 7206 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7207 | PyErr_NoMemory(); |
| 7208 | goto error; |
| 7209 | } |
| 7210 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7211 | |
| 7212 | if (*outbytes == NULL) { |
| 7213 | /* Create string object */ |
| 7214 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7215 | if (*outbytes == NULL) |
| 7216 | goto error; |
| 7217 | out = PyBytes_AS_STRING(*outbytes); |
| 7218 | } |
| 7219 | else { |
| 7220 | /* Extend string object */ |
| 7221 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7222 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7223 | PyErr_NoMemory(); |
| 7224 | goto error; |
| 7225 | } |
| 7226 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7227 | goto error; |
| 7228 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7229 | } |
| 7230 | |
| 7231 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7232 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7233 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7234 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7235 | wchar_t chars[2]; |
| 7236 | int charsize; |
| 7237 | if (ch < 0x10000) { |
| 7238 | chars[0] = (wchar_t)ch; |
| 7239 | charsize = 1; |
| 7240 | } |
| 7241 | else { |
| 7242 | ch -= 0x10000; |
| 7243 | chars[0] = 0xd800 + (ch >> 10); |
| 7244 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7245 | charsize = 2; |
| 7246 | } |
| 7247 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7249 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7251 | NULL, pusedDefaultChar); |
| 7252 | if (outsize > 0) { |
| 7253 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7254 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7255 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 | memcpy(out, buffer, outsize); |
| 7257 | out += outsize; |
| 7258 | continue; |
| 7259 | } |
| 7260 | } |
| 7261 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7262 | PyErr_SetFromWindowsErr(0); |
| 7263 | goto error; |
| 7264 | } |
| 7265 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | rep = unicode_encode_call_errorhandler( |
| 7267 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7268 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7269 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7270 | if (rep == NULL) |
| 7271 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7272 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | |
| 7274 | if (PyBytes_Check(rep)) { |
| 7275 | outsize = PyBytes_GET_SIZE(rep); |
| 7276 | if (outsize != 1) { |
| 7277 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7278 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7279 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7280 | Py_DECREF(rep); |
| 7281 | goto error; |
| 7282 | } |
| 7283 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7284 | } |
| 7285 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7286 | out += outsize; |
| 7287 | } |
| 7288 | else { |
| 7289 | Py_ssize_t i; |
| 7290 | enum PyUnicode_Kind kind; |
| 7291 | void *data; |
| 7292 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7293 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7294 | Py_DECREF(rep); |
| 7295 | goto error; |
| 7296 | } |
| 7297 | |
| 7298 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7299 | if (outsize != 1) { |
| 7300 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7301 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7302 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7303 | Py_DECREF(rep); |
| 7304 | goto error; |
| 7305 | } |
| 7306 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7307 | } |
| 7308 | kind = PyUnicode_KIND(rep); |
| 7309 | data = PyUnicode_DATA(rep); |
| 7310 | for (i=0; i < outsize; i++) { |
| 7311 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7312 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7313 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7314 | encoding, unicode, |
| 7315 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | "unable to encode error handler result to ASCII"); |
| 7317 | Py_DECREF(rep); |
| 7318 | goto error; |
| 7319 | } |
| 7320 | *out = (unsigned char)ch; |
| 7321 | out++; |
| 7322 | } |
| 7323 | } |
| 7324 | Py_DECREF(rep); |
| 7325 | } |
| 7326 | /* write a NUL byte */ |
| 7327 | *out = 0; |
| 7328 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7329 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7330 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7331 | goto error; |
| 7332 | ret = 0; |
| 7333 | |
| 7334 | error: |
| 7335 | Py_XDECREF(encoding_obj); |
| 7336 | Py_XDECREF(errorHandler); |
| 7337 | Py_XDECREF(exc); |
| 7338 | return ret; |
| 7339 | } |
| 7340 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7341 | static PyObject * |
| 7342 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7343 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7344 | const char *errors) |
| 7345 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7346 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7347 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7348 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7349 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7350 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7351 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7352 | return NULL; |
| 7353 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7354 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7355 | if (code_page < 0) { |
| 7356 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7357 | return NULL; |
| 7358 | } |
| 7359 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7360 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7361 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7362 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7363 | offset = 0; |
| 7364 | do |
| 7365 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7366 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7367 | /* 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] | 7368 | chunks. */ |
| 7369 | if (len > INT_MAX/2) { |
| 7370 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7371 | done = 0; |
| 7372 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7373 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7374 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7375 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7376 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7377 | done = 1; |
| 7378 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7379 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7380 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7381 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7382 | errors); |
| 7383 | if (ret == -2) |
| 7384 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7385 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7386 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7387 | if (ret < 0) { |
| 7388 | Py_XDECREF(outbytes); |
| 7389 | return NULL; |
| 7390 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7391 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7392 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7393 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7394 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7395 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7396 | return outbytes; |
| 7397 | } |
| 7398 | |
| 7399 | PyObject * |
| 7400 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7401 | Py_ssize_t size, |
| 7402 | const char *errors) |
| 7403 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7404 | PyObject *unicode, *res; |
| 7405 | unicode = PyUnicode_FromUnicode(p, size); |
| 7406 | if (unicode == NULL) |
| 7407 | return NULL; |
| 7408 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7409 | Py_DECREF(unicode); |
| 7410 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7411 | } |
| 7412 | |
| 7413 | PyObject * |
| 7414 | PyUnicode_EncodeCodePage(int code_page, |
| 7415 | PyObject *unicode, |
| 7416 | const char *errors) |
| 7417 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7418 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7419 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7421 | PyObject * |
| 7422 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7423 | { |
| 7424 | if (!PyUnicode_Check(unicode)) { |
| 7425 | PyErr_BadArgument(); |
| 7426 | return NULL; |
| 7427 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7428 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7431 | #undef NEED_RETRY |
| 7432 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7433 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7435 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7436 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7437 | PyObject * |
| 7438 | PyUnicode_DecodeCharmap(const char *s, |
| 7439 | Py_ssize_t size, |
| 7440 | PyObject *mapping, |
| 7441 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7443 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7444 | Py_ssize_t startinpos; |
| 7445 | Py_ssize_t endinpos; |
| 7446 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7447 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7448 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7449 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7450 | PyObject *errorHandler = NULL; |
| 7451 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7453 | /* Default to Latin-1 */ |
| 7454 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7457 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7461 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7462 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7463 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7464 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7465 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7466 | enum PyUnicode_Kind mapkind; |
| 7467 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7468 | Py_UCS4 x; |
| 7469 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7470 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7471 | return NULL; |
| 7472 | |
| 7473 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7474 | mapdata = PyUnicode_DATA(mapping); |
| 7475 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7476 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7477 | unsigned char ch; |
| 7478 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7479 | enum PyUnicode_Kind outkind = PyUnicode_KIND(v); |
| 7480 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7481 | void *outdata = PyUnicode_DATA(v); |
| 7482 | Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(v); |
| 7483 | while (s < e) { |
| 7484 | unsigned char ch = *s; |
| 7485 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7486 | if (x > maxchar) |
| 7487 | goto Error; |
| 7488 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, outpos++, x); |
| 7489 | ++s; |
| 7490 | } |
| 7491 | break; |
| 7492 | } |
| 7493 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7494 | void *outdata = PyUnicode_DATA(v); |
| 7495 | while (s < e) { |
| 7496 | unsigned char ch = *s; |
| 7497 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7498 | if (x == 0xFFFE) |
| 7499 | goto Error; |
| 7500 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, outpos++, x); |
| 7501 | ++s; |
| 7502 | } |
| 7503 | break; |
| 7504 | } |
| 7505 | } |
| 7506 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7509 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7510 | else |
| 7511 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7512 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7513 | if (x == 0xfffe) |
| 7514 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7515 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7516 | startinpos = s-starts; |
| 7517 | endinpos = startinpos+1; |
| 7518 | if (unicode_decode_call_errorhandler( |
| 7519 | errors, &errorHandler, |
| 7520 | "charmap", "character maps to <undefined>", |
| 7521 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7522 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | goto onError; |
| 7524 | } |
| 7525 | continue; |
| 7526 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7527 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7528 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7529 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7531 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7532 | } |
| 7533 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7534 | while (s < e) { |
| 7535 | unsigned char ch = *s; |
| 7536 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7537 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7538 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7539 | w = PyLong_FromLong((long)ch); |
| 7540 | if (w == NULL) |
| 7541 | goto onError; |
| 7542 | x = PyObject_GetItem(mapping, w); |
| 7543 | Py_DECREF(w); |
| 7544 | if (x == NULL) { |
| 7545 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7546 | /* No mapping found means: mapping is undefined. */ |
| 7547 | PyErr_Clear(); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7548 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7549 | } else |
| 7550 | goto onError; |
| 7551 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7552 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7553 | /* Apply mapping */ |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7554 | if (x == Py_None) |
| 7555 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | if (PyLong_Check(x)) { |
| 7557 | long value = PyLong_AS_LONG(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7558 | if (value == 0xFFFE) |
| 7559 | goto Undefined; |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7560 | if (value < 0 || value > MAX_UNICODE) { |
| 7561 | PyErr_Format(PyExc_TypeError, |
| 7562 | "character mapping must be in range(0x%lx)", |
| 7563 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | Py_DECREF(x); |
| 7565 | goto onError; |
| 7566 | } |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7567 | if (unicode_putchar(&v, &outpos, value) < 0) { |
| 7568 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7569 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7570 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7571 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7572 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7573 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7574 | |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7575 | if (PyUnicode_READY(x) == -1) { |
| 7576 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7577 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7578 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7579 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7580 | |
| 7581 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7582 | /* 1-1 mapping */ |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7583 | Py_UCS4 value = PyUnicode_READ_CHAR(x, 0); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7584 | if (value == 0xFFFE) |
| 7585 | goto Undefined; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7586 | if (unicode_putchar(&v, &outpos, value) < 0) { |
| 7587 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7588 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7589 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7590 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7591 | else if (targetsize > 1) { |
| 7592 | /* 1-n mapping */ |
| 7593 | if (targetsize > extrachars) { |
| 7594 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7595 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7596 | (targetsize << 2); |
| 7597 | extrachars += needed; |
| 7598 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7599 | if (unicode_resize(&v, |
| 7600 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7601 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7602 | Py_DECREF(x); |
| 7603 | goto onError; |
| 7604 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | } |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7606 | if (unicode_widen(&v, outpos, |
| 7607 | PyUnicode_MAX_CHAR_VALUE(x)) < 0) { |
| 7608 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7609 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7610 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7611 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7612 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7613 | extrachars -= targetsize; |
| 7614 | } |
| 7615 | /* 1-0 mapping: skip the character */ |
| 7616 | } |
| 7617 | else { |
| 7618 | /* wrong return value */ |
| 7619 | PyErr_SetString(PyExc_TypeError, |
| 7620 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7621 | Py_DECREF(x); |
| 7622 | goto onError; |
| 7623 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7624 | Py_DECREF(x); |
| 7625 | ++s; |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7626 | continue; |
| 7627 | Undefined: |
| 7628 | /* undefined mapping */ |
| 7629 | Py_XDECREF(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7630 | startinpos = s-starts; |
| 7631 | endinpos = startinpos+1; |
| 7632 | if (unicode_decode_call_errorhandler( |
| 7633 | errors, &errorHandler, |
| 7634 | "charmap", "character maps to <undefined>", |
| 7635 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7636 | &v, &outpos)) { |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7637 | goto onError; |
| 7638 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7639 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7641 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7642 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7643 | Py_XDECREF(errorHandler); |
| 7644 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7645 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7646 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7648 | Py_XDECREF(errorHandler); |
| 7649 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7650 | Py_XDECREF(v); |
| 7651 | return NULL; |
| 7652 | } |
| 7653 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7654 | /* Charmap encoding: the lookup table */ |
| 7655 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7656 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7657 | PyObject_HEAD |
| 7658 | unsigned char level1[32]; |
| 7659 | int count2, count3; |
| 7660 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7661 | }; |
| 7662 | |
| 7663 | static PyObject* |
| 7664 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7665 | { |
| 7666 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7667 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7672 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7674 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7675 | }; |
| 7676 | |
| 7677 | static void |
| 7678 | encoding_map_dealloc(PyObject* o) |
| 7679 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7680 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7681 | } |
| 7682 | |
| 7683 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7684 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | "EncodingMap", /*tp_name*/ |
| 7686 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7687 | 0, /*tp_itemsize*/ |
| 7688 | /* methods */ |
| 7689 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7690 | 0, /*tp_print*/ |
| 7691 | 0, /*tp_getattr*/ |
| 7692 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7693 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | 0, /*tp_repr*/ |
| 7695 | 0, /*tp_as_number*/ |
| 7696 | 0, /*tp_as_sequence*/ |
| 7697 | 0, /*tp_as_mapping*/ |
| 7698 | 0, /*tp_hash*/ |
| 7699 | 0, /*tp_call*/ |
| 7700 | 0, /*tp_str*/ |
| 7701 | 0, /*tp_getattro*/ |
| 7702 | 0, /*tp_setattro*/ |
| 7703 | 0, /*tp_as_buffer*/ |
| 7704 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7705 | 0, /*tp_doc*/ |
| 7706 | 0, /*tp_traverse*/ |
| 7707 | 0, /*tp_clear*/ |
| 7708 | 0, /*tp_richcompare*/ |
| 7709 | 0, /*tp_weaklistoffset*/ |
| 7710 | 0, /*tp_iter*/ |
| 7711 | 0, /*tp_iternext*/ |
| 7712 | encoding_map_methods, /*tp_methods*/ |
| 7713 | 0, /*tp_members*/ |
| 7714 | 0, /*tp_getset*/ |
| 7715 | 0, /*tp_base*/ |
| 7716 | 0, /*tp_dict*/ |
| 7717 | 0, /*tp_descr_get*/ |
| 7718 | 0, /*tp_descr_set*/ |
| 7719 | 0, /*tp_dictoffset*/ |
| 7720 | 0, /*tp_init*/ |
| 7721 | 0, /*tp_alloc*/ |
| 7722 | 0, /*tp_new*/ |
| 7723 | 0, /*tp_free*/ |
| 7724 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7725 | }; |
| 7726 | |
| 7727 | PyObject* |
| 7728 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7729 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7730 | PyObject *result; |
| 7731 | struct encoding_map *mresult; |
| 7732 | int i; |
| 7733 | int need_dict = 0; |
| 7734 | unsigned char level1[32]; |
| 7735 | unsigned char level2[512]; |
| 7736 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7737 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7738 | int kind; |
| 7739 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7740 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7741 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7742 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7743 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7744 | PyErr_BadArgument(); |
| 7745 | return NULL; |
| 7746 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7747 | kind = PyUnicode_KIND(string); |
| 7748 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7749 | length = PyUnicode_GET_LENGTH(string); |
| 7750 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7751 | memset(level1, 0xFF, sizeof level1); |
| 7752 | memset(level2, 0xFF, sizeof level2); |
| 7753 | |
| 7754 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7755 | or if there are non-BMP characters, we need to use |
| 7756 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7757 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7758 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7759 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7761 | ch = PyUnicode_READ(kind, data, i); |
| 7762 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7763 | need_dict = 1; |
| 7764 | break; |
| 7765 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7766 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7767 | /* unmapped character */ |
| 7768 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7769 | l1 = ch >> 11; |
| 7770 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7771 | if (level1[l1] == 0xFF) |
| 7772 | level1[l1] = count2++; |
| 7773 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7774 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7778 | need_dict = 1; |
| 7779 | |
| 7780 | if (need_dict) { |
| 7781 | PyObject *result = PyDict_New(); |
| 7782 | PyObject *key, *value; |
| 7783 | if (!result) |
| 7784 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7785 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7786 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7787 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7788 | if (!key || !value) |
| 7789 | goto failed1; |
| 7790 | if (PyDict_SetItem(result, key, value) == -1) |
| 7791 | goto failed1; |
| 7792 | Py_DECREF(key); |
| 7793 | Py_DECREF(value); |
| 7794 | } |
| 7795 | return result; |
| 7796 | failed1: |
| 7797 | Py_XDECREF(key); |
| 7798 | Py_XDECREF(value); |
| 7799 | Py_DECREF(result); |
| 7800 | return NULL; |
| 7801 | } |
| 7802 | |
| 7803 | /* Create a three-level trie */ |
| 7804 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7805 | 16*count2 + 128*count3 - 1); |
| 7806 | if (!result) |
| 7807 | return PyErr_NoMemory(); |
| 7808 | PyObject_Init(result, &EncodingMapType); |
| 7809 | mresult = (struct encoding_map*)result; |
| 7810 | mresult->count2 = count2; |
| 7811 | mresult->count3 = count3; |
| 7812 | mlevel1 = mresult->level1; |
| 7813 | mlevel2 = mresult->level23; |
| 7814 | mlevel3 = mresult->level23 + 16*count2; |
| 7815 | memcpy(mlevel1, level1, 32); |
| 7816 | memset(mlevel2, 0xFF, 16*count2); |
| 7817 | memset(mlevel3, 0, 128*count3); |
| 7818 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7819 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7820 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7821 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7822 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7823 | /* unmapped character */ |
| 7824 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7825 | o1 = ch>>11; |
| 7826 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7827 | i2 = 16*mlevel1[o1] + o2; |
| 7828 | if (mlevel2[i2] == 0xFF) |
| 7829 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7830 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7831 | i3 = 128*mlevel2[i2] + o3; |
| 7832 | mlevel3[i3] = i; |
| 7833 | } |
| 7834 | return result; |
| 7835 | } |
| 7836 | |
| 7837 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7838 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7839 | { |
| 7840 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7841 | int l1 = c>>11; |
| 7842 | int l2 = (c>>7) & 0xF; |
| 7843 | int l3 = c & 0x7F; |
| 7844 | int i; |
| 7845 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7846 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7847 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7848 | if (c == 0) |
| 7849 | return 0; |
| 7850 | /* level 1*/ |
| 7851 | i = map->level1[l1]; |
| 7852 | if (i == 0xFF) { |
| 7853 | return -1; |
| 7854 | } |
| 7855 | /* level 2*/ |
| 7856 | i = map->level23[16*i+l2]; |
| 7857 | if (i == 0xFF) { |
| 7858 | return -1; |
| 7859 | } |
| 7860 | /* level 3 */ |
| 7861 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7862 | if (i == 0) { |
| 7863 | return -1; |
| 7864 | } |
| 7865 | return i; |
| 7866 | } |
| 7867 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7868 | /* Lookup the character ch in the mapping. If the character |
| 7869 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7870 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7871 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7872 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7873 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7874 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7875 | PyObject *x; |
| 7876 | |
| 7877 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7878 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7879 | x = PyObject_GetItem(mapping, w); |
| 7880 | Py_DECREF(w); |
| 7881 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7882 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7883 | /* No mapping found means: mapping is undefined. */ |
| 7884 | PyErr_Clear(); |
| 7885 | x = Py_None; |
| 7886 | Py_INCREF(x); |
| 7887 | return x; |
| 7888 | } else |
| 7889 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7890 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7891 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7893 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7894 | long value = PyLong_AS_LONG(x); |
| 7895 | if (value < 0 || value > 255) { |
| 7896 | PyErr_SetString(PyExc_TypeError, |
| 7897 | "character mapping must be in range(256)"); |
| 7898 | Py_DECREF(x); |
| 7899 | return NULL; |
| 7900 | } |
| 7901 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7903 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7904 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7905 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 | /* wrong return value */ |
| 7907 | PyErr_Format(PyExc_TypeError, |
| 7908 | "character mapping must return integer, bytes or None, not %.400s", |
| 7909 | x->ob_type->tp_name); |
| 7910 | Py_DECREF(x); |
| 7911 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7912 | } |
| 7913 | } |
| 7914 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7915 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7916 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7917 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7919 | /* exponentially overallocate to minimize reallocations */ |
| 7920 | if (requiredsize < 2*outsize) |
| 7921 | requiredsize = 2*outsize; |
| 7922 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7923 | return -1; |
| 7924 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | } |
| 7926 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7927 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7929 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7930 | /* 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] | 7931 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7932 | space is available. Return a new reference to the object that |
| 7933 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7934 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7935 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7936 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7937 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7938 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7939 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7940 | PyObject *rep; |
| 7941 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7942 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7943 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7944 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7945 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7946 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7947 | if (res == -1) |
| 7948 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7949 | if (outsize<requiredsize) |
| 7950 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7951 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7952 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7953 | outstart[(*outpos)++] = (char)res; |
| 7954 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7955 | } |
| 7956 | |
| 7957 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7958 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7959 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7960 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7961 | Py_DECREF(rep); |
| 7962 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7963 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | if (PyLong_Check(rep)) { |
| 7965 | Py_ssize_t requiredsize = *outpos+1; |
| 7966 | if (outsize<requiredsize) |
| 7967 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7968 | Py_DECREF(rep); |
| 7969 | return enc_EXCEPTION; |
| 7970 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7971 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7972 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7973 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7974 | else { |
| 7975 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7976 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7977 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7978 | if (outsize<requiredsize) |
| 7979 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7980 | Py_DECREF(rep); |
| 7981 | return enc_EXCEPTION; |
| 7982 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7983 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | memcpy(outstart + *outpos, repchars, repsize); |
| 7985 | *outpos += repsize; |
| 7986 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7987 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7988 | Py_DECREF(rep); |
| 7989 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
| 7992 | /* handle an error in PyUnicode_EncodeCharmap |
| 7993 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7994 | static int |
| 7995 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7996 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7997 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7998 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7999 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8000 | { |
| 8001 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8002 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8003 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8004 | enum PyUnicode_Kind kind; |
| 8005 | void *data; |
| 8006 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8007 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8008 | Py_ssize_t collstartpos = *inpos; |
| 8009 | Py_ssize_t collendpos = *inpos+1; |
| 8010 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8011 | char *encoding = "charmap"; |
| 8012 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8013 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8014 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8015 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8016 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8017 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8018 | return -1; |
| 8019 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 | /* find all unencodable characters */ |
| 8021 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8022 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8023 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8024 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8025 | val = encoding_map_lookup(ch, mapping); |
| 8026 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | break; |
| 8028 | ++collendpos; |
| 8029 | continue; |
| 8030 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8031 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8032 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8033 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | if (rep==NULL) |
| 8035 | return -1; |
| 8036 | else if (rep!=Py_None) { |
| 8037 | Py_DECREF(rep); |
| 8038 | break; |
| 8039 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8040 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8042 | } |
| 8043 | /* cache callback name lookup |
| 8044 | * (if not done yet, i.e. it's the first error) */ |
| 8045 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8047 | *known_errorHandler = 1; |
| 8048 | else if (!strcmp(errors, "replace")) |
| 8049 | *known_errorHandler = 2; |
| 8050 | else if (!strcmp(errors, "ignore")) |
| 8051 | *known_errorHandler = 3; |
| 8052 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8053 | *known_errorHandler = 4; |
| 8054 | else |
| 8055 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8056 | } |
| 8057 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8058 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8059 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8060 | return -1; |
| 8061 | case 2: /* replace */ |
| 8062 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | x = charmapencode_output('?', mapping, res, respos); |
| 8064 | if (x==enc_EXCEPTION) { |
| 8065 | return -1; |
| 8066 | } |
| 8067 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8068 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | return -1; |
| 8070 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8071 | } |
| 8072 | /* fall through */ |
| 8073 | case 3: /* ignore */ |
| 8074 | *inpos = collendpos; |
| 8075 | break; |
| 8076 | case 4: /* xmlcharrefreplace */ |
| 8077 | /* generate replacement (temporarily (mis)uses p) */ |
| 8078 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8079 | char buffer[2+29+1+1]; |
| 8080 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8081 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8082 | for (cp = buffer; *cp; ++cp) { |
| 8083 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8084 | if (x==enc_EXCEPTION) |
| 8085 | return -1; |
| 8086 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8087 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8088 | return -1; |
| 8089 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8090 | } |
| 8091 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8092 | *inpos = collendpos; |
| 8093 | break; |
| 8094 | default: |
| 8095 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8096 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8097 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8098 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8100 | if (PyBytes_Check(repunicode)) { |
| 8101 | /* Directly copy bytes result to output. */ |
| 8102 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8103 | Py_ssize_t requiredsize; |
| 8104 | repsize = PyBytes_Size(repunicode); |
| 8105 | requiredsize = *respos + repsize; |
| 8106 | if (requiredsize > outsize) |
| 8107 | /* Make room for all additional bytes. */ |
| 8108 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8109 | Py_DECREF(repunicode); |
| 8110 | return -1; |
| 8111 | } |
| 8112 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8113 | PyBytes_AsString(repunicode), repsize); |
| 8114 | *respos += repsize; |
| 8115 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8116 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8117 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8118 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8119 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8120 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8121 | Py_DECREF(repunicode); |
| 8122 | return -1; |
| 8123 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8124 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8125 | data = PyUnicode_DATA(repunicode); |
| 8126 | kind = PyUnicode_KIND(repunicode); |
| 8127 | for (index = 0; index < repsize; index++) { |
| 8128 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8129 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8131 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | return -1; |
| 8133 | } |
| 8134 | else if (x==enc_FAILED) { |
| 8135 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8136 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | return -1; |
| 8138 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8139 | } |
| 8140 | *inpos = newpos; |
| 8141 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8142 | } |
| 8143 | return 0; |
| 8144 | } |
| 8145 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8146 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8147 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8148 | PyObject *mapping, |
| 8149 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8151 | /* output object */ |
| 8152 | PyObject *res = NULL; |
| 8153 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8154 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8155 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8156 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8157 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8158 | PyObject *errorHandler = NULL; |
| 8159 | PyObject *exc = NULL; |
| 8160 | /* the following variable is used for caching string comparisons |
| 8161 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8162 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8163 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8164 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8165 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8166 | return NULL; |
| 8167 | size = PyUnicode_GET_LENGTH(unicode); |
| 8168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8169 | /* Default to Latin-1 */ |
| 8170 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8171 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8173 | /* allocate enough for a simple encoding without |
| 8174 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8175 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8176 | if (res == NULL) |
| 8177 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8178 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8182 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8183 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8184 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | if (x==enc_EXCEPTION) /* error */ |
| 8186 | goto onError; |
| 8187 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8188 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | &exc, |
| 8190 | &known_errorHandler, &errorHandler, errors, |
| 8191 | &res, &respos)) { |
| 8192 | goto onError; |
| 8193 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8195 | else |
| 8196 | /* done with this character => adjust input position */ |
| 8197 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8200 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8201 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8202 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8203 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8204 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | Py_XDECREF(exc); |
| 8206 | Py_XDECREF(errorHandler); |
| 8207 | return res; |
| 8208 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8209 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8210 | Py_XDECREF(res); |
| 8211 | Py_XDECREF(exc); |
| 8212 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | return NULL; |
| 8214 | } |
| 8215 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8216 | /* Deprecated */ |
| 8217 | PyObject * |
| 8218 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8219 | Py_ssize_t size, |
| 8220 | PyObject *mapping, |
| 8221 | const char *errors) |
| 8222 | { |
| 8223 | PyObject *result; |
| 8224 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8225 | if (unicode == NULL) |
| 8226 | return NULL; |
| 8227 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8228 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8229 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8230 | } |
| 8231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8232 | PyObject * |
| 8233 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8234 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8235 | { |
| 8236 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | PyErr_BadArgument(); |
| 8238 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8240 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8241 | } |
| 8242 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8243 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8244 | static void |
| 8245 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8246 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8247 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8248 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8250 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8251 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8252 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8253 | } |
| 8254 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8255 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8256 | goto onError; |
| 8257 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8258 | goto onError; |
| 8259 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8260 | goto onError; |
| 8261 | return; |
| 8262 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8263 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | } |
| 8265 | } |
| 8266 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8267 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8268 | static void |
| 8269 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8270 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8271 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8272 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8273 | { |
| 8274 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8275 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8276 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8278 | } |
| 8279 | |
| 8280 | /* error handling callback helper: |
| 8281 | build arguments, call the callback and check the arguments, |
| 8282 | put the result into newpos and return the replacement string, which |
| 8283 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8284 | static PyObject * |
| 8285 | unicode_translate_call_errorhandler(const char *errors, |
| 8286 | PyObject **errorHandler, |
| 8287 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8288 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8289 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8290 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8291 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8292 | 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] | 8293 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8294 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8295 | PyObject *restuple; |
| 8296 | PyObject *resunicode; |
| 8297 | |
| 8298 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8299 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8300 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
| 8304 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8305 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8308 | |
| 8309 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8311 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8312 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8313 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8314 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8315 | Py_DECREF(restuple); |
| 8316 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8317 | } |
| 8318 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | &resunicode, &i_newpos)) { |
| 8320 | Py_DECREF(restuple); |
| 8321 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8323 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8324 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8325 | else |
| 8326 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8329 | Py_DECREF(restuple); |
| 8330 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8331 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8332 | Py_INCREF(resunicode); |
| 8333 | Py_DECREF(restuple); |
| 8334 | return resunicode; |
| 8335 | } |
| 8336 | |
| 8337 | /* Lookup the character ch in the mapping and put the result in result, |
| 8338 | which must be decrefed by the caller. |
| 8339 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8340 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8341 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8342 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8343 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8344 | PyObject *x; |
| 8345 | |
| 8346 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | x = PyObject_GetItem(mapping, w); |
| 8349 | Py_DECREF(w); |
| 8350 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8352 | /* No mapping found means: use 1:1 mapping. */ |
| 8353 | PyErr_Clear(); |
| 8354 | *result = NULL; |
| 8355 | return 0; |
| 8356 | } else |
| 8357 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8358 | } |
| 8359 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | *result = x; |
| 8361 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8363 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | long value = PyLong_AS_LONG(x); |
| 8365 | long max = PyUnicode_GetMax(); |
| 8366 | if (value < 0 || value > max) { |
| 8367 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8368 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8369 | Py_DECREF(x); |
| 8370 | return -1; |
| 8371 | } |
| 8372 | *result = x; |
| 8373 | return 0; |
| 8374 | } |
| 8375 | else if (PyUnicode_Check(x)) { |
| 8376 | *result = x; |
| 8377 | return 0; |
| 8378 | } |
| 8379 | else { |
| 8380 | /* wrong return value */ |
| 8381 | PyErr_SetString(PyExc_TypeError, |
| 8382 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8383 | Py_DECREF(x); |
| 8384 | return -1; |
| 8385 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8386 | } |
| 8387 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | if not reallocate and adjust various state variables. |
| 8389 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8390 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8391 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8392 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8394 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8395 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8396 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | /* exponentially overallocate to minimize reallocations */ |
| 8398 | if (requiredsize < 2 * oldsize) |
| 8399 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8400 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8401 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8403 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | } |
| 8406 | return 0; |
| 8407 | } |
| 8408 | /* lookup the character, put the result in the output string and adjust |
| 8409 | various state variables. Return a new reference to the object that |
| 8410 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8411 | undefined (in which case no character was written). |
| 8412 | The called must decref result. |
| 8413 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8414 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8415 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8416 | PyObject *mapping, Py_UCS4 **output, |
| 8417 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8418 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8419 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8420 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8421 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8423 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8424 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8425 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8426 | } |
| 8427 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8429 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | /* 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] | 8431 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8432 | } |
| 8433 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | Py_ssize_t repsize; |
| 8435 | if (PyUnicode_READY(*res) == -1) |
| 8436 | return -1; |
| 8437 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8438 | if (repsize==1) { |
| 8439 | /* 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] | 8440 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 | } |
| 8442 | else if (repsize!=0) { |
| 8443 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8444 | Py_ssize_t requiredsize = *opos + |
| 8445 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8447 | Py_ssize_t i; |
| 8448 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 | for(i = 0; i < repsize; i++) |
| 8451 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8453 | } |
| 8454 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8455 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8456 | return 0; |
| 8457 | } |
| 8458 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8459 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8461 | PyObject *mapping, |
| 8462 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | /* input object */ |
| 8465 | char *idata; |
| 8466 | Py_ssize_t size, i; |
| 8467 | int kind; |
| 8468 | /* output buffer */ |
| 8469 | Py_UCS4 *output = NULL; |
| 8470 | Py_ssize_t osize; |
| 8471 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8473 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | char *reason = "character maps to <undefined>"; |
| 8475 | PyObject *errorHandler = NULL; |
| 8476 | PyObject *exc = NULL; |
| 8477 | /* the following variable is used for caching string comparisons |
| 8478 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8479 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8480 | int known_errorHandler = -1; |
| 8481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8482 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | PyErr_BadArgument(); |
| 8484 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8485 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8486 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8487 | if (PyUnicode_READY(input) == -1) |
| 8488 | return NULL; |
| 8489 | idata = (char*)PyUnicode_DATA(input); |
| 8490 | kind = PyUnicode_KIND(input); |
| 8491 | size = PyUnicode_GET_LENGTH(input); |
| 8492 | i = 0; |
| 8493 | |
| 8494 | if (size == 0) { |
| 8495 | Py_INCREF(input); |
| 8496 | return input; |
| 8497 | } |
| 8498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8499 | /* allocate enough for a simple 1:1 translation without |
| 8500 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8501 | osize = size; |
| 8502 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8503 | opos = 0; |
| 8504 | if (output == NULL) { |
| 8505 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8506 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8507 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8510 | /* try to encode it */ |
| 8511 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8512 | if (charmaptranslate_output(input, i, mapping, |
| 8513 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | Py_XDECREF(x); |
| 8515 | goto onError; |
| 8516 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8517 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8519 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | else { /* untranslatable character */ |
| 8521 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8522 | Py_ssize_t repsize; |
| 8523 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | Py_ssize_t collstart = i; |
| 8527 | Py_ssize_t collend = i+1; |
| 8528 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8529 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8530 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | while (collend < size) { |
| 8532 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8533 | goto onError; |
| 8534 | Py_XDECREF(x); |
| 8535 | if (x!=Py_None) |
| 8536 | break; |
| 8537 | ++collend; |
| 8538 | } |
| 8539 | /* cache callback name lookup |
| 8540 | * (if not done yet, i.e. it's the first error) */ |
| 8541 | if (known_errorHandler==-1) { |
| 8542 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8543 | known_errorHandler = 1; |
| 8544 | else if (!strcmp(errors, "replace")) |
| 8545 | known_errorHandler = 2; |
| 8546 | else if (!strcmp(errors, "ignore")) |
| 8547 | known_errorHandler = 3; |
| 8548 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8549 | known_errorHandler = 4; |
| 8550 | else |
| 8551 | known_errorHandler = 0; |
| 8552 | } |
| 8553 | switch (known_errorHandler) { |
| 8554 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8555 | raise_translate_exception(&exc, input, collstart, |
| 8556 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8557 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | case 2: /* replace */ |
| 8559 | /* 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] | 8560 | for (coll = collstart; coll<collend; coll++) |
| 8561 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | /* fall through */ |
| 8563 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | break; |
| 8566 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8567 | /* generate replacement (temporarily (mis)uses i) */ |
| 8568 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | char buffer[2+29+1+1]; |
| 8570 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8572 | if (charmaptranslate_makespace(&output, &osize, |
| 8573 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8574 | goto onError; |
| 8575 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8576 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | break; |
| 8580 | default: |
| 8581 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8582 | reason, input, &exc, |
| 8583 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8584 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8585 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8586 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8587 | Py_DECREF(repunicode); |
| 8588 | goto onError; |
| 8589 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8591 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8592 | if (charmaptranslate_makespace(&output, &osize, |
| 8593 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | Py_DECREF(repunicode); |
| 8595 | goto onError; |
| 8596 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8597 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8598 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8599 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8600 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8601 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8602 | } |
| 8603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8605 | if (!res) |
| 8606 | goto onError; |
| 8607 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8608 | Py_XDECREF(exc); |
| 8609 | Py_XDECREF(errorHandler); |
| 8610 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8614 | Py_XDECREF(exc); |
| 8615 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8616 | return NULL; |
| 8617 | } |
| 8618 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8620 | PyObject * |
| 8621 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8622 | Py_ssize_t size, |
| 8623 | PyObject *mapping, |
| 8624 | const char *errors) |
| 8625 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8626 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8628 | if (!unicode) |
| 8629 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8630 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8631 | Py_DECREF(unicode); |
| 8632 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8633 | } |
| 8634 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8635 | PyObject * |
| 8636 | PyUnicode_Translate(PyObject *str, |
| 8637 | PyObject *mapping, |
| 8638 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | { |
| 8640 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8642 | str = PyUnicode_FromObject(str); |
| 8643 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8644 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8645 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8646 | Py_DECREF(str); |
| 8647 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8649 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8650 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8651 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | { |
| 8653 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8654 | called as a callback from fixup() which does it already. */ |
| 8655 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8656 | const int kind = PyUnicode_KIND(self); |
| 8657 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8658 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8659 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8660 | Py_ssize_t i; |
| 8661 | |
| 8662 | for (i = 0; i < len; ++i) { |
| 8663 | ch = PyUnicode_READ(kind, data, i); |
| 8664 | fixed = 0; |
| 8665 | if (ch > 127) { |
| 8666 | if (Py_UNICODE_ISSPACE(ch)) |
| 8667 | fixed = ' '; |
| 8668 | else { |
| 8669 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8670 | if (decimal >= 0) |
| 8671 | fixed = '0' + decimal; |
| 8672 | } |
| 8673 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8674 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8675 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8677 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8678 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8679 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8681 | } |
| 8682 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8683 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | } |
| 8685 | |
| 8686 | PyObject * |
| 8687 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8688 | { |
| 8689 | if (!PyUnicode_Check(unicode)) { |
| 8690 | PyErr_BadInternalCall(); |
| 8691 | return NULL; |
| 8692 | } |
| 8693 | if (PyUnicode_READY(unicode) == -1) |
| 8694 | return NULL; |
| 8695 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8696 | /* If the string is already ASCII, just return the same string */ |
| 8697 | Py_INCREF(unicode); |
| 8698 | return unicode; |
| 8699 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8700 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | } |
| 8702 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8703 | PyObject * |
| 8704 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8705 | Py_ssize_t length) |
| 8706 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8707 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8708 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8709 | Py_UCS4 maxchar; |
| 8710 | enum PyUnicode_Kind kind; |
| 8711 | void *data; |
| 8712 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8713 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8714 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8715 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8716 | if (ch > 127) { |
| 8717 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8718 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8719 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8720 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8721 | } |
| 8722 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8723 | |
| 8724 | /* Copy to a new string */ |
| 8725 | decimal = PyUnicode_New(length, maxchar); |
| 8726 | if (decimal == NULL) |
| 8727 | return decimal; |
| 8728 | kind = PyUnicode_KIND(decimal); |
| 8729 | data = PyUnicode_DATA(decimal); |
| 8730 | /* Iterate over code points */ |
| 8731 | for (i = 0; i < length; i++) { |
| 8732 | Py_UNICODE ch = s[i]; |
| 8733 | if (ch > 127) { |
| 8734 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8735 | if (decimal >= 0) |
| 8736 | ch = '0' + decimal; |
| 8737 | } |
| 8738 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8740 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8741 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8742 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8743 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8744 | int |
| 8745 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8746 | Py_ssize_t length, |
| 8747 | char *output, |
| 8748 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8749 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8750 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8751 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8752 | enum PyUnicode_Kind kind; |
| 8753 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8754 | |
| 8755 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8756 | PyErr_BadArgument(); |
| 8757 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8758 | } |
| 8759 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8760 | unicode = PyUnicode_FromUnicode(s, length); |
| 8761 | if (unicode == NULL) |
| 8762 | return -1; |
| 8763 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8764 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8765 | Py_DECREF(unicode); |
| 8766 | return -1; |
| 8767 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8768 | kind = PyUnicode_KIND(unicode); |
| 8769 | data = PyUnicode_DATA(unicode); |
| 8770 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8771 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8772 | PyObject *exc; |
| 8773 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8774 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8775 | Py_ssize_t startpos; |
| 8776 | |
| 8777 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8779 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8780 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8781 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8783 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8784 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8785 | if (decimal >= 0) { |
| 8786 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8787 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8788 | continue; |
| 8789 | } |
| 8790 | if (0 < ch && ch < 256) { |
| 8791 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8792 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8793 | continue; |
| 8794 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8795 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8796 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8797 | exc = NULL; |
| 8798 | raise_encode_exception(&exc, "decimal", unicode, |
| 8799 | startpos, startpos+1, |
| 8800 | "invalid decimal Unicode string"); |
| 8801 | Py_XDECREF(exc); |
| 8802 | Py_DECREF(unicode); |
| 8803 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8804 | } |
| 8805 | /* 0-terminate the output string */ |
| 8806 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8807 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8808 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8809 | } |
| 8810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8811 | /* --- Helpers ------------------------------------------------------------ */ |
| 8812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8813 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8814 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8815 | Py_ssize_t start, |
| 8816 | Py_ssize_t end) |
| 8817 | { |
| 8818 | int kind1, kind2, kind; |
| 8819 | void *buf1, *buf2; |
| 8820 | Py_ssize_t len1, len2, result; |
| 8821 | |
| 8822 | kind1 = PyUnicode_KIND(s1); |
| 8823 | kind2 = PyUnicode_KIND(s2); |
| 8824 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8825 | buf1 = PyUnicode_DATA(s1); |
| 8826 | buf2 = PyUnicode_DATA(s2); |
| 8827 | if (kind1 != kind) |
| 8828 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8829 | if (!buf1) |
| 8830 | return -2; |
| 8831 | if (kind2 != kind) |
| 8832 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8833 | if (!buf2) { |
| 8834 | if (kind1 != kind) PyMem_Free(buf1); |
| 8835 | return -2; |
| 8836 | } |
| 8837 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8838 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8839 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8840 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8841 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8842 | case PyUnicode_1BYTE_KIND: |
| 8843 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8844 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8845 | else |
| 8846 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8847 | break; |
| 8848 | case PyUnicode_2BYTE_KIND: |
| 8849 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8850 | break; |
| 8851 | case PyUnicode_4BYTE_KIND: |
| 8852 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8853 | break; |
| 8854 | default: |
| 8855 | assert(0); result = -2; |
| 8856 | } |
| 8857 | } |
| 8858 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8859 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8860 | case PyUnicode_1BYTE_KIND: |
| 8861 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8862 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8863 | else |
| 8864 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8865 | break; |
| 8866 | case PyUnicode_2BYTE_KIND: |
| 8867 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8868 | break; |
| 8869 | case PyUnicode_4BYTE_KIND: |
| 8870 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8871 | break; |
| 8872 | default: |
| 8873 | assert(0); result = -2; |
| 8874 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | } |
| 8876 | |
| 8877 | if (kind1 != kind) |
| 8878 | PyMem_Free(buf1); |
| 8879 | if (kind2 != kind) |
| 8880 | PyMem_Free(buf2); |
| 8881 | |
| 8882 | return result; |
| 8883 | } |
| 8884 | |
| 8885 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8886 | _PyUnicode_InsertThousandsGrouping( |
| 8887 | PyObject *unicode, Py_ssize_t index, |
| 8888 | Py_ssize_t n_buffer, |
| 8889 | void *digits, Py_ssize_t n_digits, |
| 8890 | Py_ssize_t min_width, |
| 8891 | const char *grouping, PyObject *thousands_sep, |
| 8892 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8894 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8895 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8896 | Py_ssize_t thousands_sep_len; |
| 8897 | Py_ssize_t len; |
| 8898 | |
| 8899 | if (unicode != NULL) { |
| 8900 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8901 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8902 | } |
| 8903 | else { |
| 8904 | kind = PyUnicode_1BYTE_KIND; |
| 8905 | data = NULL; |
| 8906 | } |
| 8907 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8908 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8909 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8910 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8911 | if (thousands_sep_kind < kind) { |
| 8912 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8913 | if (!thousands_sep_data) |
| 8914 | return -1; |
| 8915 | } |
| 8916 | else { |
| 8917 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8918 | if (!data) |
| 8919 | return -1; |
| 8920 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8921 | } |
| 8922 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8923 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8925 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8926 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8927 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8928 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8929 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8930 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8931 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8932 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8933 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8934 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8935 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8937 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8938 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8939 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8940 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8941 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8942 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8943 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8944 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8945 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8946 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8947 | break; |
| 8948 | default: |
| 8949 | assert(0); |
| 8950 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8952 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8953 | if (thousands_sep_kind < kind) |
| 8954 | PyMem_Free(thousands_sep_data); |
| 8955 | else |
| 8956 | PyMem_Free(data); |
| 8957 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8958 | if (unicode == NULL) { |
| 8959 | *maxchar = 127; |
| 8960 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8961 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8962 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8963 | } |
| 8964 | } |
| 8965 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8969 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8970 | #define ADJUST_INDICES(start, end, len) \ |
| 8971 | if (end > len) \ |
| 8972 | end = len; \ |
| 8973 | else if (end < 0) { \ |
| 8974 | end += len; \ |
| 8975 | if (end < 0) \ |
| 8976 | end = 0; \ |
| 8977 | } \ |
| 8978 | if (start < 0) { \ |
| 8979 | start += len; \ |
| 8980 | if (start < 0) \ |
| 8981 | start = 0; \ |
| 8982 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8984 | Py_ssize_t |
| 8985 | PyUnicode_Count(PyObject *str, |
| 8986 | PyObject *substr, |
| 8987 | Py_ssize_t start, |
| 8988 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8990 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8991 | PyObject* str_obj; |
| 8992 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8993 | int kind1, kind2, kind; |
| 8994 | void *buf1 = NULL, *buf2 = NULL; |
| 8995 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8996 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8997 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8998 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9000 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9001 | if (!sub_obj) { |
| 9002 | Py_DECREF(str_obj); |
| 9003 | return -1; |
| 9004 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9005 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9006 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9007 | Py_DECREF(str_obj); |
| 9008 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9010 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 | kind1 = PyUnicode_KIND(str_obj); |
| 9012 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9013 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9014 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9015 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9016 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9017 | if (kind2 > kind) { |
| 9018 | Py_DECREF(sub_obj); |
| 9019 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9020 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9021 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9022 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9023 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9024 | if (!buf2) |
| 9025 | goto onError; |
| 9026 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9027 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9028 | |
| 9029 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9030 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9031 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9032 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9033 | result = asciilib_count( |
| 9034 | ((Py_UCS1*)buf1) + start, end - start, |
| 9035 | buf2, len2, PY_SSIZE_T_MAX |
| 9036 | ); |
| 9037 | else |
| 9038 | result = ucs1lib_count( |
| 9039 | ((Py_UCS1*)buf1) + start, end - start, |
| 9040 | buf2, len2, PY_SSIZE_T_MAX |
| 9041 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | break; |
| 9043 | case PyUnicode_2BYTE_KIND: |
| 9044 | result = ucs2lib_count( |
| 9045 | ((Py_UCS2*)buf1) + start, end - start, |
| 9046 | buf2, len2, PY_SSIZE_T_MAX |
| 9047 | ); |
| 9048 | break; |
| 9049 | case PyUnicode_4BYTE_KIND: |
| 9050 | result = ucs4lib_count( |
| 9051 | ((Py_UCS4*)buf1) + start, end - start, |
| 9052 | buf2, len2, PY_SSIZE_T_MAX |
| 9053 | ); |
| 9054 | break; |
| 9055 | default: |
| 9056 | assert(0); result = 0; |
| 9057 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9058 | |
| 9059 | Py_DECREF(sub_obj); |
| 9060 | Py_DECREF(str_obj); |
| 9061 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9062 | if (kind2 != kind) |
| 9063 | PyMem_Free(buf2); |
| 9064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9065 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9066 | onError: |
| 9067 | Py_DECREF(sub_obj); |
| 9068 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | if (kind2 != kind && buf2) |
| 9070 | PyMem_Free(buf2); |
| 9071 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9072 | } |
| 9073 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9074 | Py_ssize_t |
| 9075 | PyUnicode_Find(PyObject *str, |
| 9076 | PyObject *sub, |
| 9077 | Py_ssize_t start, |
| 9078 | Py_ssize_t end, |
| 9079 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9080 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9081 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9083 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9084 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9085 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9086 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9087 | if (!sub) { |
| 9088 | Py_DECREF(str); |
| 9089 | return -2; |
| 9090 | } |
| 9091 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9092 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9093 | Py_DECREF(str); |
| 9094 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9095 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9096 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9097 | result = any_find_slice(direction, |
| 9098 | str, sub, start, end |
| 9099 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9100 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9101 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9102 | Py_DECREF(sub); |
| 9103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9104 | return result; |
| 9105 | } |
| 9106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9107 | Py_ssize_t |
| 9108 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9109 | Py_ssize_t start, Py_ssize_t end, |
| 9110 | int direction) |
| 9111 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9113 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9114 | if (PyUnicode_READY(str) == -1) |
| 9115 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9116 | if (start < 0 || end < 0) { |
| 9117 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9118 | return -2; |
| 9119 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9121 | end = PyUnicode_GET_LENGTH(str); |
| 9122 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9123 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9124 | kind, end-start, ch, direction); |
| 9125 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9126 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9127 | else |
| 9128 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9129 | } |
| 9130 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9131 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9132 | tailmatch(PyObject *self, |
| 9133 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9134 | Py_ssize_t start, |
| 9135 | Py_ssize_t end, |
| 9136 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9137 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9138 | int kind_self; |
| 9139 | int kind_sub; |
| 9140 | void *data_self; |
| 9141 | void *data_sub; |
| 9142 | Py_ssize_t offset; |
| 9143 | Py_ssize_t i; |
| 9144 | Py_ssize_t end_sub; |
| 9145 | |
| 9146 | if (PyUnicode_READY(self) == -1 || |
| 9147 | PyUnicode_READY(substring) == -1) |
| 9148 | return 0; |
| 9149 | |
| 9150 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | return 1; |
| 9152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9153 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9154 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9156 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9158 | kind_self = PyUnicode_KIND(self); |
| 9159 | data_self = PyUnicode_DATA(self); |
| 9160 | kind_sub = PyUnicode_KIND(substring); |
| 9161 | data_sub = PyUnicode_DATA(substring); |
| 9162 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9163 | |
| 9164 | if (direction > 0) |
| 9165 | offset = end; |
| 9166 | else |
| 9167 | offset = start; |
| 9168 | |
| 9169 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9170 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9171 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9172 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9173 | /* If both are of the same kind, memcmp is sufficient */ |
| 9174 | if (kind_self == kind_sub) { |
| 9175 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9176 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9177 | data_sub, |
| 9178 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9179 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9180 | } |
| 9181 | /* otherwise we have to compare each character by first accesing it */ |
| 9182 | else { |
| 9183 | /* We do not need to compare 0 and len(substring)-1 because |
| 9184 | the if statement above ensured already that they are equal |
| 9185 | when we end up here. */ |
Antoine Pitrou | 057119b | 2012-09-02 17:56:33 +0200 | [diff] [blame] | 9186 | /* TODO: honor direction and do a forward or backwards search */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9187 | for (i = 1; i < end_sub; ++i) { |
| 9188 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9189 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9190 | return 0; |
| 9191 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9192 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9193 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | return 0; |
| 9197 | } |
| 9198 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9199 | Py_ssize_t |
| 9200 | PyUnicode_Tailmatch(PyObject *str, |
| 9201 | PyObject *substr, |
| 9202 | Py_ssize_t start, |
| 9203 | Py_ssize_t end, |
| 9204 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9206 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9208 | str = PyUnicode_FromObject(str); |
| 9209 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9210 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9211 | substr = PyUnicode_FromObject(substr); |
| 9212 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9213 | Py_DECREF(str); |
| 9214 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9216 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9217 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9218 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9219 | Py_DECREF(str); |
| 9220 | Py_DECREF(substr); |
| 9221 | return result; |
| 9222 | } |
| 9223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9224 | /* Apply fixfct filter to the Unicode object self and return a |
| 9225 | reference to the modified object */ |
| 9226 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9227 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9228 | fixup(PyObject *self, |
| 9229 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9230 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9231 | PyObject *u; |
| 9232 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9233 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9234 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9235 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9237 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9238 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9240 | /* fix functions return the new maximum character in a string, |
| 9241 | if the kind of the resulting unicode object does not change, |
| 9242 | everything is fine. Otherwise we need to change the string kind |
| 9243 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9244 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9245 | |
| 9246 | if (maxchar_new == 0) { |
| 9247 | /* no changes */; |
| 9248 | if (PyUnicode_CheckExact(self)) { |
| 9249 | Py_DECREF(u); |
| 9250 | Py_INCREF(self); |
| 9251 | return self; |
| 9252 | } |
| 9253 | else |
| 9254 | return u; |
| 9255 | } |
| 9256 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9257 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9259 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9260 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9261 | |
| 9262 | /* In case the maximum character changed, we need to |
| 9263 | convert the string to the new category. */ |
| 9264 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9265 | if (v == NULL) { |
| 9266 | Py_DECREF(u); |
| 9267 | return NULL; |
| 9268 | } |
| 9269 | if (maxchar_new > maxchar_old) { |
| 9270 | /* If the maxchar increased so that the kind changed, not all |
| 9271 | characters are representable anymore and we need to fix the |
| 9272 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9273 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9274 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9275 | maxchar_old = fixfct(v); |
| 9276 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | } |
| 9278 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9279 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9280 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9282 | Py_DECREF(u); |
| 9283 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9284 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9287 | static PyObject * |
| 9288 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9290 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9291 | char *resdata, *data = PyUnicode_DATA(self); |
| 9292 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9293 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9294 | res = PyUnicode_New(len, 127); |
| 9295 | if (res == NULL) |
| 9296 | return NULL; |
| 9297 | resdata = PyUnicode_DATA(res); |
| 9298 | if (lower) |
| 9299 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9301 | _Py_bytes_upper(resdata, data, len); |
| 9302 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9303 | } |
| 9304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9306 | handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9307 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9308 | Py_ssize_t j; |
| 9309 | int final_sigma; |
| 9310 | Py_UCS4 c; |
| 9311 | /* U+03A3 is in the Final_Sigma context when, it is found like this: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9312 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9313 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9314 | |
| 9315 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9316 | */ |
| 9317 | for (j = i - 1; j >= 0; j--) { |
| 9318 | c = PyUnicode_READ(kind, data, j); |
| 9319 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9320 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9322 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9323 | if (final_sigma) { |
| 9324 | for (j = i + 1; j < length; j++) { |
| 9325 | c = PyUnicode_READ(kind, data, j); |
| 9326 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9327 | break; |
| 9328 | } |
| 9329 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9330 | } |
| 9331 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9332 | } |
| 9333 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9334 | static int |
| 9335 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9336 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9338 | /* Obscure special case. */ |
| 9339 | if (c == 0x3A3) { |
| 9340 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9341 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9343 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | } |
| 9345 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9346 | static Py_ssize_t |
| 9347 | do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9348 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9349 | Py_ssize_t i, k = 0; |
| 9350 | int n_res, j; |
| 9351 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9352 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9353 | c = PyUnicode_READ(kind, data, 0); |
| 9354 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9355 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9356 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9357 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9358 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9359 | for (i = 1; i < length; i++) { |
| 9360 | c = PyUnicode_READ(kind, data, i); |
| 9361 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9362 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9363 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9364 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9365 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9366 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9367 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9370 | static Py_ssize_t |
| 9371 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9372 | Py_ssize_t i, k = 0; |
| 9373 | |
| 9374 | for (i = 0; i < length; i++) { |
| 9375 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9376 | int n_res, j; |
| 9377 | if (Py_UNICODE_ISUPPER(c)) { |
| 9378 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9379 | } |
| 9380 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9381 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9382 | } |
| 9383 | else { |
| 9384 | n_res = 1; |
| 9385 | mapped[0] = c; |
| 9386 | } |
| 9387 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9388 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9389 | res[k++] = mapped[j]; |
| 9390 | } |
| 9391 | } |
| 9392 | return k; |
| 9393 | } |
| 9394 | |
| 9395 | static Py_ssize_t |
| 9396 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9397 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9398 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9399 | Py_ssize_t i, k = 0; |
| 9400 | |
| 9401 | for (i = 0; i < length; i++) { |
| 9402 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9403 | int n_res, j; |
| 9404 | if (lower) |
| 9405 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9406 | else |
| 9407 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9408 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9409 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9410 | res[k++] = mapped[j]; |
| 9411 | } |
| 9412 | } |
| 9413 | return k; |
| 9414 | } |
| 9415 | |
| 9416 | static Py_ssize_t |
| 9417 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9418 | { |
| 9419 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9420 | } |
| 9421 | |
| 9422 | static Py_ssize_t |
| 9423 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9424 | { |
| 9425 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9426 | } |
| 9427 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9428 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9429 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9430 | { |
| 9431 | Py_ssize_t i, k = 0; |
| 9432 | |
| 9433 | for (i = 0; i < length; i++) { |
| 9434 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9435 | Py_UCS4 mapped[3]; |
| 9436 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9437 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9438 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9439 | res[k++] = mapped[j]; |
| 9440 | } |
| 9441 | } |
| 9442 | return k; |
| 9443 | } |
| 9444 | |
| 9445 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9446 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9447 | { |
| 9448 | Py_ssize_t i, k = 0; |
| 9449 | int previous_is_cased; |
| 9450 | |
| 9451 | previous_is_cased = 0; |
| 9452 | for (i = 0; i < length; i++) { |
| 9453 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9454 | Py_UCS4 mapped[3]; |
| 9455 | int n_res, j; |
| 9456 | |
| 9457 | if (previous_is_cased) |
| 9458 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9459 | else |
| 9460 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9461 | |
| 9462 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9463 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9464 | res[k++] = mapped[j]; |
| 9465 | } |
| 9466 | |
| 9467 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9468 | } |
| 9469 | return k; |
| 9470 | } |
| 9471 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9472 | static PyObject * |
| 9473 | case_operation(PyObject *self, |
| 9474 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9475 | { |
| 9476 | PyObject *res = NULL; |
| 9477 | Py_ssize_t length, newlength = 0; |
| 9478 | int kind, outkind; |
| 9479 | void *data, *outdata; |
| 9480 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9481 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9482 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9483 | |
| 9484 | kind = PyUnicode_KIND(self); |
| 9485 | data = PyUnicode_DATA(self); |
| 9486 | length = PyUnicode_GET_LENGTH(self); |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame^] | 9487 | if (length > PY_SSIZE_T_MAX / 3 || |
| 9488 | length > PY_SIZE_MAX / (3 * sizeof(Py_UCS4))) { |
| 9489 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9490 | return NULL; |
| 9491 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9492 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9493 | if (tmp == NULL) |
| 9494 | return PyErr_NoMemory(); |
| 9495 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9496 | res = PyUnicode_New(newlength, maxchar); |
| 9497 | if (res == NULL) |
| 9498 | goto leave; |
| 9499 | tmpend = tmp + newlength; |
| 9500 | outdata = PyUnicode_DATA(res); |
| 9501 | outkind = PyUnicode_KIND(res); |
| 9502 | switch (outkind) { |
| 9503 | case PyUnicode_1BYTE_KIND: |
| 9504 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9505 | break; |
| 9506 | case PyUnicode_2BYTE_KIND: |
| 9507 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9508 | break; |
| 9509 | case PyUnicode_4BYTE_KIND: |
| 9510 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9511 | break; |
| 9512 | default: |
| 9513 | assert(0); |
| 9514 | break; |
| 9515 | } |
| 9516 | leave: |
| 9517 | PyMem_FREE(tmp); |
| 9518 | return res; |
| 9519 | } |
| 9520 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9521 | PyObject * |
| 9522 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9524 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9525 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9526 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9527 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9528 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9529 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9530 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9531 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9532 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9534 | int use_memcpy; |
| 9535 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9536 | PyObject *last_obj; |
| 9537 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9538 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9539 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9540 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9541 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9542 | } |
| 9543 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9544 | /* NOTE: the following code can't call back into Python code, |
| 9545 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9546 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9547 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9548 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9549 | /* If empty sequence, return u"". */ |
| 9550 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9551 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9552 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9553 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9554 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9555 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9556 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9557 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9558 | if (seqlen == 1) { |
| 9559 | if (PyUnicode_CheckExact(items[0])) { |
| 9560 | res = items[0]; |
| 9561 | Py_INCREF(res); |
| 9562 | Py_DECREF(fseq); |
| 9563 | return res; |
| 9564 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9565 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9566 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9567 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9568 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9569 | /* Set up sep and seplen */ |
| 9570 | if (separator == NULL) { |
| 9571 | /* fall back to a blank space separator */ |
| 9572 | sep = PyUnicode_FromOrdinal(' '); |
| 9573 | if (!sep) |
| 9574 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9575 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9576 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9577 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9578 | else { |
| 9579 | if (!PyUnicode_Check(separator)) { |
| 9580 | PyErr_Format(PyExc_TypeError, |
| 9581 | "separator: expected str instance," |
| 9582 | " %.80s found", |
| 9583 | Py_TYPE(separator)->tp_name); |
| 9584 | goto onError; |
| 9585 | } |
| 9586 | if (PyUnicode_READY(separator)) |
| 9587 | goto onError; |
| 9588 | sep = separator; |
| 9589 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9590 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9591 | /* inc refcount to keep this code path symmetric with the |
| 9592 | above case of a blank separator */ |
| 9593 | Py_INCREF(sep); |
| 9594 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9595 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9596 | } |
| 9597 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9598 | /* There are at least two things to join, or else we have a subclass |
| 9599 | * of str in the sequence. |
| 9600 | * Do a pre-pass to figure out the total amount of space we'll |
| 9601 | * need (sz), and see whether all argument are strings. |
| 9602 | */ |
| 9603 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9604 | #ifdef Py_DEBUG |
| 9605 | use_memcpy = 0; |
| 9606 | #else |
| 9607 | use_memcpy = 1; |
| 9608 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9609 | for (i = 0; i < seqlen; i++) { |
| 9610 | const Py_ssize_t old_sz = sz; |
| 9611 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | if (!PyUnicode_Check(item)) { |
| 9613 | PyErr_Format(PyExc_TypeError, |
| 9614 | "sequence item %zd: expected str instance," |
| 9615 | " %.80s found", |
| 9616 | i, Py_TYPE(item)->tp_name); |
| 9617 | goto onError; |
| 9618 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9619 | if (PyUnicode_READY(item) == -1) |
| 9620 | goto onError; |
| 9621 | sz += PyUnicode_GET_LENGTH(item); |
| 9622 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9623 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9624 | if (i != 0) |
| 9625 | sz += seplen; |
| 9626 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9627 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9628 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9629 | goto onError; |
| 9630 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9631 | if (use_memcpy && last_obj != NULL) { |
| 9632 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9633 | use_memcpy = 0; |
| 9634 | } |
| 9635 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9636 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9638 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9639 | if (res == NULL) |
| 9640 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9641 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9642 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9643 | #ifdef Py_DEBUG |
| 9644 | use_memcpy = 0; |
| 9645 | #else |
| 9646 | if (use_memcpy) { |
| 9647 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9648 | kind = PyUnicode_KIND(res); |
| 9649 | if (seplen != 0) |
| 9650 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9651 | } |
| 9652 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9653 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9654 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9655 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9656 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9657 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9658 | if (use_memcpy) { |
| 9659 | Py_MEMCPY(res_data, |
| 9660 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9661 | kind * seplen); |
| 9662 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9663 | } |
| 9664 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9665 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9666 | res_offset += seplen; |
| 9667 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9668 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9669 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9670 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9671 | if (use_memcpy) { |
| 9672 | Py_MEMCPY(res_data, |
| 9673 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9674 | kind * itemlen); |
| 9675 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9676 | } |
| 9677 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9678 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9679 | res_offset += itemlen; |
| 9680 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9681 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9682 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9683 | if (use_memcpy) |
| 9684 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9685 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9686 | else |
| 9687 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9688 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9689 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9690 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9691 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9693 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9694 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9695 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9696 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9697 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9698 | return NULL; |
| 9699 | } |
| 9700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9701 | #define FILL(kind, data, value, start, length) \ |
| 9702 | do { \ |
| 9703 | Py_ssize_t i_ = 0; \ |
| 9704 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9705 | switch ((kind)) { \ |
| 9706 | case PyUnicode_1BYTE_KIND: { \ |
| 9707 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9708 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | break; \ |
| 9710 | } \ |
| 9711 | case PyUnicode_2BYTE_KIND: { \ |
| 9712 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9713 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9714 | break; \ |
| 9715 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9716 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9717 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9718 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9719 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9720 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | } \ |
| 9722 | } \ |
| 9723 | } while (0) |
| 9724 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9725 | void |
| 9726 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9727 | Py_UCS4 fill_char) |
| 9728 | { |
| 9729 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9730 | const void *data = PyUnicode_DATA(unicode); |
| 9731 | assert(PyUnicode_IS_READY(unicode)); |
| 9732 | assert(unicode_modifiable(unicode)); |
| 9733 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9734 | assert(start >= 0); |
| 9735 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9736 | FILL(kind, data, fill_char, start, length); |
| 9737 | } |
| 9738 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9739 | Py_ssize_t |
| 9740 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9741 | Py_UCS4 fill_char) |
| 9742 | { |
| 9743 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9744 | |
| 9745 | if (!PyUnicode_Check(unicode)) { |
| 9746 | PyErr_BadInternalCall(); |
| 9747 | return -1; |
| 9748 | } |
| 9749 | if (PyUnicode_READY(unicode) == -1) |
| 9750 | return -1; |
| 9751 | if (unicode_check_modifiable(unicode)) |
| 9752 | return -1; |
| 9753 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9754 | if (start < 0) { |
| 9755 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9756 | return -1; |
| 9757 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9758 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9759 | PyErr_SetString(PyExc_ValueError, |
| 9760 | "fill character is bigger than " |
| 9761 | "the string maximum character"); |
| 9762 | return -1; |
| 9763 | } |
| 9764 | |
| 9765 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9766 | length = Py_MIN(maxlen, length); |
| 9767 | if (length <= 0) |
| 9768 | return 0; |
| 9769 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9770 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9771 | return length; |
| 9772 | } |
| 9773 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9774 | static PyObject * |
| 9775 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9776 | Py_ssize_t left, |
| 9777 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9778 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9779 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9780 | PyObject *u; |
| 9781 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9782 | int kind; |
| 9783 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9784 | |
| 9785 | if (left < 0) |
| 9786 | left = 0; |
| 9787 | if (right < 0) |
| 9788 | right = 0; |
| 9789 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9790 | if (left == 0 && right == 0) |
| 9791 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9793 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9794 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9795 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9796 | return NULL; |
| 9797 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9798 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9799 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9800 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9801 | if (!u) |
| 9802 | return NULL; |
| 9803 | |
| 9804 | kind = PyUnicode_KIND(u); |
| 9805 | data = PyUnicode_DATA(u); |
| 9806 | if (left) |
| 9807 | FILL(kind, data, fill, 0, left); |
| 9808 | if (right) |
| 9809 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9810 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9811 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9812 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9813 | } |
| 9814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9815 | PyObject * |
| 9816 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9817 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9818 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9819 | |
| 9820 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9821 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9822 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9823 | if (PyUnicode_READY(string) == -1) { |
| 9824 | Py_DECREF(string); |
| 9825 | return NULL; |
| 9826 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9827 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9828 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9830 | if (PyUnicode_IS_ASCII(string)) |
| 9831 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9832 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9833 | PyUnicode_GET_LENGTH(string), keepends); |
| 9834 | else |
| 9835 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9836 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9837 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | break; |
| 9839 | case PyUnicode_2BYTE_KIND: |
| 9840 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9841 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9842 | PyUnicode_GET_LENGTH(string), keepends); |
| 9843 | break; |
| 9844 | case PyUnicode_4BYTE_KIND: |
| 9845 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9846 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | PyUnicode_GET_LENGTH(string), keepends); |
| 9848 | break; |
| 9849 | default: |
| 9850 | assert(0); |
| 9851 | list = 0; |
| 9852 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9853 | Py_DECREF(string); |
| 9854 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9855 | } |
| 9856 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9857 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9858 | split(PyObject *self, |
| 9859 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9860 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9861 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | int kind1, kind2, kind; |
| 9863 | void *buf1, *buf2; |
| 9864 | Py_ssize_t len1, len2; |
| 9865 | PyObject* out; |
| 9866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9867 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9868 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | if (PyUnicode_READY(self) == -1) |
| 9871 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9874 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9876 | if (PyUnicode_IS_ASCII(self)) |
| 9877 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9878 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9879 | PyUnicode_GET_LENGTH(self), maxcount |
| 9880 | ); |
| 9881 | else |
| 9882 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9883 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9884 | PyUnicode_GET_LENGTH(self), maxcount |
| 9885 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | case PyUnicode_2BYTE_KIND: |
| 9887 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9888 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9889 | PyUnicode_GET_LENGTH(self), maxcount |
| 9890 | ); |
| 9891 | case PyUnicode_4BYTE_KIND: |
| 9892 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9893 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9894 | PyUnicode_GET_LENGTH(self), maxcount |
| 9895 | ); |
| 9896 | default: |
| 9897 | assert(0); |
| 9898 | return NULL; |
| 9899 | } |
| 9900 | |
| 9901 | if (PyUnicode_READY(substring) == -1) |
| 9902 | return NULL; |
| 9903 | |
| 9904 | kind1 = PyUnicode_KIND(self); |
| 9905 | kind2 = PyUnicode_KIND(substring); |
| 9906 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9907 | buf1 = PyUnicode_DATA(self); |
| 9908 | buf2 = PyUnicode_DATA(substring); |
| 9909 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9910 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | if (!buf1) |
| 9912 | return NULL; |
| 9913 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9914 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | if (!buf2) { |
| 9916 | if (kind1 != kind) PyMem_Free(buf1); |
| 9917 | return NULL; |
| 9918 | } |
| 9919 | len1 = PyUnicode_GET_LENGTH(self); |
| 9920 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9921 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9922 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9923 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9924 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9925 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9926 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9927 | else |
| 9928 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9929 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9930 | break; |
| 9931 | case PyUnicode_2BYTE_KIND: |
| 9932 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9933 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | break; |
| 9935 | case PyUnicode_4BYTE_KIND: |
| 9936 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9937 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | break; |
| 9939 | default: |
| 9940 | out = NULL; |
| 9941 | } |
| 9942 | if (kind1 != kind) |
| 9943 | PyMem_Free(buf1); |
| 9944 | if (kind2 != kind) |
| 9945 | PyMem_Free(buf2); |
| 9946 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | } |
| 9948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9949 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9950 | rsplit(PyObject *self, |
| 9951 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9952 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9953 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9954 | int kind1, kind2, kind; |
| 9955 | void *buf1, *buf2; |
| 9956 | Py_ssize_t len1, len2; |
| 9957 | PyObject* out; |
| 9958 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9959 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9960 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | if (PyUnicode_READY(self) == -1) |
| 9963 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9964 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9965 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9966 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9968 | if (PyUnicode_IS_ASCII(self)) |
| 9969 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9970 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9971 | PyUnicode_GET_LENGTH(self), maxcount |
| 9972 | ); |
| 9973 | else |
| 9974 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9975 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9976 | PyUnicode_GET_LENGTH(self), maxcount |
| 9977 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9978 | case PyUnicode_2BYTE_KIND: |
| 9979 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9980 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | PyUnicode_GET_LENGTH(self), maxcount |
| 9982 | ); |
| 9983 | case PyUnicode_4BYTE_KIND: |
| 9984 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9985 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | PyUnicode_GET_LENGTH(self), maxcount |
| 9987 | ); |
| 9988 | default: |
| 9989 | assert(0); |
| 9990 | return NULL; |
| 9991 | } |
| 9992 | |
| 9993 | if (PyUnicode_READY(substring) == -1) |
| 9994 | return NULL; |
| 9995 | |
| 9996 | kind1 = PyUnicode_KIND(self); |
| 9997 | kind2 = PyUnicode_KIND(substring); |
| 9998 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9999 | buf1 = PyUnicode_DATA(self); |
| 10000 | buf2 = PyUnicode_DATA(substring); |
| 10001 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10002 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 | if (!buf1) |
| 10004 | return NULL; |
| 10005 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10006 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | if (!buf2) { |
| 10008 | if (kind1 != kind) PyMem_Free(buf1); |
| 10009 | return NULL; |
| 10010 | } |
| 10011 | len1 = PyUnicode_GET_LENGTH(self); |
| 10012 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10013 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10014 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10016 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10017 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10018 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10019 | else |
| 10020 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10021 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | break; |
| 10023 | case PyUnicode_2BYTE_KIND: |
| 10024 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10025 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10026 | break; |
| 10027 | case PyUnicode_4BYTE_KIND: |
| 10028 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10029 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | break; |
| 10031 | default: |
| 10032 | out = NULL; |
| 10033 | } |
| 10034 | if (kind1 != kind) |
| 10035 | PyMem_Free(buf1); |
| 10036 | if (kind2 != kind) |
| 10037 | PyMem_Free(buf2); |
| 10038 | return out; |
| 10039 | } |
| 10040 | |
| 10041 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10042 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10043 | 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] | 10044 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10045 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10047 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10048 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10049 | else |
| 10050 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | case PyUnicode_2BYTE_KIND: |
| 10052 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10053 | case PyUnicode_4BYTE_KIND: |
| 10054 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10055 | } |
| 10056 | assert(0); |
| 10057 | return -1; |
| 10058 | } |
| 10059 | |
| 10060 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10061 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10062 | 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] | 10063 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10064 | switch (kind) { |
| 10065 | case PyUnicode_1BYTE_KIND: |
| 10066 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10067 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10068 | else |
| 10069 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10070 | case PyUnicode_2BYTE_KIND: |
| 10071 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10072 | case PyUnicode_4BYTE_KIND: |
| 10073 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10074 | } |
| 10075 | assert(0); |
| 10076 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10077 | } |
| 10078 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10079 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | replace(PyObject *self, PyObject *str1, |
| 10081 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10082 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10083 | PyObject *u; |
| 10084 | char *sbuf = PyUnicode_DATA(self); |
| 10085 | char *buf1 = PyUnicode_DATA(str1); |
| 10086 | char *buf2 = PyUnicode_DATA(str2); |
| 10087 | int srelease = 0, release1 = 0, release2 = 0; |
| 10088 | int skind = PyUnicode_KIND(self); |
| 10089 | int kind1 = PyUnicode_KIND(str1); |
| 10090 | int kind2 = PyUnicode_KIND(str2); |
| 10091 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10092 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10093 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10094 | int mayshrink; |
| 10095 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10096 | |
| 10097 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10098 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10099 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10100 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10101 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10102 | if (str1 == str2) |
| 10103 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | if (skind < kind1) |
| 10105 | /* substring too wide to be present */ |
| 10106 | goto nothing; |
| 10107 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10108 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10109 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10110 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10111 | result string. */ |
| 10112 | mayshrink = (maxchar_str2 < maxchar); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10113 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10116 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10118 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10120 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10121 | Py_UCS4 u1, u2; |
| 10122 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10123 | Py_ssize_t index, pos; |
| 10124 | char *src; |
| 10125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10126 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10127 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10128 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10129 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10132 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10133 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10134 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10136 | |
| 10137 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10138 | index = 0; |
| 10139 | src = sbuf; |
| 10140 | while (--maxcount) |
| 10141 | { |
| 10142 | pos++; |
| 10143 | src += pos * PyUnicode_KIND(self); |
| 10144 | slen -= pos; |
| 10145 | index += pos; |
| 10146 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10147 | if (pos < 0) |
| 10148 | break; |
| 10149 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10150 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10151 | } |
| 10152 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | int rkind = skind; |
| 10154 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10155 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10156 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | if (kind1 < rkind) { |
| 10158 | /* widen substring */ |
| 10159 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10160 | if (!buf1) goto error; |
| 10161 | release1 = 1; |
| 10162 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10163 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10164 | if (i < 0) |
| 10165 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | if (rkind > kind2) { |
| 10167 | /* widen replacement */ |
| 10168 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10169 | if (!buf2) goto error; |
| 10170 | release2 = 1; |
| 10171 | } |
| 10172 | else if (rkind < kind2) { |
| 10173 | /* widen self and buf1 */ |
| 10174 | rkind = kind2; |
| 10175 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10176 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10178 | if (!sbuf) goto error; |
| 10179 | srelease = 1; |
| 10180 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10181 | if (!buf1) goto error; |
| 10182 | release1 = 1; |
| 10183 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10184 | u = PyUnicode_New(slen, maxchar); |
| 10185 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10187 | assert(PyUnicode_KIND(u) == rkind); |
| 10188 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10189 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10190 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10191 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10192 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10194 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10196 | |
| 10197 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10198 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10199 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10200 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10201 | if (i == -1) |
| 10202 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10203 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10204 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10205 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10207 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10208 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10209 | } |
| 10210 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10212 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | int rkind = skind; |
| 10214 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10215 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10216 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10217 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10219 | if (!buf1) goto error; |
| 10220 | release1 = 1; |
| 10221 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10222 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10223 | if (n == 0) |
| 10224 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10226 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10228 | if (!buf2) goto error; |
| 10229 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10230 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10231 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10232 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | rkind = kind2; |
| 10234 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10235 | if (!sbuf) goto error; |
| 10236 | srelease = 1; |
| 10237 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10238 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10240 | if (!buf1) goto error; |
| 10241 | release1 = 1; |
| 10242 | } |
| 10243 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10244 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10245 | if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | PyErr_SetString(PyExc_OverflowError, |
| 10247 | "replace string is too long"); |
| 10248 | goto error; |
| 10249 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10250 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10251 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10252 | _Py_INCREF_UNICODE_EMPTY(); |
| 10253 | if (!unicode_empty) |
| 10254 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10255 | u = unicode_empty; |
| 10256 | goto done; |
| 10257 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10258 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | PyErr_SetString(PyExc_OverflowError, |
| 10260 | "replace string is too long"); |
| 10261 | goto error; |
| 10262 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10263 | u = PyUnicode_New(new_size, maxchar); |
| 10264 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10265 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10266 | assert(PyUnicode_KIND(u) == rkind); |
| 10267 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10268 | ires = i = 0; |
| 10269 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10270 | while (n-- > 0) { |
| 10271 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10272 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10273 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10274 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10275 | if (j == -1) |
| 10276 | break; |
| 10277 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10278 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10279 | memcpy(res + rkind * ires, |
| 10280 | sbuf + rkind * i, |
| 10281 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10283 | } |
| 10284 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10286 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10288 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10290 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10292 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10294 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10295 | memcpy(res + rkind * ires, |
| 10296 | sbuf + rkind * i, |
| 10297 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10298 | } |
| 10299 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10300 | /* interleave */ |
| 10301 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10302 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10303 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10304 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10306 | if (--n <= 0) |
| 10307 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10308 | memcpy(res + rkind * ires, |
| 10309 | sbuf + rkind * i, |
| 10310 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | ires++; |
| 10312 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10313 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10314 | memcpy(res + rkind * ires, |
| 10315 | sbuf + rkind * i, |
| 10316 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10317 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10318 | } |
| 10319 | |
| 10320 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10321 | unicode_adjust_maxchar(&u); |
| 10322 | if (u == NULL) |
| 10323 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10324 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10325 | |
| 10326 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10327 | if (srelease) |
| 10328 | PyMem_FREE(sbuf); |
| 10329 | if (release1) |
| 10330 | PyMem_FREE(buf1); |
| 10331 | if (release2) |
| 10332 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10333 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10334 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10335 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10336 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10337 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | if (srelease) |
| 10339 | PyMem_FREE(sbuf); |
| 10340 | if (release1) |
| 10341 | PyMem_FREE(buf1); |
| 10342 | if (release2) |
| 10343 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10344 | return unicode_result_unchanged(self); |
| 10345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10346 | error: |
| 10347 | if (srelease && sbuf) |
| 10348 | PyMem_FREE(sbuf); |
| 10349 | if (release1 && buf1) |
| 10350 | PyMem_FREE(buf1); |
| 10351 | if (release2 && buf2) |
| 10352 | PyMem_FREE(buf2); |
| 10353 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10354 | } |
| 10355 | |
| 10356 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10358 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10359 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10360 | \n\ |
| 10361 | 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] | 10362 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10363 | |
| 10364 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10365 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10366 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10367 | if (PyUnicode_READY(self) == -1) |
| 10368 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10369 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10372 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10373 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | \n\ |
| 10375 | 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] | 10376 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | |
| 10378 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10379 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10380 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10381 | if (PyUnicode_READY(self) == -1) |
| 10382 | return NULL; |
| 10383 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10384 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10385 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | } |
| 10387 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10388 | PyDoc_STRVAR(casefold__doc__, |
| 10389 | "S.casefold() -> str\n\ |
| 10390 | \n\ |
| 10391 | Return a version of S suitable for caseless comparisons."); |
| 10392 | |
| 10393 | static PyObject * |
| 10394 | unicode_casefold(PyObject *self) |
| 10395 | { |
| 10396 | if (PyUnicode_READY(self) == -1) |
| 10397 | return NULL; |
| 10398 | if (PyUnicode_IS_ASCII(self)) |
| 10399 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10400 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10401 | } |
| 10402 | |
| 10403 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10404 | /* Argument converter. Coerces to a single unicode character */ |
| 10405 | |
| 10406 | static int |
| 10407 | convert_uc(PyObject *obj, void *addr) |
| 10408 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10409 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10410 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10411 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10412 | uniobj = PyUnicode_FromObject(obj); |
| 10413 | if (uniobj == NULL) { |
| 10414 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10415 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10416 | return 0; |
| 10417 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10419 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10420 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10421 | Py_DECREF(uniobj); |
| 10422 | return 0; |
| 10423 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10424 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10425 | Py_DECREF(uniobj); |
| 10426 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10427 | } |
| 10428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10429 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10430 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10431 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10432 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10433 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10434 | |
| 10435 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10436 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10437 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10438 | Py_ssize_t marg, left; |
| 10439 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 | Py_UCS4 fillchar = ' '; |
| 10441 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10442 | 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] | 10443 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10444 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10445 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10446 | return NULL; |
| 10447 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10448 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10449 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10451 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10452 | left = marg / 2 + (marg & width & 1); |
| 10453 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10454 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10455 | } |
| 10456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10458 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10459 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10460 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10461 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10462 | int kind1, kind2; |
| 10463 | void *data1, *data2; |
| 10464 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10465 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10466 | kind1 = PyUnicode_KIND(str1); |
| 10467 | kind2 = PyUnicode_KIND(str2); |
| 10468 | data1 = PyUnicode_DATA(str1); |
| 10469 | data2 = PyUnicode_DATA(str2); |
| 10470 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10471 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10472 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10474 | Py_UCS4 c1, c2; |
| 10475 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10476 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10477 | |
| 10478 | if (c1 != c2) |
| 10479 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10480 | } |
| 10481 | |
| 10482 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10483 | } |
| 10484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10485 | int |
| 10486 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10487 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10489 | if (PyUnicode_READY(left) == -1 || |
| 10490 | PyUnicode_READY(right) == -1) |
| 10491 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10492 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10493 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10494 | PyErr_Format(PyExc_TypeError, |
| 10495 | "Can't compare %.100s and %.100s", |
| 10496 | left->ob_type->tp_name, |
| 10497 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | return -1; |
| 10499 | } |
| 10500 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10501 | int |
| 10502 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10504 | Py_ssize_t i; |
| 10505 | int kind; |
| 10506 | void *data; |
| 10507 | Py_UCS4 chr; |
| 10508 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10509 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | if (PyUnicode_READY(uni) == -1) |
| 10511 | return -1; |
| 10512 | kind = PyUnicode_KIND(uni); |
| 10513 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10514 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10516 | if (chr != str[i]) |
| 10517 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10518 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10519 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10521 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10522 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10523 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10524 | return 0; |
| 10525 | } |
| 10526 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10528 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10529 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10530 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10531 | PyObject * |
| 10532 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10533 | { |
| 10534 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10535 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10536 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10537 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10538 | if (PyUnicode_READY(left) == -1 || |
| 10539 | PyUnicode_READY(right) == -1) |
| 10540 | return NULL; |
| 10541 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10542 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10543 | if (op == Py_EQ) { |
| 10544 | Py_INCREF(Py_False); |
| 10545 | return Py_False; |
| 10546 | } |
| 10547 | if (op == Py_NE) { |
| 10548 | Py_INCREF(Py_True); |
| 10549 | return Py_True; |
| 10550 | } |
| 10551 | } |
| 10552 | if (left == right) |
| 10553 | result = 0; |
| 10554 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10555 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10556 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10557 | /* Convert the return value to a Boolean */ |
| 10558 | switch (op) { |
| 10559 | case Py_EQ: |
| 10560 | v = TEST_COND(result == 0); |
| 10561 | break; |
| 10562 | case Py_NE: |
| 10563 | v = TEST_COND(result != 0); |
| 10564 | break; |
| 10565 | case Py_LE: |
| 10566 | v = TEST_COND(result <= 0); |
| 10567 | break; |
| 10568 | case Py_GE: |
| 10569 | v = TEST_COND(result >= 0); |
| 10570 | break; |
| 10571 | case Py_LT: |
| 10572 | v = TEST_COND(result == -1); |
| 10573 | break; |
| 10574 | case Py_GT: |
| 10575 | v = TEST_COND(result == 1); |
| 10576 | break; |
| 10577 | default: |
| 10578 | PyErr_BadArgument(); |
| 10579 | return NULL; |
| 10580 | } |
| 10581 | Py_INCREF(v); |
| 10582 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10583 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10584 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10585 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10586 | } |
| 10587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10588 | int |
| 10589 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10590 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10591 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10592 | int kind1, kind2, kind; |
| 10593 | void *buf1, *buf2; |
| 10594 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10595 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10596 | |
| 10597 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10598 | sub = PyUnicode_FromObject(element); |
| 10599 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10600 | PyErr_Format(PyExc_TypeError, |
| 10601 | "'in <string>' requires string as left operand, not %s", |
| 10602 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10603 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10604 | } |
| 10605 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10606 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10607 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10608 | Py_DECREF(sub); |
| 10609 | return -1; |
| 10610 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10611 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10612 | Py_DECREF(sub); |
| 10613 | Py_DECREF(str); |
| 10614 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | kind1 = PyUnicode_KIND(str); |
| 10617 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10618 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10619 | buf1 = PyUnicode_DATA(str); |
| 10620 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10621 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10622 | if (kind2 > kind) { |
| 10623 | Py_DECREF(sub); |
| 10624 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10625 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10626 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10627 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | if (!buf2) { |
| 10630 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10631 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | return -1; |
| 10633 | } |
| 10634 | len1 = PyUnicode_GET_LENGTH(str); |
| 10635 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10636 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10637 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | case PyUnicode_1BYTE_KIND: |
| 10639 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10640 | break; |
| 10641 | case PyUnicode_2BYTE_KIND: |
| 10642 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10643 | break; |
| 10644 | case PyUnicode_4BYTE_KIND: |
| 10645 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10646 | break; |
| 10647 | default: |
| 10648 | result = -1; |
| 10649 | assert(0); |
| 10650 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10651 | |
| 10652 | Py_DECREF(str); |
| 10653 | Py_DECREF(sub); |
| 10654 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | if (kind2 != kind) |
| 10656 | PyMem_Free(buf2); |
| 10657 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10658 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10659 | } |
| 10660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10661 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10662 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10663 | PyObject * |
| 10664 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10665 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10666 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10667 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10668 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10669 | |
| 10670 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10672 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10673 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10674 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10675 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10676 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10677 | |
| 10678 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10679 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10680 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10683 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10684 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10685 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | } |
| 10687 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10688 | u_len = PyUnicode_GET_LENGTH(u); |
| 10689 | v_len = PyUnicode_GET_LENGTH(v); |
| 10690 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10691 | PyErr_SetString(PyExc_OverflowError, |
| 10692 | "strings are too large to concat"); |
| 10693 | goto onError; |
| 10694 | } |
| 10695 | new_len = u_len + v_len; |
| 10696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10697 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10698 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10699 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10702 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10703 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10704 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10705 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10706 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10707 | Py_DECREF(u); |
| 10708 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10709 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10710 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10712 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | Py_XDECREF(u); |
| 10714 | Py_XDECREF(v); |
| 10715 | return NULL; |
| 10716 | } |
| 10717 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10718 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10719 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10720 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10721 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10722 | Py_UCS4 maxchar, maxchar2; |
| 10723 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10724 | |
| 10725 | if (p_left == NULL) { |
| 10726 | if (!PyErr_Occurred()) |
| 10727 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10728 | return; |
| 10729 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10730 | left = *p_left; |
Serhiy Storchaka | 6c83e73 | 2013-01-04 12:39:34 +0200 | [diff] [blame] | 10731 | if (right == NULL || left == NULL || !PyUnicode_Check(left)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10732 | if (!PyErr_Occurred()) |
| 10733 | PyErr_BadInternalCall(); |
| 10734 | goto error; |
| 10735 | } |
| 10736 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10737 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10738 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10739 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10740 | goto error; |
| 10741 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10742 | /* Shortcuts */ |
| 10743 | if (left == unicode_empty) { |
| 10744 | Py_DECREF(left); |
| 10745 | Py_INCREF(right); |
| 10746 | *p_left = right; |
| 10747 | return; |
| 10748 | } |
| 10749 | if (right == unicode_empty) |
| 10750 | return; |
| 10751 | |
| 10752 | left_len = PyUnicode_GET_LENGTH(left); |
| 10753 | right_len = PyUnicode_GET_LENGTH(right); |
| 10754 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10755 | PyErr_SetString(PyExc_OverflowError, |
| 10756 | "strings are too large to concat"); |
| 10757 | goto error; |
| 10758 | } |
| 10759 | new_len = left_len + right_len; |
| 10760 | |
| 10761 | if (unicode_modifiable(left) |
| 10762 | && PyUnicode_CheckExact(right) |
| 10763 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10764 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10765 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10766 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10767 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10768 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10769 | { |
| 10770 | /* append inplace */ |
| 10771 | if (unicode_resize(p_left, new_len) != 0) { |
| 10772 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10773 | * deallocated so it cannot be put back into |
| 10774 | * 'variable'. The MemoryError is raised when there |
| 10775 | * is no value in 'variable', which might (very |
| 10776 | * remotely) be a cause of incompatibilities. |
| 10777 | */ |
| 10778 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10779 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10780 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10781 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10782 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10783 | else { |
| 10784 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10785 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10786 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10787 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10788 | /* Concat the two Unicode strings */ |
| 10789 | res = PyUnicode_New(new_len, maxchar); |
| 10790 | if (res == NULL) |
| 10791 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10792 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10793 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10794 | Py_DECREF(left); |
| 10795 | *p_left = res; |
| 10796 | } |
| 10797 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10798 | return; |
| 10799 | |
| 10800 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10801 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10802 | } |
| 10803 | |
| 10804 | void |
| 10805 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10806 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10807 | PyUnicode_Append(pleft, right); |
| 10808 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10809 | } |
| 10810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10811 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10812 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10813 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10814 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10815 | 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] | 10816 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10817 | |
| 10818 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10819 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10820 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10821 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10822 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10823 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10824 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10825 | int kind1, kind2, kind; |
| 10826 | void *buf1, *buf2; |
| 10827 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10828 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10829 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10830 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10831 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10833 | kind1 = PyUnicode_KIND(self); |
| 10834 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10835 | if (kind2 > kind1) |
| 10836 | return PyLong_FromLong(0); |
| 10837 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | buf1 = PyUnicode_DATA(self); |
| 10839 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10840 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10841 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10842 | if (!buf2) { |
| 10843 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10844 | return NULL; |
| 10845 | } |
| 10846 | len1 = PyUnicode_GET_LENGTH(self); |
| 10847 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10848 | |
| 10849 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10850 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10851 | case PyUnicode_1BYTE_KIND: |
| 10852 | iresult = ucs1lib_count( |
| 10853 | ((Py_UCS1*)buf1) + start, end - start, |
| 10854 | buf2, len2, PY_SSIZE_T_MAX |
| 10855 | ); |
| 10856 | break; |
| 10857 | case PyUnicode_2BYTE_KIND: |
| 10858 | iresult = ucs2lib_count( |
| 10859 | ((Py_UCS2*)buf1) + start, end - start, |
| 10860 | buf2, len2, PY_SSIZE_T_MAX |
| 10861 | ); |
| 10862 | break; |
| 10863 | case PyUnicode_4BYTE_KIND: |
| 10864 | iresult = ucs4lib_count( |
| 10865 | ((Py_UCS4*)buf1) + start, end - start, |
| 10866 | buf2, len2, PY_SSIZE_T_MAX |
| 10867 | ); |
| 10868 | break; |
| 10869 | default: |
| 10870 | assert(0); iresult = 0; |
| 10871 | } |
| 10872 | |
| 10873 | result = PyLong_FromSsize_t(iresult); |
| 10874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10875 | if (kind2 != kind) |
| 10876 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | |
| 10878 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | return result; |
| 10881 | } |
| 10882 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10883 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10884 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10885 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10886 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10887 | 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] | 10888 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10889 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10890 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10891 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10892 | |
| 10893 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10894 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10895 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10896 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | char *encoding = NULL; |
| 10898 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10899 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10900 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10901 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10902 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10903 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10904 | } |
| 10905 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10906 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10907 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 | \n\ |
| 10909 | 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] | 10910 | 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] | 10911 | |
| 10912 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10913 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10914 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10915 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10916 | Py_UCS4 ch; |
| 10917 | PyObject *u; |
| 10918 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10919 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10920 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10921 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 | |
| 10923 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10924 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10926 | if (PyUnicode_READY(self) == -1) |
| 10927 | return NULL; |
| 10928 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10929 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10930 | src_len = PyUnicode_GET_LENGTH(self); |
| 10931 | i = j = line_pos = 0; |
| 10932 | kind = PyUnicode_KIND(self); |
| 10933 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10934 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10935 | for (; i < src_len; i++) { |
| 10936 | ch = PyUnicode_READ(kind, src_data, i); |
| 10937 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10938 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10939 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10940 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10941 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10942 | goto overflow; |
| 10943 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10945 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10948 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10949 | goto overflow; |
| 10950 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10951 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10952 | if (ch == '\n' || ch == '\r') |
| 10953 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10954 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10955 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10956 | if (!found) |
| 10957 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10959 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10960 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | if (!u) |
| 10962 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10963 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10965 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10967 | for (; i < src_len; i++) { |
| 10968 | ch = PyUnicode_READ(kind, src_data, i); |
| 10969 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10970 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10971 | incr = tabsize - (line_pos % tabsize); |
| 10972 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10973 | FILL(kind, dest_data, ' ', j, incr); |
| 10974 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10975 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10976 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10977 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10978 | line_pos++; |
| 10979 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10980 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10981 | if (ch == '\n' || ch == '\r') |
| 10982 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10984 | } |
| 10985 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10986 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10987 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10988 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10989 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10990 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10991 | } |
| 10992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10993 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10994 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | \n\ |
| 10996 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10997 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | arguments start and end are interpreted as in slice notation.\n\ |
| 10999 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11000 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | |
| 11002 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11003 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11005 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11006 | Py_ssize_t start; |
| 11007 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11008 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11010 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11011 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11014 | if (PyUnicode_READY(self) == -1) |
| 11015 | return NULL; |
| 11016 | if (PyUnicode_READY(substring) == -1) |
| 11017 | return NULL; |
| 11018 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11019 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | |
| 11021 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11022 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11023 | if (result == -2) |
| 11024 | return NULL; |
| 11025 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11026 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | } |
| 11028 | |
| 11029 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11030 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11032 | void *data; |
| 11033 | enum PyUnicode_Kind kind; |
| 11034 | Py_UCS4 ch; |
| 11035 | PyObject *res; |
| 11036 | |
| 11037 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11038 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11039 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11040 | } |
| 11041 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11042 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11043 | return NULL; |
| 11044 | } |
| 11045 | kind = PyUnicode_KIND(self); |
| 11046 | data = PyUnicode_DATA(self); |
| 11047 | ch = PyUnicode_READ(kind, data, index); |
| 11048 | if (ch < 256) |
| 11049 | return get_latin1_char(ch); |
| 11050 | |
| 11051 | res = PyUnicode_New(1, ch); |
| 11052 | if (res == NULL) |
| 11053 | return NULL; |
| 11054 | kind = PyUnicode_KIND(res); |
| 11055 | data = PyUnicode_DATA(res); |
| 11056 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11057 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11058 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11061 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11062 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11063 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11064 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11066 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11067 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11068 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11069 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11070 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11071 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11072 | if (_PyUnicode_HASH(self) != -1) |
| 11073 | return _PyUnicode_HASH(self); |
| 11074 | if (PyUnicode_READY(self) == -1) |
| 11075 | return -1; |
| 11076 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11077 | /* |
| 11078 | We make the hash of the empty string be 0, rather than using |
| 11079 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11080 | */ |
| 11081 | if (len == 0) { |
| 11082 | _PyUnicode_HASH(self) = 0; |
| 11083 | return 0; |
| 11084 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | |
| 11086 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11087 | #define HASH(P) \ |
| 11088 | x ^= (Py_uhash_t) *P << 7; \ |
| 11089 | while (--len >= 0) \ |
| 11090 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11091 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11092 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | switch (PyUnicode_KIND(self)) { |
| 11094 | case PyUnicode_1BYTE_KIND: { |
| 11095 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11096 | HASH(c); |
| 11097 | break; |
| 11098 | } |
| 11099 | case PyUnicode_2BYTE_KIND: { |
| 11100 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11101 | HASH(s); |
| 11102 | break; |
| 11103 | } |
| 11104 | default: { |
| 11105 | Py_UCS4 *l; |
| 11106 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11107 | "Impossible switch case in unicode_hash"); |
| 11108 | l = PyUnicode_4BYTE_DATA(self); |
| 11109 | HASH(l); |
| 11110 | break; |
| 11111 | } |
| 11112 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11113 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11114 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11115 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11116 | if (x == -1) |
| 11117 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11118 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11119 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11123 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11124 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11125 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11126 | 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] | 11127 | |
| 11128 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11130 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11131 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11132 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11133 | Py_ssize_t start; |
| 11134 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11136 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11137 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11139 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | if (PyUnicode_READY(self) == -1) |
| 11141 | return NULL; |
| 11142 | if (PyUnicode_READY(substring) == -1) |
| 11143 | return NULL; |
| 11144 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11145 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | |
| 11147 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11148 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11149 | if (result == -2) |
| 11150 | return NULL; |
| 11151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | if (result < 0) { |
| 11153 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11154 | return NULL; |
| 11155 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11156 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11157 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | } |
| 11159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11160 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11161 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11163 | 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] | 11164 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | |
| 11166 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11167 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11169 | Py_ssize_t i, length; |
| 11170 | int kind; |
| 11171 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | int cased; |
| 11173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | if (PyUnicode_READY(self) == -1) |
| 11175 | return NULL; |
| 11176 | length = PyUnicode_GET_LENGTH(self); |
| 11177 | kind = PyUnicode_KIND(self); |
| 11178 | data = PyUnicode_DATA(self); |
| 11179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11181 | if (length == 1) |
| 11182 | return PyBool_FromLong( |
| 11183 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11185 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11187 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11190 | for (i = 0; i < length; i++) { |
| 11191 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11192 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11193 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11194 | return PyBool_FromLong(0); |
| 11195 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11196 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11197 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11198 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | } |
| 11200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11201 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11204 | 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] | 11205 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | |
| 11207 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11208 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11210 | Py_ssize_t i, length; |
| 11211 | int kind; |
| 11212 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | int cased; |
| 11214 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11215 | if (PyUnicode_READY(self) == -1) |
| 11216 | return NULL; |
| 11217 | length = PyUnicode_GET_LENGTH(self); |
| 11218 | kind = PyUnicode_KIND(self); |
| 11219 | data = PyUnicode_DATA(self); |
| 11220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11222 | if (length == 1) |
| 11223 | return PyBool_FromLong( |
| 11224 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11226 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11228 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11231 | for (i = 0; i < length; i++) { |
| 11232 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11234 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11235 | return PyBool_FromLong(0); |
| 11236 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11237 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11239 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | } |
| 11241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11242 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11243 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11245 | Return True if S is a titlecased string and there is at least one\n\ |
| 11246 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11247 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11248 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | |
| 11250 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11251 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11253 | Py_ssize_t i, length; |
| 11254 | int kind; |
| 11255 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11256 | int cased, previous_is_cased; |
| 11257 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11258 | if (PyUnicode_READY(self) == -1) |
| 11259 | return NULL; |
| 11260 | length = PyUnicode_GET_LENGTH(self); |
| 11261 | kind = PyUnicode_KIND(self); |
| 11262 | data = PyUnicode_DATA(self); |
| 11263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11265 | if (length == 1) { |
| 11266 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11267 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11268 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11269 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11271 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11272 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11273 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | cased = 0; |
| 11276 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11277 | for (i = 0; i < length; i++) { |
| 11278 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11279 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11280 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11281 | if (previous_is_cased) |
| 11282 | return PyBool_FromLong(0); |
| 11283 | previous_is_cased = 1; |
| 11284 | cased = 1; |
| 11285 | } |
| 11286 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11287 | if (!previous_is_cased) |
| 11288 | return PyBool_FromLong(0); |
| 11289 | previous_is_cased = 1; |
| 11290 | cased = 1; |
| 11291 | } |
| 11292 | else |
| 11293 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11295 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | } |
| 11297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11301 | Return True if all characters in S are whitespace\n\ |
| 11302 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
| 11304 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11305 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11307 | Py_ssize_t i, length; |
| 11308 | int kind; |
| 11309 | void *data; |
| 11310 | |
| 11311 | if (PyUnicode_READY(self) == -1) |
| 11312 | return NULL; |
| 11313 | length = PyUnicode_GET_LENGTH(self); |
| 11314 | kind = PyUnicode_KIND(self); |
| 11315 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11318 | if (length == 1) |
| 11319 | return PyBool_FromLong( |
| 11320 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11322 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11323 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11324 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11325 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | for (i = 0; i < length; i++) { |
| 11327 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11328 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11329 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11330 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11331 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | } |
| 11333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11334 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11335 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11336 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11337 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11338 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11339 | |
| 11340 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11341 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11342 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11343 | Py_ssize_t i, length; |
| 11344 | int kind; |
| 11345 | void *data; |
| 11346 | |
| 11347 | if (PyUnicode_READY(self) == -1) |
| 11348 | return NULL; |
| 11349 | length = PyUnicode_GET_LENGTH(self); |
| 11350 | kind = PyUnicode_KIND(self); |
| 11351 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11352 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11353 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11354 | if (length == 1) |
| 11355 | return PyBool_FromLong( |
| 11356 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11357 | |
| 11358 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11359 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11360 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11361 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | for (i = 0; i < length; i++) { |
| 11363 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11364 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11365 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11366 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11367 | } |
| 11368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11369 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11370 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11371 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11372 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11373 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11374 | |
| 11375 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11376 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11377 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | int kind; |
| 11379 | void *data; |
| 11380 | Py_ssize_t len, i; |
| 11381 | |
| 11382 | if (PyUnicode_READY(self) == -1) |
| 11383 | return NULL; |
| 11384 | |
| 11385 | kind = PyUnicode_KIND(self); |
| 11386 | data = PyUnicode_DATA(self); |
| 11387 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11388 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11389 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | if (len == 1) { |
| 11391 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11392 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11393 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11394 | |
| 11395 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11397 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | for (i = 0; i < len; i++) { |
| 11400 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11401 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11402 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11403 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11404 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11405 | } |
| 11406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11407 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11408 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11410 | 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] | 11411 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | |
| 11413 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11414 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11416 | Py_ssize_t i, length; |
| 11417 | int kind; |
| 11418 | void *data; |
| 11419 | |
| 11420 | if (PyUnicode_READY(self) == -1) |
| 11421 | return NULL; |
| 11422 | length = PyUnicode_GET_LENGTH(self); |
| 11423 | kind = PyUnicode_KIND(self); |
| 11424 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11427 | if (length == 1) |
| 11428 | return PyBool_FromLong( |
| 11429 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11431 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11433 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11435 | for (i = 0; i < length; i++) { |
| 11436 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11437 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11439 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | } |
| 11441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11442 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11443 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11445 | Return True if all characters in S are digits\n\ |
| 11446 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | |
| 11448 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11449 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | Py_ssize_t i, length; |
| 11452 | int kind; |
| 11453 | void *data; |
| 11454 | |
| 11455 | if (PyUnicode_READY(self) == -1) |
| 11456 | return NULL; |
| 11457 | length = PyUnicode_GET_LENGTH(self); |
| 11458 | kind = PyUnicode_KIND(self); |
| 11459 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11462 | if (length == 1) { |
| 11463 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11464 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11465 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11467 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11468 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11469 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | for (i = 0; i < length; i++) { |
| 11472 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11473 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11474 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11475 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | } |
| 11477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11478 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11481 | 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] | 11482 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | |
| 11484 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11485 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11487 | Py_ssize_t i, length; |
| 11488 | int kind; |
| 11489 | void *data; |
| 11490 | |
| 11491 | if (PyUnicode_READY(self) == -1) |
| 11492 | return NULL; |
| 11493 | length = PyUnicode_GET_LENGTH(self); |
| 11494 | kind = PyUnicode_KIND(self); |
| 11495 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11498 | if (length == 1) |
| 11499 | return PyBool_FromLong( |
| 11500 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11502 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11503 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11504 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11505 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11506 | for (i = 0; i < length; i++) { |
| 11507 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11510 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } |
| 11512 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11513 | int |
| 11514 | PyUnicode_IsIdentifier(PyObject *self) |
| 11515 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 | int kind; |
| 11517 | void *data; |
| 11518 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11519 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | if (PyUnicode_READY(self) == -1) { |
| 11522 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | } |
| 11525 | |
| 11526 | /* Special case for empty strings */ |
| 11527 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11528 | return 0; |
| 11529 | kind = PyUnicode_KIND(self); |
| 11530 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11531 | |
| 11532 | /* PEP 3131 says that the first character must be in |
| 11533 | XID_Start and subsequent characters in XID_Continue, |
| 11534 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11535 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11536 | letters, digits, underscore). However, given the current |
| 11537 | definition of XID_Start and XID_Continue, it is sufficient |
| 11538 | to check just for these, except that _ must be allowed |
| 11539 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11541 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11542 | return 0; |
| 11543 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11544 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11545 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11546 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11547 | return 1; |
| 11548 | } |
| 11549 | |
| 11550 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11551 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11552 | \n\ |
| 11553 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11554 | to the language definition.\n\ |
| 11555 | \n\ |
| 11556 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11557 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11558 | |
| 11559 | static PyObject* |
| 11560 | unicode_isidentifier(PyObject *self) |
| 11561 | { |
| 11562 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11563 | } |
| 11564 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11565 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11566 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11567 | \n\ |
| 11568 | Return True if all characters in S are considered\n\ |
| 11569 | printable in repr() or S is empty, False otherwise."); |
| 11570 | |
| 11571 | static PyObject* |
| 11572 | unicode_isprintable(PyObject *self) |
| 11573 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | Py_ssize_t i, length; |
| 11575 | int kind; |
| 11576 | void *data; |
| 11577 | |
| 11578 | if (PyUnicode_READY(self) == -1) |
| 11579 | return NULL; |
| 11580 | length = PyUnicode_GET_LENGTH(self); |
| 11581 | kind = PyUnicode_KIND(self); |
| 11582 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11583 | |
| 11584 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11585 | if (length == 1) |
| 11586 | return PyBool_FromLong( |
| 11587 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | for (i = 0; i < length; i++) { |
| 11590 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11591 | Py_RETURN_FALSE; |
| 11592 | } |
| 11593 | } |
| 11594 | Py_RETURN_TRUE; |
| 11595 | } |
| 11596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11597 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11598 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | \n\ |
| 11600 | 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] | 11601 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11602 | |
| 11603 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11604 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11605 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11606 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | } |
| 11608 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11609 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11610 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11612 | if (PyUnicode_READY(self) == -1) |
| 11613 | return -1; |
| 11614 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | } |
| 11616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11617 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11618 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11620 | 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] | 11621 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11622 | |
| 11623 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11624 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11626 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11627 | Py_UCS4 fillchar = ' '; |
| 11628 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11629 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11630 | return NULL; |
| 11631 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11632 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11633 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11635 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11636 | return unicode_result_unchanged(self); |
| 11637 | |
| 11638 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | } |
| 11640 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11641 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11642 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11643 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11644 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | |
| 11646 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11647 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11648 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11649 | if (PyUnicode_READY(self) == -1) |
| 11650 | return NULL; |
| 11651 | if (PyUnicode_IS_ASCII(self)) |
| 11652 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11653 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11654 | } |
| 11655 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11656 | #define LEFTSTRIP 0 |
| 11657 | #define RIGHTSTRIP 1 |
| 11658 | #define BOTHSTRIP 2 |
| 11659 | |
| 11660 | /* Arrays indexed by above */ |
| 11661 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11662 | |
| 11663 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11664 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11665 | /* externally visible for str.strip(unicode) */ |
| 11666 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11667 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11668 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 | void *data; |
| 11670 | int kind; |
| 11671 | Py_ssize_t i, j, len; |
| 11672 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11673 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11674 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11675 | return NULL; |
| 11676 | |
| 11677 | kind = PyUnicode_KIND(self); |
| 11678 | data = PyUnicode_DATA(self); |
| 11679 | len = PyUnicode_GET_LENGTH(self); |
| 11680 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11681 | PyUnicode_DATA(sepobj), |
| 11682 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11683 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11684 | i = 0; |
| 11685 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | while (i < len && |
| 11687 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11688 | i++; |
| 11689 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11690 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11691 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11692 | j = len; |
| 11693 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11694 | do { |
| 11695 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | } while (j >= i && |
| 11697 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11699 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11700 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11701 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11702 | } |
| 11703 | |
| 11704 | PyObject* |
| 11705 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11706 | { |
| 11707 | unsigned char *data; |
| 11708 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11709 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11710 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11711 | if (PyUnicode_READY(self) == -1) |
| 11712 | return NULL; |
| 11713 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11714 | length = PyUnicode_GET_LENGTH(self); |
| 11715 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11716 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11717 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11718 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11719 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11720 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11721 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11722 | return NULL; |
| 11723 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11724 | if (start >= length || end < start) |
| 11725 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11726 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11727 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11728 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11729 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11730 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11731 | } |
| 11732 | else { |
| 11733 | kind = PyUnicode_KIND(self); |
| 11734 | data = PyUnicode_1BYTE_DATA(self); |
| 11735 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11736 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11737 | length); |
| 11738 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11739 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | |
| 11741 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11742 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11744 | int kind; |
| 11745 | void *data; |
| 11746 | Py_ssize_t len, i, j; |
| 11747 | |
| 11748 | if (PyUnicode_READY(self) == -1) |
| 11749 | return NULL; |
| 11750 | |
| 11751 | kind = PyUnicode_KIND(self); |
| 11752 | data = PyUnicode_DATA(self); |
| 11753 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11754 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11755 | i = 0; |
| 11756 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11757 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11758 | i++; |
| 11759 | } |
| 11760 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11761 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11762 | j = len; |
| 11763 | if (striptype != LEFTSTRIP) { |
| 11764 | do { |
| 11765 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11767 | j++; |
| 11768 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11769 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11770 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | } |
| 11772 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11773 | |
| 11774 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11775 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11776 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11777 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11778 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11779 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11780 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11781 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11782 | if (sep != NULL && sep != Py_None) { |
| 11783 | if (PyUnicode_Check(sep)) |
| 11784 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11785 | else { |
| 11786 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11787 | "%s arg must be None or str", |
| 11788 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11789 | return NULL; |
| 11790 | } |
| 11791 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11792 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11793 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11794 | } |
| 11795 | |
| 11796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11797 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11798 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11799 | \n\ |
| 11800 | Return a copy of the string S with leading and trailing\n\ |
| 11801 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11802 | 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] | 11803 | |
| 11804 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11805 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11806 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11807 | if (PyTuple_GET_SIZE(args) == 0) |
| 11808 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11809 | else |
| 11810 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11811 | } |
| 11812 | |
| 11813 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11814 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11815 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11816 | \n\ |
| 11817 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11818 | 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] | 11819 | |
| 11820 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11821 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11822 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11823 | if (PyTuple_GET_SIZE(args) == 0) |
| 11824 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11825 | else |
| 11826 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11827 | } |
| 11828 | |
| 11829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11830 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11831 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11832 | \n\ |
| 11833 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11834 | 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] | 11835 | |
| 11836 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11837 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11838 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11839 | if (PyTuple_GET_SIZE(args) == 0) |
| 11840 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11841 | else |
| 11842 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11843 | } |
| 11844 | |
| 11845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11846 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11847 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11849 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11850 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11852 | if (len < 1) |
| 11853 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11855 | /* no repeat, return original string */ |
| 11856 | if (len == 1) |
| 11857 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11858 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11859 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11860 | return NULL; |
| 11861 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11862 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11863 | PyErr_SetString(PyExc_OverflowError, |
| 11864 | "repeated string is too long"); |
| 11865 | return NULL; |
| 11866 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11867 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11868 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11869 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | if (!u) |
| 11871 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11872 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11874 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11875 | const int kind = PyUnicode_KIND(str); |
| 11876 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11877 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11878 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11879 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11880 | } |
| 11881 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11882 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11883 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11884 | ucs2[n] = fill_char; |
| 11885 | } else { |
| 11886 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11887 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11888 | for (n = 0; n < len; ++n) |
| 11889 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11890 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11891 | } |
| 11892 | else { |
| 11893 | /* number of characters copied this far */ |
| 11894 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11895 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11896 | char *to = (char *) PyUnicode_DATA(u); |
| 11897 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11898 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11899 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | n = (done <= nchars-done) ? done : nchars-done; |
| 11901 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11902 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11903 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11906 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11907 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | } |
| 11909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11910 | PyObject * |
| 11911 | PyUnicode_Replace(PyObject *obj, |
| 11912 | PyObject *subobj, |
| 11913 | PyObject *replobj, |
| 11914 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11915 | { |
| 11916 | PyObject *self; |
| 11917 | PyObject *str1; |
| 11918 | PyObject *str2; |
| 11919 | PyObject *result; |
| 11920 | |
| 11921 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11922 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11923 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11925 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11926 | Py_DECREF(self); |
| 11927 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11928 | } |
| 11929 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11930 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11931 | Py_DECREF(self); |
| 11932 | Py_DECREF(str1); |
| 11933 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11935 | if (PyUnicode_READY(self) == -1 || |
| 11936 | PyUnicode_READY(str1) == -1 || |
| 11937 | PyUnicode_READY(str2) == -1) |
| 11938 | result = NULL; |
| 11939 | else |
| 11940 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | Py_DECREF(self); |
| 11942 | Py_DECREF(str1); |
| 11943 | Py_DECREF(str2); |
| 11944 | return result; |
| 11945 | } |
| 11946 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11947 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11948 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | \n\ |
| 11950 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11951 | old replaced by new. If the optional argument count is\n\ |
| 11952 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | |
| 11954 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11956 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | PyObject *str1; |
| 11958 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11959 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | PyObject *result; |
| 11961 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11962 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11964 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11966 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11967 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | return NULL; |
| 11969 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11970 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | Py_DECREF(str1); |
| 11972 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11973 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11974 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11975 | result = NULL; |
| 11976 | else |
| 11977 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | |
| 11979 | Py_DECREF(str1); |
| 11980 | Py_DECREF(str2); |
| 11981 | return result; |
| 11982 | } |
| 11983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11984 | static PyObject * |
| 11985 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11986 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11987 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11988 | Py_ssize_t isize; |
| 11989 | Py_ssize_t osize, squote, dquote, i, o; |
| 11990 | Py_UCS4 max, quote; |
| 11991 | int ikind, okind; |
| 11992 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11993 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11995 | return NULL; |
| 11996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11997 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11998 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11999 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12000 | /* Compute length of output, quote characters, and |
| 12001 | maximum character */ |
| 12002 | osize = 2; /* quotes */ |
| 12003 | max = 127; |
| 12004 | squote = dquote = 0; |
| 12005 | ikind = PyUnicode_KIND(unicode); |
| 12006 | for (i = 0; i < isize; i++) { |
| 12007 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12008 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12009 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12010 | case '\'': squote++; break; |
| 12011 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12012 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12013 | incr = 2; |
| 12014 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12015 | default: |
| 12016 | /* Fast-path ASCII */ |
| 12017 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12018 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12020 | ; |
| 12021 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12023 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12024 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12026 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12028 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12029 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12030 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12031 | PyErr_SetString(PyExc_OverflowError, |
| 12032 | "string is too long to generate repr"); |
| 12033 | return NULL; |
| 12034 | } |
| 12035 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12036 | } |
| 12037 | |
| 12038 | quote = '\''; |
| 12039 | if (squote) { |
| 12040 | if (dquote) |
| 12041 | /* Both squote and dquote present. Use squote, |
| 12042 | and escape them */ |
| 12043 | osize += squote; |
| 12044 | else |
| 12045 | quote = '"'; |
| 12046 | } |
| 12047 | |
| 12048 | repr = PyUnicode_New(osize, max); |
| 12049 | if (repr == NULL) |
| 12050 | return NULL; |
| 12051 | okind = PyUnicode_KIND(repr); |
| 12052 | odata = PyUnicode_DATA(repr); |
| 12053 | |
| 12054 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12055 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12056 | |
| 12057 | for (i = 0, o = 1; i < isize; i++) { |
| 12058 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12059 | |
| 12060 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12061 | if ((ch == quote) || (ch == '\\')) { |
| 12062 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12063 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12064 | continue; |
| 12065 | } |
| 12066 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12067 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12068 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12070 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12071 | } |
| 12072 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12074 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12075 | } |
| 12076 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12077 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12078 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12079 | } |
| 12080 | |
| 12081 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12082 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12083 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12084 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12085 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12086 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12087 | } |
| 12088 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12089 | /* Copy ASCII characters as-is */ |
| 12090 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12092 | } |
| 12093 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12094 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12095 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12096 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12097 | (categories Z* and C* except ASCII space) |
| 12098 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12099 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12100 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12101 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12102 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12103 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12104 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12106 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12107 | /* Map 16-bit characters to '\uxxxx' */ |
| 12108 | else if (ch <= 0xffff) { |
| 12109 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12110 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12111 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12112 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12113 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12114 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12115 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12116 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12117 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12118 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12119 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12120 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12121 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12122 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12123 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12124 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12125 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12126 | } |
| 12127 | } |
| 12128 | /* Copy characters as-is */ |
| 12129 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12130 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12131 | } |
| 12132 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12133 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12134 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12135 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12136 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12137 | } |
| 12138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12139 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12140 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12141 | \n\ |
| 12142 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12143 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | arguments start and end are interpreted as in slice notation.\n\ |
| 12145 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12146 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 | |
| 12148 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12149 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12150 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12151 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12152 | Py_ssize_t start; |
| 12153 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12154 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12155 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12156 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12157 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12158 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | if (PyUnicode_READY(self) == -1) |
| 12161 | return NULL; |
| 12162 | if (PyUnicode_READY(substring) == -1) |
| 12163 | return NULL; |
| 12164 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12165 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | |
| 12167 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12168 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12169 | if (result == -2) |
| 12170 | return NULL; |
| 12171 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12172 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | } |
| 12174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12175 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12176 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12178 | 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] | 12179 | |
| 12180 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12182 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12183 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12184 | Py_ssize_t start; |
| 12185 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12186 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12187 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12188 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12189 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12190 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12192 | if (PyUnicode_READY(self) == -1) |
| 12193 | return NULL; |
| 12194 | if (PyUnicode_READY(substring) == -1) |
| 12195 | return NULL; |
| 12196 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12197 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12198 | |
| 12199 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12201 | if (result == -2) |
| 12202 | return NULL; |
| 12203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12204 | if (result < 0) { |
| 12205 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12206 | return NULL; |
| 12207 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12209 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | } |
| 12211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12212 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12213 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12214 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12215 | 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] | 12216 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12217 | |
| 12218 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12219 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12221 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12222 | Py_UCS4 fillchar = ' '; |
| 12223 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12224 | 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] | 12225 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12226 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12227 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | return NULL; |
| 12229 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12230 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12231 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12233 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12234 | } |
| 12235 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12236 | PyObject * |
| 12237 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12238 | { |
| 12239 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12241 | s = PyUnicode_FromObject(s); |
| 12242 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12243 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12244 | if (sep != NULL) { |
| 12245 | sep = PyUnicode_FromObject(sep); |
| 12246 | if (sep == NULL) { |
| 12247 | Py_DECREF(s); |
| 12248 | return NULL; |
| 12249 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12250 | } |
| 12251 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12252 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | |
| 12254 | Py_DECREF(s); |
| 12255 | Py_XDECREF(sep); |
| 12256 | return result; |
| 12257 | } |
| 12258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12259 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12260 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | \n\ |
| 12262 | Return a list of the words in S, using sep as the\n\ |
| 12263 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12264 | 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] | 12265 | whitespace string is a separator and empty strings are\n\ |
| 12266 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12267 | |
| 12268 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12269 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12270 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12271 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12273 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12275 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12276 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | return NULL; |
| 12278 | |
| 12279 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12282 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12284 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | } |
| 12286 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12287 | PyObject * |
| 12288 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12289 | { |
| 12290 | PyObject* str_obj; |
| 12291 | PyObject* sep_obj; |
| 12292 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12293 | int kind1, kind2, kind; |
| 12294 | void *buf1 = NULL, *buf2 = NULL; |
| 12295 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12296 | |
| 12297 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12298 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12299 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12300 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12301 | if (!sep_obj) { |
| 12302 | Py_DECREF(str_obj); |
| 12303 | return NULL; |
| 12304 | } |
| 12305 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12306 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12307 | Py_DECREF(str_obj); |
| 12308 | return NULL; |
| 12309 | } |
| 12310 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12311 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12312 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12313 | kind = Py_MAX(kind1, kind2); |
| 12314 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12316 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12317 | if (!buf1) |
| 12318 | goto onError; |
| 12319 | buf2 = PyUnicode_DATA(sep_obj); |
| 12320 | if (kind2 != kind) |
| 12321 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12322 | if (!buf2) |
| 12323 | goto onError; |
| 12324 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12325 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12326 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12327 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12329 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12330 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12331 | else |
| 12332 | 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] | 12333 | break; |
| 12334 | case PyUnicode_2BYTE_KIND: |
| 12335 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12336 | break; |
| 12337 | case PyUnicode_4BYTE_KIND: |
| 12338 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12339 | break; |
| 12340 | default: |
| 12341 | assert(0); |
| 12342 | out = 0; |
| 12343 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12344 | |
| 12345 | Py_DECREF(sep_obj); |
| 12346 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12347 | if (kind1 != kind) |
| 12348 | PyMem_Free(buf1); |
| 12349 | if (kind2 != kind) |
| 12350 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12351 | |
| 12352 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | onError: |
| 12354 | Py_DECREF(sep_obj); |
| 12355 | Py_DECREF(str_obj); |
| 12356 | if (kind1 != kind && buf1) |
| 12357 | PyMem_Free(buf1); |
| 12358 | if (kind2 != kind && buf2) |
| 12359 | PyMem_Free(buf2); |
| 12360 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12361 | } |
| 12362 | |
| 12363 | |
| 12364 | PyObject * |
| 12365 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12366 | { |
| 12367 | PyObject* str_obj; |
| 12368 | PyObject* sep_obj; |
| 12369 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12370 | int kind1, kind2, kind; |
| 12371 | void *buf1 = NULL, *buf2 = NULL; |
| 12372 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12373 | |
| 12374 | str_obj = PyUnicode_FromObject(str_in); |
| 12375 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12376 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12377 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12378 | if (!sep_obj) { |
| 12379 | Py_DECREF(str_obj); |
| 12380 | return NULL; |
| 12381 | } |
| 12382 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12383 | kind1 = PyUnicode_KIND(str_in); |
| 12384 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12385 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | buf1 = PyUnicode_DATA(str_in); |
| 12387 | if (kind1 != kind) |
| 12388 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12389 | if (!buf1) |
| 12390 | goto onError; |
| 12391 | buf2 = PyUnicode_DATA(sep_obj); |
| 12392 | if (kind2 != kind) |
| 12393 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12394 | if (!buf2) |
| 12395 | goto onError; |
| 12396 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12397 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12398 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12399 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12401 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12402 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12403 | else |
| 12404 | 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] | 12405 | break; |
| 12406 | case PyUnicode_2BYTE_KIND: |
| 12407 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12408 | break; |
| 12409 | case PyUnicode_4BYTE_KIND: |
| 12410 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12411 | break; |
| 12412 | default: |
| 12413 | assert(0); |
| 12414 | out = 0; |
| 12415 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12416 | |
| 12417 | Py_DECREF(sep_obj); |
| 12418 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12419 | if (kind1 != kind) |
| 12420 | PyMem_Free(buf1); |
| 12421 | if (kind2 != kind) |
| 12422 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12423 | |
| 12424 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 | onError: |
| 12426 | Py_DECREF(sep_obj); |
| 12427 | Py_DECREF(str_obj); |
| 12428 | if (kind1 != kind && buf1) |
| 12429 | PyMem_Free(buf1); |
| 12430 | if (kind2 != kind && buf2) |
| 12431 | PyMem_Free(buf2); |
| 12432 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12433 | } |
| 12434 | |
| 12435 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12436 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12437 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12438 | 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] | 12439 | 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] | 12440 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12441 | |
| 12442 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12443 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12444 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12445 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12446 | } |
| 12447 | |
| 12448 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12449 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12450 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12451 | 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] | 12452 | 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] | 12453 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12454 | |
| 12455 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12456 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12457 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12458 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12459 | } |
| 12460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12461 | PyObject * |
| 12462 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12463 | { |
| 12464 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12465 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12466 | s = PyUnicode_FromObject(s); |
| 12467 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12468 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12469 | if (sep != NULL) { |
| 12470 | sep = PyUnicode_FromObject(sep); |
| 12471 | if (sep == NULL) { |
| 12472 | Py_DECREF(s); |
| 12473 | return NULL; |
| 12474 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12475 | } |
| 12476 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12477 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12478 | |
| 12479 | Py_DECREF(s); |
| 12480 | Py_XDECREF(sep); |
| 12481 | return result; |
| 12482 | } |
| 12483 | |
| 12484 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12485 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12486 | \n\ |
| 12487 | Return a list of the words in S, using sep as the\n\ |
| 12488 | delimiter string, starting at the end of the string and\n\ |
| 12489 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12490 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12491 | is a separator."); |
| 12492 | |
| 12493 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12494 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12495 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12496 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12497 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12498 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12499 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12500 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12501 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12502 | return NULL; |
| 12503 | |
| 12504 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12505 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12506 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12507 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12508 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12509 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12510 | } |
| 12511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12512 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12513 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12514 | \n\ |
| 12515 | 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] | 12516 | 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] | 12517 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | |
| 12519 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12520 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
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 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12523 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12524 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12525 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12526 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12527 | return NULL; |
| 12528 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12529 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12530 | } |
| 12531 | |
| 12532 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12533 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12534 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12535 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | } |
| 12537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12538 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12539 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12540 | \n\ |
| 12541 | 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] | 12542 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
| 12544 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12545 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12547 | if (PyUnicode_READY(self) == -1) |
| 12548 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12549 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12599 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12600 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12601 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12602 | if (!value) { |
| 12603 | Py_DECREF(key); |
| 12604 | goto err; |
| 12605 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12606 | res = PyDict_SetItem(new, key, value); |
| 12607 | Py_DECREF(key); |
| 12608 | Py_DECREF(value); |
| 12609 | if (res < 0) |
| 12610 | goto err; |
| 12611 | } |
| 12612 | /* create entries for deleting chars in z */ |
| 12613 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12614 | z_kind = PyUnicode_KIND(z); |
| 12615 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12616 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12617 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12618 | if (!key) |
| 12619 | goto err; |
| 12620 | res = PyDict_SetItem(new, key, Py_None); |
| 12621 | Py_DECREF(key); |
| 12622 | if (res < 0) |
| 12623 | goto err; |
| 12624 | } |
| 12625 | } |
| 12626 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12627 | int kind; |
| 12628 | void *data; |
| 12629 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12630 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12631 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12632 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12633 | "to maketrans it must be a dict"); |
| 12634 | goto err; |
| 12635 | } |
| 12636 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12637 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12638 | if (PyUnicode_Check(key)) { |
| 12639 | /* convert string keys to integer keys */ |
| 12640 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12641 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12642 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12643 | "table must be of length 1"); |
| 12644 | goto err; |
| 12645 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12646 | kind = PyUnicode_KIND(key); |
| 12647 | data = PyUnicode_DATA(key); |
| 12648 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12649 | if (!newkey) |
| 12650 | goto err; |
| 12651 | res = PyDict_SetItem(new, newkey, value); |
| 12652 | Py_DECREF(newkey); |
| 12653 | if (res < 0) |
| 12654 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12655 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12656 | /* just keep integer keys */ |
| 12657 | if (PyDict_SetItem(new, key, value) < 0) |
| 12658 | goto err; |
| 12659 | } else { |
| 12660 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12661 | "be strings or integers"); |
| 12662 | goto err; |
| 12663 | } |
| 12664 | } |
| 12665 | } |
| 12666 | return new; |
| 12667 | err: |
| 12668 | Py_DECREF(new); |
| 12669 | return NULL; |
| 12670 | } |
| 12671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12672 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12673 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12674 | \n\ |
| 12675 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12676 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12677 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12678 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12679 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12680 | |
| 12681 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12682 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12684 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | } |
| 12686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12687 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12688 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12690 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | |
| 12692 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12693 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12694 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12695 | if (PyUnicode_READY(self) == -1) |
| 12696 | return NULL; |
| 12697 | if (PyUnicode_IS_ASCII(self)) |
| 12698 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12699 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12700 | } |
| 12701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12702 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12703 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12704 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12705 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12706 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12707 | |
| 12708 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12709 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12710 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12711 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12712 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12713 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | int kind; |
| 12715 | void *data; |
| 12716 | Py_UCS4 chr; |
| 12717 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12718 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12719 | return NULL; |
| 12720 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12721 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12722 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12724 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12725 | return unicode_result_unchanged(self); |
| 12726 | |
| 12727 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | |
| 12729 | u = pad(self, fill, 0, '0'); |
| 12730 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12731 | if (u == NULL) |
| 12732 | return NULL; |
| 12733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12734 | kind = PyUnicode_KIND(u); |
| 12735 | data = PyUnicode_DATA(u); |
| 12736 | chr = PyUnicode_READ(kind, data, fill); |
| 12737 | |
| 12738 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12739 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12740 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12741 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12742 | } |
| 12743 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12744 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12745 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | |
| 12748 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12749 | static PyObject * |
| 12750 | unicode__decimal2ascii(PyObject *self) |
| 12751 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12753 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12754 | #endif |
| 12755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12756 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12757 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12758 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12759 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12760 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12761 | With optional end, stop comparing S at that position.\n\ |
| 12762 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12763 | |
| 12764 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12765 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12766 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12767 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12768 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12769 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12770 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12771 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12772 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12773 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12774 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12775 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12776 | if (PyTuple_Check(subobj)) { |
| 12777 | Py_ssize_t i; |
| 12778 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12779 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12780 | if (substring == NULL) |
| 12781 | return NULL; |
| 12782 | result = tailmatch(self, substring, start, end, -1); |
| 12783 | Py_DECREF(substring); |
| 12784 | if (result) { |
| 12785 | Py_RETURN_TRUE; |
| 12786 | } |
| 12787 | } |
| 12788 | /* nothing matched */ |
| 12789 | Py_RETURN_FALSE; |
| 12790 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12791 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12792 | if (substring == NULL) { |
| 12793 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12794 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12795 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12796 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12797 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12799 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12800 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12801 | } |
| 12802 | |
| 12803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12804 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12805 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12806 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12807 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12808 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12809 | With optional end, stop comparing S at that position.\n\ |
| 12810 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12811 | |
| 12812 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12813 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12814 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12815 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12816 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12817 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12818 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12819 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12820 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12822 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12823 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12824 | if (PyTuple_Check(subobj)) { |
| 12825 | Py_ssize_t i; |
| 12826 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12827 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12828 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12829 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12830 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12831 | result = tailmatch(self, substring, start, end, +1); |
| 12832 | Py_DECREF(substring); |
| 12833 | if (result) { |
| 12834 | Py_RETURN_TRUE; |
| 12835 | } |
| 12836 | } |
| 12837 | Py_RETURN_FALSE; |
| 12838 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12839 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12840 | if (substring == NULL) { |
| 12841 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12842 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12843 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12844 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12845 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12846 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12848 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12849 | } |
| 12850 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12851 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12852 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12853 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12854 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12855 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12856 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12857 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12858 | } |
| 12859 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12860 | void |
| 12861 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12862 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12863 | memset(writer, 0, sizeof(*writer)); |
| 12864 | #ifdef Py_DEBUG |
| 12865 | writer->kind = 5; /* invalid kind */ |
| 12866 | #endif |
| 12867 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12868 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12869 | } |
| 12870 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12871 | int |
| 12872 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12873 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12874 | { |
| 12875 | Py_ssize_t newlen; |
| 12876 | PyObject *newbuffer; |
| 12877 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12878 | assert(length > 0); |
| 12879 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12880 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12881 | PyErr_NoMemory(); |
| 12882 | return -1; |
| 12883 | } |
| 12884 | newlen = writer->pos + length; |
| 12885 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12886 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12887 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12888 | /* overallocate 25% to limit the number of resize */ |
| 12889 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12890 | newlen += newlen / 4; |
| 12891 | if (newlen < writer->min_length) |
| 12892 | newlen = writer->min_length; |
| 12893 | } |
| 12894 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12895 | if (writer->buffer == NULL) |
| 12896 | return -1; |
| 12897 | _PyUnicodeWriter_Update(writer); |
| 12898 | return 0; |
| 12899 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12900 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12901 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12902 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12903 | /* overallocate 25% to limit the number of resize */ |
| 12904 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12905 | newlen += newlen / 4; |
| 12906 | if (newlen < writer->min_length) |
| 12907 | newlen = writer->min_length; |
| 12908 | } |
| 12909 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12910 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12911 | /* resize + widen */ |
| 12912 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12913 | if (newbuffer == NULL) |
| 12914 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12915 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12916 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12917 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12918 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12919 | } |
| 12920 | else { |
| 12921 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12922 | if (newbuffer == NULL) |
| 12923 | return -1; |
| 12924 | } |
| 12925 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12926 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12927 | } |
| 12928 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12929 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12930 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12931 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12932 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12933 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12934 | writer->buffer, 0, writer->pos); |
| 12935 | Py_DECREF(writer->buffer); |
| 12936 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12937 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12938 | } |
| 12939 | return 0; |
| 12940 | } |
| 12941 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12942 | int |
| 12943 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12944 | { |
| 12945 | Py_UCS4 maxchar; |
| 12946 | Py_ssize_t len; |
| 12947 | |
| 12948 | if (PyUnicode_READY(str) == -1) |
| 12949 | return -1; |
| 12950 | len = PyUnicode_GET_LENGTH(str); |
| 12951 | if (len == 0) |
| 12952 | return 0; |
| 12953 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12954 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12955 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12956 | Py_INCREF(str); |
| 12957 | writer->buffer = str; |
| 12958 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12959 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12960 | writer->size = 0; |
| 12961 | writer->pos += len; |
| 12962 | return 0; |
| 12963 | } |
| 12964 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12965 | return -1; |
| 12966 | } |
| 12967 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12968 | str, 0, len); |
| 12969 | writer->pos += len; |
| 12970 | return 0; |
| 12971 | } |
| 12972 | |
| 12973 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12974 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12975 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12976 | if (writer->pos == 0) { |
| 12977 | Py_XDECREF(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12978 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12979 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12980 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12981 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12982 | return writer->buffer; |
| 12983 | } |
| 12984 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12985 | PyObject *newbuffer; |
| 12986 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12987 | if (newbuffer == NULL) { |
| 12988 | Py_DECREF(writer->buffer); |
| 12989 | return NULL; |
| 12990 | } |
| 12991 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12992 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12993 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 2cb16aa | 2013-03-06 19:28:37 +0100 | [diff] [blame] | 12994 | return unicode_result_ready(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12995 | } |
| 12996 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12997 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12998 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12999 | { |
| 13000 | Py_CLEAR(writer->buffer); |
| 13001 | } |
| 13002 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13003 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13004 | |
| 13005 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13006 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13007 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13008 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13009 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13010 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13011 | PyDoc_STRVAR(format_map__doc__, |
| 13012 | "S.format_map(mapping) -> str\n\ |
| 13013 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13014 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13015 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13016 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13017 | static PyObject * |
| 13018 | unicode__format__(PyObject* self, PyObject* args) |
| 13019 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13020 | PyObject *format_spec; |
| 13021 | _PyUnicodeWriter writer; |
| 13022 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13023 | |
| 13024 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13025 | return NULL; |
| 13026 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13027 | if (PyUnicode_READY(self) == -1) |
| 13028 | return NULL; |
| 13029 | _PyUnicodeWriter_Init(&writer, 0); |
| 13030 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13031 | self, format_spec, 0, |
| 13032 | PyUnicode_GET_LENGTH(format_spec)); |
| 13033 | if (ret == -1) { |
| 13034 | _PyUnicodeWriter_Dealloc(&writer); |
| 13035 | return NULL; |
| 13036 | } |
| 13037 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13038 | } |
| 13039 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13040 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13041 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13042 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13043 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13044 | |
| 13045 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13046 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13047 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13048 | Py_ssize_t size; |
| 13049 | |
| 13050 | /* If it's a compact object, account for base structure + |
| 13051 | character data. */ |
| 13052 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13053 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13054 | else if (PyUnicode_IS_COMPACT(v)) |
| 13055 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13056 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13057 | else { |
| 13058 | /* If it is a two-block object, account for base object, and |
| 13059 | for character block if present. */ |
| 13060 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13061 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13062 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13063 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13064 | } |
| 13065 | /* 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] | 13066 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13067 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13068 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13069 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13070 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13071 | |
| 13072 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13073 | } |
| 13074 | |
| 13075 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13076 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13077 | |
| 13078 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13079 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13080 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13081 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13082 | if (!copy) |
| 13083 | return NULL; |
| 13084 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13085 | } |
| 13086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13087 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13088 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13089 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13090 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13091 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13092 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13093 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13094 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13095 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13096 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13097 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13098 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13099 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13100 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13101 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13102 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13103 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13104 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13105 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13106 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13107 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13108 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13109 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13110 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13111 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13112 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13113 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13114 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13115 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13116 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13117 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13118 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13119 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13120 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13121 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13122 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13123 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13124 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13125 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13126 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13127 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13128 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13129 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13130 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13131 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13132 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13133 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13134 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13135 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13136 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13137 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13138 | #endif |
| 13139 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13140 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13141 | {NULL, NULL} |
| 13142 | }; |
| 13143 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13144 | static PyObject * |
| 13145 | unicode_mod(PyObject *v, PyObject *w) |
| 13146 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13147 | if (!PyUnicode_Check(v)) |
| 13148 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13149 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13150 | } |
| 13151 | |
| 13152 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13153 | 0, /*nb_add*/ |
| 13154 | 0, /*nb_subtract*/ |
| 13155 | 0, /*nb_multiply*/ |
| 13156 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13157 | }; |
| 13158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13159 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13160 | (lenfunc) unicode_length, /* sq_length */ |
| 13161 | PyUnicode_Concat, /* sq_concat */ |
| 13162 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13163 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13164 | 0, /* sq_slice */ |
| 13165 | 0, /* sq_ass_item */ |
| 13166 | 0, /* sq_ass_slice */ |
| 13167 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | }; |
| 13169 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13170 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13171 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13172 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13173 | if (PyUnicode_READY(self) == -1) |
| 13174 | return NULL; |
| 13175 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13176 | if (PyIndex_Check(item)) { |
| 13177 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13178 | if (i == -1 && PyErr_Occurred()) |
| 13179 | return NULL; |
| 13180 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13181 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13182 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13183 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13184 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13185 | PyObject *result; |
| 13186 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13187 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13188 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13189 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13190 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13191 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13192 | return NULL; |
| 13193 | } |
| 13194 | |
| 13195 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13196 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13197 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13198 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13199 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13200 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13201 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13202 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13203 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13204 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13205 | src_kind = PyUnicode_KIND(self); |
| 13206 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13207 | if (!PyUnicode_IS_ASCII(self)) { |
| 13208 | kind_limit = kind_maxchar_limit(src_kind); |
| 13209 | max_char = 0; |
| 13210 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13211 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13212 | if (ch > max_char) { |
| 13213 | max_char = ch; |
| 13214 | if (max_char >= kind_limit) |
| 13215 | break; |
| 13216 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13217 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13218 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13219 | else |
| 13220 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13221 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13222 | if (result == NULL) |
| 13223 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13224 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13225 | dest_data = PyUnicode_DATA(result); |
| 13226 | |
| 13227 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13228 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13229 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13230 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13231 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13232 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13233 | } else { |
| 13234 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13235 | return NULL; |
| 13236 | } |
| 13237 | } |
| 13238 | |
| 13239 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13240 | (lenfunc)unicode_length, /* mp_length */ |
| 13241 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13242 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13243 | }; |
| 13244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13246 | /* Helpers for PyUnicode_Format() */ |
| 13247 | |
| 13248 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13249 | 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] | 13250 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13251 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13252 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13253 | (*p_argidx)++; |
| 13254 | if (arglen < 0) |
| 13255 | return args; |
| 13256 | else |
| 13257 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13258 | } |
| 13259 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13260 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13261 | return NULL; |
| 13262 | } |
| 13263 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13264 | /* 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] | 13265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 | static int |
| 13267 | formatfloat(PyObject *v, int flags, int prec, int type, |
| 13268 | PyObject **p_output, _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13269 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13270 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13271 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13272 | Py_ssize_t len; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13274 | x = PyFloat_AsDouble(v); |
| 13275 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13276 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13278 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13279 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13280 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13281 | p = PyOS_double_to_string(x, type, prec, |
| 13282 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13283 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13284 | return -1; |
| 13285 | len = strlen(p); |
| 13286 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13287 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13288 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13289 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13290 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13291 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13292 | writer->pos += len; |
| 13293 | } |
| 13294 | else |
| 13295 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13296 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13297 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13298 | } |
| 13299 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13300 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13301 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13302 | * Python's regular ints. |
| 13303 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13304 | * The output string is of the form |
| 13305 | * "-"? ("0x" | "0X")? digit+ |
| 13306 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13307 | * set in flags. The case of hex digits will be correct, |
| 13308 | * There will be at least prec digits, zero-filled on the left if |
| 13309 | * necessary to get that many. |
| 13310 | * val object to be converted |
| 13311 | * flags bitmask of format flags; only F_ALT is looked at |
| 13312 | * prec minimum number of digits; 0-fill on left if needed |
| 13313 | * type a character in [duoxX]; u acts the same as d |
| 13314 | * |
| 13315 | * CAUTION: o, x and X conversions on regular ints can never |
| 13316 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13317 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13318 | static PyObject* |
| 13319 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13320 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13321 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13322 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13323 | Py_ssize_t i; |
| 13324 | int sign; /* 1 if '-', else 0 */ |
| 13325 | int len; /* number of characters */ |
| 13326 | Py_ssize_t llen; |
| 13327 | int numdigits; /* len == numnondigits + numdigits */ |
| 13328 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13329 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13330 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13331 | if (prec > INT_MAX-3) { |
| 13332 | PyErr_SetString(PyExc_OverflowError, |
| 13333 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13334 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13335 | } |
| 13336 | |
| 13337 | assert(PyLong_Check(val)); |
| 13338 | |
| 13339 | switch (type) { |
| 13340 | case 'd': |
| 13341 | case 'u': |
| 13342 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13343 | if (PyBool_Check(val)) |
| 13344 | result = PyNumber_ToBase(val, 10); |
| 13345 | else |
| 13346 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13347 | break; |
| 13348 | case 'o': |
| 13349 | numnondigits = 2; |
| 13350 | result = PyNumber_ToBase(val, 8); |
| 13351 | break; |
| 13352 | case 'x': |
| 13353 | case 'X': |
| 13354 | numnondigits = 2; |
| 13355 | result = PyNumber_ToBase(val, 16); |
| 13356 | break; |
| 13357 | default: |
| 13358 | assert(!"'type' not in [duoxX]"); |
| 13359 | } |
| 13360 | if (!result) |
| 13361 | return NULL; |
| 13362 | |
| 13363 | assert(unicode_modifiable(result)); |
| 13364 | assert(PyUnicode_IS_READY(result)); |
| 13365 | assert(PyUnicode_IS_ASCII(result)); |
| 13366 | |
| 13367 | /* To modify the string in-place, there can only be one reference. */ |
| 13368 | if (Py_REFCNT(result) != 1) { |
| 13369 | PyErr_BadInternalCall(); |
| 13370 | return NULL; |
| 13371 | } |
| 13372 | buf = PyUnicode_DATA(result); |
| 13373 | llen = PyUnicode_GET_LENGTH(result); |
| 13374 | if (llen > INT_MAX) { |
| 13375 | PyErr_SetString(PyExc_ValueError, |
| 13376 | "string too large in _PyBytes_FormatLong"); |
| 13377 | return NULL; |
| 13378 | } |
| 13379 | len = (int)llen; |
| 13380 | sign = buf[0] == '-'; |
| 13381 | numnondigits += sign; |
| 13382 | numdigits = len - numnondigits; |
| 13383 | assert(numdigits > 0); |
| 13384 | |
| 13385 | /* Get rid of base marker unless F_ALT */ |
| 13386 | if (((flags & F_ALT) == 0 && |
| 13387 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13388 | assert(buf[sign] == '0'); |
| 13389 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13390 | buf[sign+1] == 'o'); |
| 13391 | numnondigits -= 2; |
| 13392 | buf += 2; |
| 13393 | len -= 2; |
| 13394 | if (sign) |
| 13395 | buf[0] = '-'; |
| 13396 | assert(len == numnondigits + numdigits); |
| 13397 | assert(numdigits > 0); |
| 13398 | } |
| 13399 | |
| 13400 | /* Fill with leading zeroes to meet minimum width. */ |
| 13401 | if (prec > numdigits) { |
| 13402 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13403 | numnondigits + prec); |
| 13404 | char *b1; |
| 13405 | if (!r1) { |
| 13406 | Py_DECREF(result); |
| 13407 | return NULL; |
| 13408 | } |
| 13409 | b1 = PyBytes_AS_STRING(r1); |
| 13410 | for (i = 0; i < numnondigits; ++i) |
| 13411 | *b1++ = *buf++; |
| 13412 | for (i = 0; i < prec - numdigits; i++) |
| 13413 | *b1++ = '0'; |
| 13414 | for (i = 0; i < numdigits; i++) |
| 13415 | *b1++ = *buf++; |
| 13416 | *b1 = '\0'; |
| 13417 | Py_DECREF(result); |
| 13418 | result = r1; |
| 13419 | buf = PyBytes_AS_STRING(result); |
| 13420 | len = numnondigits + prec; |
| 13421 | } |
| 13422 | |
| 13423 | /* Fix up case for hex conversions. */ |
| 13424 | if (type == 'X') { |
| 13425 | /* Need to convert all lower case letters to upper case. |
| 13426 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13427 | for (i = 0; i < len; i++) |
| 13428 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13429 | buf[i] -= 'a'-'A'; |
| 13430 | } |
| 13431 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13432 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13433 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13434 | Py_DECREF(result); |
| 13435 | result = unicode; |
| 13436 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13437 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13438 | } |
| 13439 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13440 | static Py_UCS4 |
| 13441 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13442 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13443 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13444 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13445 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13446 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13447 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | goto onError; |
| 13449 | } |
| 13450 | else { |
| 13451 | /* Integer input truncated to a character */ |
| 13452 | long x; |
| 13453 | x = PyLong_AsLong(v); |
| 13454 | if (x == -1 && PyErr_Occurred()) |
| 13455 | goto onError; |
| 13456 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13457 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13458 | PyErr_SetString(PyExc_OverflowError, |
| 13459 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13460 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13461 | } |
| 13462 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13463 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13464 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13465 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13466 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13467 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13468 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13469 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13470 | } |
| 13471 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13472 | PyObject * |
| 13473 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13474 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13475 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13476 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13477 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13478 | PyObject *temp = NULL; |
| 13479 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13480 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13481 | void *fmt; |
| 13482 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13483 | _PyUnicodeWriter writer; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13484 | Py_ssize_t sublen; |
| 13485 | Py_UCS4 maxchar; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13487 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13488 | PyErr_BadInternalCall(); |
| 13489 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13490 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13491 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13492 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13493 | return NULL; |
Victor Stinner | 1929407 | 2012-10-05 00:09:33 +0200 | [diff] [blame] | 13494 | if (PyUnicode_READY(uformat) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13495 | Py_DECREF(uformat); |
Victor Stinner | 1929407 | 2012-10-05 00:09:33 +0200 | [diff] [blame] | 13496 | return NULL; |
| 13497 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13498 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13499 | fmt = PyUnicode_DATA(uformat); |
| 13500 | fmtkind = PyUnicode_KIND(uformat); |
| 13501 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13502 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13503 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13504 | _PyUnicodeWriter_Init(&writer, fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13506 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13507 | arglen = PyTuple_Size(args); |
| 13508 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13509 | } |
| 13510 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13511 | arglen = -1; |
| 13512 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13513 | } |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 13514 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13515 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13516 | |
| 13517 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13518 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13519 | Py_ssize_t nonfmtpos; |
| 13520 | nonfmtpos = fmtpos++; |
| 13521 | while (fmtcnt >= 0 && |
| 13522 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13523 | fmtpos++; |
| 13524 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13525 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13526 | if (fmtcnt < 0) |
| 13527 | fmtpos--; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13528 | sublen = fmtpos - nonfmtpos; |
| 13529 | maxchar = _PyUnicode_FindMaxChar(uformat, |
| 13530 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13531 | if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13532 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13533 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13534 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13535 | uformat, nonfmtpos, sublen); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13536 | writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13537 | } |
| 13538 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13539 | /* Got a format specifier */ |
| 13540 | int flags = 0; |
| 13541 | Py_ssize_t width = -1; |
| 13542 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13543 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13544 | Py_UCS4 fill; |
| 13545 | int sign; |
| 13546 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13547 | int isnumok; |
| 13548 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13549 | void *pbuf = NULL; |
| 13550 | Py_ssize_t pindex, len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13551 | Py_UCS4 bufmaxchar; |
| 13552 | Py_ssize_t buflen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13553 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13554 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13555 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13556 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13557 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | Py_ssize_t keylen; |
| 13559 | PyObject *key; |
| 13560 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13561 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13562 | if (dict == NULL) { |
| 13563 | PyErr_SetString(PyExc_TypeError, |
| 13564 | "format requires a mapping"); |
| 13565 | goto onError; |
| 13566 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13567 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13569 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13570 | /* Skip over balanced parentheses */ |
| 13571 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13572 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13573 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13574 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13575 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13576 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13577 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13578 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13579 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13580 | if (fmtcnt < 0 || pcount > 0) { |
| 13581 | PyErr_SetString(PyExc_ValueError, |
| 13582 | "incomplete format key"); |
| 13583 | goto onError; |
| 13584 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13585 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13586 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13587 | if (key == NULL) |
| 13588 | goto onError; |
| 13589 | if (args_owned) { |
| 13590 | Py_DECREF(args); |
| 13591 | args_owned = 0; |
| 13592 | } |
| 13593 | args = PyObject_GetItem(dict, key); |
| 13594 | Py_DECREF(key); |
| 13595 | if (args == NULL) { |
| 13596 | goto onError; |
| 13597 | } |
| 13598 | args_owned = 1; |
| 13599 | arglen = -1; |
| 13600 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13601 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13603 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13604 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13605 | case '-': flags |= F_LJUST; continue; |
| 13606 | case '+': flags |= F_SIGN; continue; |
| 13607 | case ' ': flags |= F_BLANK; continue; |
| 13608 | case '#': flags |= F_ALT; continue; |
| 13609 | case '0': flags |= F_ZERO; continue; |
| 13610 | } |
| 13611 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13612 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13613 | if (c == '*') { |
| 13614 | v = getnextarg(args, arglen, &argidx); |
| 13615 | if (v == NULL) |
| 13616 | goto onError; |
| 13617 | if (!PyLong_Check(v)) { |
| 13618 | PyErr_SetString(PyExc_TypeError, |
| 13619 | "* wants int"); |
| 13620 | goto onError; |
| 13621 | } |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 13622 | width = PyLong_AsSsize_t(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13623 | if (width == -1 && PyErr_Occurred()) |
| 13624 | goto onError; |
| 13625 | if (width < 0) { |
| 13626 | flags |= F_LJUST; |
| 13627 | width = -width; |
| 13628 | } |
| 13629 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13630 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13631 | } |
| 13632 | else if (c >= '0' && c <= '9') { |
| 13633 | width = c - '0'; |
| 13634 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13635 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13636 | if (c < '0' || c > '9') |
| 13637 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13638 | /* Since c is unsigned, the RHS would end up as unsigned, |
| 13639 | mixing signed and unsigned comparison. Since c is between |
| 13640 | '0' and '9', casting to int is safe. */ |
| 13641 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13642 | PyErr_SetString(PyExc_ValueError, |
| 13643 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13644 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13645 | } |
| 13646 | width = width*10 + (c - '0'); |
| 13647 | } |
| 13648 | } |
| 13649 | if (c == '.') { |
| 13650 | prec = 0; |
| 13651 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13652 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13653 | if (c == '*') { |
| 13654 | v = getnextarg(args, arglen, &argidx); |
| 13655 | if (v == NULL) |
| 13656 | goto onError; |
| 13657 | if (!PyLong_Check(v)) { |
| 13658 | PyErr_SetString(PyExc_TypeError, |
| 13659 | "* wants int"); |
| 13660 | goto onError; |
| 13661 | } |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 13662 | prec = _PyLong_AsInt(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13663 | if (prec == -1 && PyErr_Occurred()) |
| 13664 | goto onError; |
| 13665 | if (prec < 0) |
| 13666 | prec = 0; |
| 13667 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13668 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13669 | } |
| 13670 | else if (c >= '0' && c <= '9') { |
| 13671 | prec = c - '0'; |
| 13672 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13673 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13674 | if (c < '0' || c > '9') |
| 13675 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13676 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13677 | PyErr_SetString(PyExc_ValueError, |
| 13678 | "prec too big"); |
| 13679 | goto onError; |
| 13680 | } |
| 13681 | prec = prec*10 + (c - '0'); |
| 13682 | } |
| 13683 | } |
| 13684 | } /* prec */ |
| 13685 | if (fmtcnt >= 0) { |
| 13686 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13687 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13688 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13689 | } |
| 13690 | } |
| 13691 | if (fmtcnt < 0) { |
| 13692 | PyErr_SetString(PyExc_ValueError, |
| 13693 | "incomplete format"); |
| 13694 | goto onError; |
| 13695 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13696 | if (fmtcnt == 0) |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13697 | writer.overallocate = 0; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13698 | |
| 13699 | if (c == '%') { |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13700 | if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13701 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13702 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%'); |
| 13703 | writer.pos += 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13704 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13705 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13706 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13707 | v = getnextarg(args, arglen, &argidx); |
| 13708 | if (v == NULL) |
| 13709 | goto onError; |
| 13710 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13711 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13712 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13713 | fill = ' '; |
| 13714 | switch (c) { |
| 13715 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13716 | case 's': |
| 13717 | case 'r': |
| 13718 | case 'a': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13719 | if (PyLong_CheckExact(v) && width == -1 && prec == -1) { |
| 13720 | /* Fast path */ |
| 13721 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13722 | goto onError; |
| 13723 | goto nextarg; |
| 13724 | } |
| 13725 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13726 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13727 | temp = v; |
| 13728 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13729 | } |
| 13730 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13731 | if (c == 's') |
| 13732 | temp = PyObject_Str(v); |
| 13733 | else if (c == 'r') |
| 13734 | temp = PyObject_Repr(v); |
| 13735 | else |
| 13736 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13737 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13738 | break; |
| 13739 | |
| 13740 | case 'i': |
| 13741 | case 'd': |
| 13742 | case 'u': |
| 13743 | case 'o': |
| 13744 | case 'x': |
| 13745 | case 'X': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13746 | if (PyLong_CheckExact(v) |
| 13747 | && width == -1 && prec == -1 |
| 13748 | && !(flags & (F_SIGN | F_BLANK))) |
| 13749 | { |
| 13750 | /* Fast path */ |
| 13751 | switch(c) |
| 13752 | { |
| 13753 | case 'd': |
| 13754 | case 'i': |
| 13755 | case 'u': |
| 13756 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13757 | goto onError; |
| 13758 | goto nextarg; |
| 13759 | case 'x': |
| 13760 | if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1) |
| 13761 | goto onError; |
| 13762 | goto nextarg; |
| 13763 | case 'o': |
| 13764 | if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1) |
| 13765 | goto onError; |
| 13766 | goto nextarg; |
| 13767 | default: |
| 13768 | break; |
| 13769 | } |
| 13770 | } |
| 13771 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13772 | isnumok = 0; |
| 13773 | if (PyNumber_Check(v)) { |
| 13774 | PyObject *iobj=NULL; |
| 13775 | |
| 13776 | if (PyLong_Check(v)) { |
| 13777 | iobj = v; |
| 13778 | Py_INCREF(iobj); |
| 13779 | } |
| 13780 | else { |
| 13781 | iobj = PyNumber_Long(v); |
| 13782 | } |
| 13783 | if (iobj!=NULL) { |
| 13784 | if (PyLong_Check(iobj)) { |
| 13785 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13786 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13787 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13788 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13789 | } |
| 13790 | else { |
| 13791 | Py_DECREF(iobj); |
| 13792 | } |
| 13793 | } |
| 13794 | } |
| 13795 | if (!isnumok) { |
| 13796 | PyErr_Format(PyExc_TypeError, |
| 13797 | "%%%c format: a number is required, " |
| 13798 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13799 | goto onError; |
| 13800 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13801 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13802 | fill = '0'; |
| 13803 | break; |
| 13804 | |
| 13805 | case 'e': |
| 13806 | case 'E': |
| 13807 | case 'f': |
| 13808 | case 'F': |
| 13809 | case 'g': |
| 13810 | case 'G': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13811 | if (width == -1 && prec == -1 |
| 13812 | && !(flags & (F_SIGN | F_BLANK))) |
| 13813 | { |
| 13814 | /* Fast path */ |
| 13815 | if (formatfloat(v, flags, prec, c, NULL, &writer) == -1) |
| 13816 | goto onError; |
| 13817 | goto nextarg; |
| 13818 | } |
| 13819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13820 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13821 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13822 | fill = '0'; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13823 | if (formatfloat(v, flags, prec, c, &temp, NULL) == -1) |
| 13824 | temp = NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13825 | break; |
| 13826 | |
| 13827 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13828 | { |
| 13829 | Py_UCS4 ch = formatchar(v); |
| 13830 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13831 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13832 | if (width == -1 && prec == -1) { |
| 13833 | /* Fast path */ |
| 13834 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 13835 | goto onError; |
| 13836 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 13837 | writer.pos += 1; |
| 13838 | goto nextarg; |
| 13839 | } |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13840 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13841 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13842 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13843 | |
| 13844 | default: |
| 13845 | PyErr_Format(PyExc_ValueError, |
| 13846 | "unsupported format character '%c' (0x%x) " |
| 13847 | "at index %zd", |
| 13848 | (31<=c && c<=126) ? (char)c : '?', |
| 13849 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13850 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13851 | goto onError; |
| 13852 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13853 | if (temp == NULL) |
| 13854 | goto onError; |
| 13855 | assert (PyUnicode_Check(temp)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13856 | |
| 13857 | if (width == -1 && prec == -1 |
| 13858 | && !(flags & (F_SIGN | F_BLANK))) |
| 13859 | { |
| 13860 | /* Fast path */ |
| 13861 | if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1) |
| 13862 | goto onError; |
| 13863 | goto nextarg; |
| 13864 | } |
| 13865 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13866 | if (PyUnicode_READY(temp) == -1) { |
| 13867 | Py_CLEAR(temp); |
| 13868 | goto onError; |
| 13869 | } |
| 13870 | kind = PyUnicode_KIND(temp); |
| 13871 | pbuf = PyUnicode_DATA(temp); |
| 13872 | len = PyUnicode_GET_LENGTH(temp); |
| 13873 | |
| 13874 | if (c == 's' || c == 'r' || c == 'a') { |
| 13875 | if (prec >= 0 && len > prec) |
| 13876 | len = prec; |
| 13877 | } |
| 13878 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13879 | /* pbuf is initialized here. */ |
| 13880 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13881 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13882 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13883 | if (ch == '-' || ch == '+') { |
| 13884 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13885 | len--; |
| 13886 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | } |
| 13888 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13889 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13890 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13891 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13892 | else |
| 13893 | sign = 0; |
| 13894 | } |
| 13895 | if (width < len) |
| 13896 | width = len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13897 | |
| 13898 | /* Compute the length and maximum character of the |
| 13899 | written characters */ |
| 13900 | bufmaxchar = 127; |
| 13901 | if (!(flags & F_LJUST)) { |
| 13902 | if (sign) { |
| 13903 | if ((width-1) > len) |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 13904 | bufmaxchar = Py_MAX(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13905 | } |
| 13906 | else { |
| 13907 | if (width > len) |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 13908 | bufmaxchar = Py_MAX(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13909 | } |
| 13910 | } |
| 13911 | maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 13912 | bufmaxchar = Py_MAX(bufmaxchar, maxchar); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13913 | |
| 13914 | buflen = width; |
| 13915 | if (sign && len == width) |
| 13916 | buflen++; |
| 13917 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13918 | if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1) |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13919 | goto onError; |
| 13920 | |
| 13921 | /* Write characters */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13922 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13923 | if (fill != ' ') { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13924 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13925 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13926 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13927 | if (width > len) |
| 13928 | width--; |
| 13929 | } |
| 13930 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13931 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13932 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13933 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13934 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13935 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13936 | writer.pos += 2; |
| 13937 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13939 | width -= 2; |
| 13940 | if (width < 0) |
| 13941 | width = 0; |
| 13942 | len -= 2; |
| 13943 | } |
| 13944 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13945 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13946 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 13947 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13948 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13949 | } |
| 13950 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13951 | if (sign) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13952 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13953 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13954 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13955 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13956 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13957 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13958 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13959 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13960 | writer.pos += 2; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13961 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13962 | } |
| 13963 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13964 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 13965 | if (len) { |
| 13966 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13967 | temp, pindex, len); |
| 13968 | writer.pos += len; |
| 13969 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13970 | if (width > len) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13971 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13972 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 13973 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13974 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13975 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13976 | nextarg: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13977 | if (dict && (argidx < arglen) && c != '%') { |
| 13978 | PyErr_SetString(PyExc_TypeError, |
| 13979 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13980 | goto onError; |
| 13981 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13982 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13983 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13984 | } /* until end */ |
| 13985 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13986 | PyErr_SetString(PyExc_TypeError, |
| 13987 | "not all arguments converted during string formatting"); |
| 13988 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13989 | } |
| 13990 | |
| 13991 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13992 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13993 | } |
| 13994 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13995 | Py_XDECREF(temp); |
| 13996 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13997 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13998 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13999 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14000 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14001 | Py_XDECREF(temp); |
| 14002 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 14003 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14004 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14005 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14006 | } |
| 14007 | return NULL; |
| 14008 | } |
| 14009 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14010 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14011 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14012 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14013 | static PyObject * |
| 14014 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14015 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14016 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14017 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14018 | char *encoding = NULL; |
| 14019 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14020 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14021 | if (type != &PyUnicode_Type) |
| 14022 | return unicode_subtype_new(type, args, kwds); |
| 14023 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14024 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14025 | return NULL; |
| 14026 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14027 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14028 | if (encoding == NULL && errors == NULL) |
| 14029 | return PyObject_Str(x); |
| 14030 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14031 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14032 | } |
| 14033 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14034 | static PyObject * |
| 14035 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14036 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14037 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14038 | Py_ssize_t length, char_size; |
| 14039 | int share_wstr, share_utf8; |
| 14040 | unsigned int kind; |
| 14041 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14042 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14043 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14044 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14045 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14046 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14047 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14048 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14049 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14050 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14051 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14052 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14053 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14054 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14055 | if (self == NULL) { |
| 14056 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | return NULL; |
| 14058 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14059 | kind = PyUnicode_KIND(unicode); |
| 14060 | length = PyUnicode_GET_LENGTH(unicode); |
| 14061 | |
| 14062 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14063 | #ifdef Py_DEBUG |
| 14064 | _PyUnicode_HASH(self) = -1; |
| 14065 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14066 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14067 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14068 | _PyUnicode_STATE(self).interned = 0; |
| 14069 | _PyUnicode_STATE(self).kind = kind; |
| 14070 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14071 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14072 | _PyUnicode_STATE(self).ready = 1; |
| 14073 | _PyUnicode_WSTR(self) = NULL; |
| 14074 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14075 | _PyUnicode_UTF8(self) = NULL; |
| 14076 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14077 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14078 | |
| 14079 | share_utf8 = 0; |
| 14080 | share_wstr = 0; |
| 14081 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14082 | char_size = 1; |
| 14083 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14084 | share_utf8 = 1; |
| 14085 | } |
| 14086 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14087 | char_size = 2; |
| 14088 | if (sizeof(wchar_t) == 2) |
| 14089 | share_wstr = 1; |
| 14090 | } |
| 14091 | else { |
| 14092 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14093 | char_size = 4; |
| 14094 | if (sizeof(wchar_t) == 4) |
| 14095 | share_wstr = 1; |
| 14096 | } |
| 14097 | |
| 14098 | /* Ensure we won't overflow the length. */ |
| 14099 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14100 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14101 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14102 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14103 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14104 | if (data == NULL) { |
| 14105 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14106 | goto onError; |
| 14107 | } |
| 14108 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14109 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14110 | if (share_utf8) { |
| 14111 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14112 | _PyUnicode_UTF8(self) = data; |
| 14113 | } |
| 14114 | if (share_wstr) { |
| 14115 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14116 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14117 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14118 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14119 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14120 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14121 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14122 | #ifdef Py_DEBUG |
| 14123 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14124 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14125 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14126 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14127 | |
| 14128 | onError: |
| 14129 | Py_DECREF(unicode); |
| 14130 | Py_DECREF(self); |
| 14131 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14132 | } |
| 14133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14134 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14135 | "str(object='') -> str\n\ |
| 14136 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14137 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14138 | Create a new string object from the given object. If encoding or\n\ |
| 14139 | errors is specified, then the object must expose a data buffer\n\ |
| 14140 | that will be decoded using the given encoding and error handler.\n\ |
| 14141 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14142 | or repr(object).\n\ |
| 14143 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14144 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14145 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14146 | static PyObject *unicode_iter(PyObject *seq); |
| 14147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14148 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14149 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14150 | "str", /* tp_name */ |
| 14151 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14152 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14153 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14154 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14155 | 0, /* tp_print */ |
| 14156 | 0, /* tp_getattr */ |
| 14157 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14158 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14159 | unicode_repr, /* tp_repr */ |
| 14160 | &unicode_as_number, /* tp_as_number */ |
| 14161 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14162 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14163 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14164 | 0, /* tp_call*/ |
| 14165 | (reprfunc) unicode_str, /* tp_str */ |
| 14166 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14167 | 0, /* tp_setattro */ |
| 14168 | 0, /* tp_as_buffer */ |
| 14169 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14170 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14171 | unicode_doc, /* tp_doc */ |
| 14172 | 0, /* tp_traverse */ |
| 14173 | 0, /* tp_clear */ |
| 14174 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14175 | 0, /* tp_weaklistoffset */ |
| 14176 | unicode_iter, /* tp_iter */ |
| 14177 | 0, /* tp_iternext */ |
| 14178 | unicode_methods, /* tp_methods */ |
| 14179 | 0, /* tp_members */ |
| 14180 | 0, /* tp_getset */ |
| 14181 | &PyBaseObject_Type, /* tp_base */ |
| 14182 | 0, /* tp_dict */ |
| 14183 | 0, /* tp_descr_get */ |
| 14184 | 0, /* tp_descr_set */ |
| 14185 | 0, /* tp_dictoffset */ |
| 14186 | 0, /* tp_init */ |
| 14187 | 0, /* tp_alloc */ |
| 14188 | unicode_new, /* tp_new */ |
| 14189 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | }; |
| 14191 | |
| 14192 | /* Initialize the Unicode implementation */ |
| 14193 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14194 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14195 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14196 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14197 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14198 | 0x000A, /* LINE FEED */ |
| 14199 | 0x000D, /* CARRIAGE RETURN */ |
| 14200 | 0x001C, /* FILE SEPARATOR */ |
| 14201 | 0x001D, /* GROUP SEPARATOR */ |
| 14202 | 0x001E, /* RECORD SEPARATOR */ |
| 14203 | 0x0085, /* NEXT LINE */ |
| 14204 | 0x2028, /* LINE SEPARATOR */ |
| 14205 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14206 | }; |
| 14207 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14208 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14209 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14210 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14211 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14212 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14213 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14214 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14215 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14216 | |
| 14217 | /* initialize the linebreak bloom filter */ |
| 14218 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14219 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14220 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14221 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14222 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14223 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14224 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14225 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14226 | Py_FatalError("Can't initialize field name iterator type"); |
| 14227 | |
| 14228 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14229 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14230 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14231 | #ifdef HAVE_MBCS |
| 14232 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14233 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14234 | PyErr_SetFromWindowsErr(0); |
| 14235 | return -1; |
| 14236 | } |
| 14237 | #endif |
| 14238 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14239 | } |
| 14240 | |
| 14241 | /* Finalize the Unicode implementation */ |
| 14242 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14243 | int |
| 14244 | PyUnicode_ClearFreeList(void) |
| 14245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14246 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14247 | } |
| 14248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14249 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14250 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14251 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14252 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14253 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14254 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14255 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14256 | for (i = 0; i < 256; i++) |
| 14257 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14258 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14259 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14260 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14261 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14262 | void |
| 14263 | PyUnicode_InternInPlace(PyObject **p) |
| 14264 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14265 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14266 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14267 | #ifdef Py_DEBUG |
| 14268 | assert(s != NULL); |
| 14269 | assert(_PyUnicode_CHECK(s)); |
| 14270 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14271 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14272 | return; |
| 14273 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14274 | /* If it's a subclass, we don't really know what putting |
| 14275 | it in the interned dict might do. */ |
| 14276 | if (!PyUnicode_CheckExact(s)) |
| 14277 | return; |
| 14278 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14279 | return; |
| 14280 | if (interned == NULL) { |
| 14281 | interned = PyDict_New(); |
| 14282 | if (interned == NULL) { |
| 14283 | PyErr_Clear(); /* Don't leave an exception */ |
| 14284 | return; |
| 14285 | } |
| 14286 | } |
| 14287 | /* It might be that the GetItem call fails even |
| 14288 | though the key is present in the dictionary, |
| 14289 | namely when this happens during a stack overflow. */ |
| 14290 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14291 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14292 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14293 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14294 | if (t) { |
| 14295 | Py_INCREF(t); |
| 14296 | Py_DECREF(*p); |
| 14297 | *p = t; |
| 14298 | return; |
| 14299 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14300 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14301 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14302 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14303 | PyErr_Clear(); |
| 14304 | PyThreadState_GET()->recursion_critical = 0; |
| 14305 | return; |
| 14306 | } |
| 14307 | PyThreadState_GET()->recursion_critical = 0; |
| 14308 | /* The two references in interned are not counted by refcnt. |
| 14309 | The deallocator will take care of this */ |
| 14310 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14311 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14312 | } |
| 14313 | |
| 14314 | void |
| 14315 | PyUnicode_InternImmortal(PyObject **p) |
| 14316 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14317 | PyUnicode_InternInPlace(p); |
| 14318 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14319 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14320 | Py_INCREF(*p); |
| 14321 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14322 | } |
| 14323 | |
| 14324 | PyObject * |
| 14325 | PyUnicode_InternFromString(const char *cp) |
| 14326 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14327 | PyObject *s = PyUnicode_FromString(cp); |
| 14328 | if (s == NULL) |
| 14329 | return NULL; |
| 14330 | PyUnicode_InternInPlace(&s); |
| 14331 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14332 | } |
| 14333 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14334 | void |
| 14335 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14336 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14337 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14338 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14339 | Py_ssize_t i, n; |
| 14340 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14341 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14342 | if (interned == NULL || !PyDict_Check(interned)) |
| 14343 | return; |
| 14344 | keys = PyDict_Keys(interned); |
| 14345 | if (keys == NULL || !PyList_Check(keys)) { |
| 14346 | PyErr_Clear(); |
| 14347 | return; |
| 14348 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14349 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14350 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14351 | detector, interned unicode strings are not forcibly deallocated; |
| 14352 | rather, we give them their stolen references back, and then clear |
| 14353 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14354 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14355 | n = PyList_GET_SIZE(keys); |
| 14356 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14357 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14358 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14359 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14360 | if (PyUnicode_READY(s) == -1) { |
| 14361 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14362 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14363 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14364 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14365 | case SSTATE_NOT_INTERNED: |
| 14366 | /* XXX Shouldn't happen */ |
| 14367 | break; |
| 14368 | case SSTATE_INTERNED_IMMORTAL: |
| 14369 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14370 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14371 | break; |
| 14372 | case SSTATE_INTERNED_MORTAL: |
| 14373 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14374 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14375 | break; |
| 14376 | default: |
| 14377 | Py_FatalError("Inconsistent interned string state."); |
| 14378 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14379 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14380 | } |
| 14381 | fprintf(stderr, "total size of all interned strings: " |
| 14382 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14383 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14384 | Py_DECREF(keys); |
| 14385 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14386 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14387 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14388 | |
| 14389 | |
| 14390 | /********************* Unicode Iterator **************************/ |
| 14391 | |
| 14392 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14393 | PyObject_HEAD |
| 14394 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14395 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14396 | } unicodeiterobject; |
| 14397 | |
| 14398 | static void |
| 14399 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14400 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14401 | _PyObject_GC_UNTRACK(it); |
| 14402 | Py_XDECREF(it->it_seq); |
| 14403 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14404 | } |
| 14405 | |
| 14406 | static int |
| 14407 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14408 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14409 | Py_VISIT(it->it_seq); |
| 14410 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14411 | } |
| 14412 | |
| 14413 | static PyObject * |
| 14414 | unicodeiter_next(unicodeiterobject *it) |
| 14415 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14416 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14417 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14418 | assert(it != NULL); |
| 14419 | seq = it->it_seq; |
| 14420 | if (seq == NULL) |
| 14421 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14422 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14424 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14425 | int kind = PyUnicode_KIND(seq); |
| 14426 | void *data = PyUnicode_DATA(seq); |
| 14427 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14428 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14429 | if (item != NULL) |
| 14430 | ++it->it_index; |
| 14431 | return item; |
| 14432 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14433 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14434 | Py_DECREF(seq); |
| 14435 | it->it_seq = NULL; |
| 14436 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14437 | } |
| 14438 | |
| 14439 | static PyObject * |
| 14440 | unicodeiter_len(unicodeiterobject *it) |
| 14441 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14442 | Py_ssize_t len = 0; |
| 14443 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14444 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14445 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14446 | } |
| 14447 | |
| 14448 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14449 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14450 | static PyObject * |
| 14451 | unicodeiter_reduce(unicodeiterobject *it) |
| 14452 | { |
| 14453 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14454 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14455 | it->it_seq, it->it_index); |
| 14456 | } else { |
| 14457 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14458 | if (u == NULL) |
| 14459 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14460 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14461 | } |
| 14462 | } |
| 14463 | |
| 14464 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14465 | |
| 14466 | static PyObject * |
| 14467 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14468 | { |
| 14469 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14470 | if (index == -1 && PyErr_Occurred()) |
| 14471 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 14472 | if (it->it_seq != NULL) { |
| 14473 | if (index < 0) |
| 14474 | index = 0; |
| 14475 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 14476 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 14477 | it->it_index = index; |
| 14478 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14479 | Py_RETURN_NONE; |
| 14480 | } |
| 14481 | |
| 14482 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14483 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14484 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14485 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14486 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14487 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14488 | reduce_doc}, |
| 14489 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14490 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14491 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14492 | }; |
| 14493 | |
| 14494 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14495 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14496 | "str_iterator", /* tp_name */ |
| 14497 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14498 | 0, /* tp_itemsize */ |
| 14499 | /* methods */ |
| 14500 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14501 | 0, /* tp_print */ |
| 14502 | 0, /* tp_getattr */ |
| 14503 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14504 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14505 | 0, /* tp_repr */ |
| 14506 | 0, /* tp_as_number */ |
| 14507 | 0, /* tp_as_sequence */ |
| 14508 | 0, /* tp_as_mapping */ |
| 14509 | 0, /* tp_hash */ |
| 14510 | 0, /* tp_call */ |
| 14511 | 0, /* tp_str */ |
| 14512 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14513 | 0, /* tp_setattro */ |
| 14514 | 0, /* tp_as_buffer */ |
| 14515 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14516 | 0, /* tp_doc */ |
| 14517 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14518 | 0, /* tp_clear */ |
| 14519 | 0, /* tp_richcompare */ |
| 14520 | 0, /* tp_weaklistoffset */ |
| 14521 | PyObject_SelfIter, /* tp_iter */ |
| 14522 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14523 | unicodeiter_methods, /* tp_methods */ |
| 14524 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14525 | }; |
| 14526 | |
| 14527 | static PyObject * |
| 14528 | unicode_iter(PyObject *seq) |
| 14529 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14530 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14531 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14532 | if (!PyUnicode_Check(seq)) { |
| 14533 | PyErr_BadInternalCall(); |
| 14534 | return NULL; |
| 14535 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14536 | if (PyUnicode_READY(seq) == -1) |
| 14537 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14538 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14539 | if (it == NULL) |
| 14540 | return NULL; |
| 14541 | it->it_index = 0; |
| 14542 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14543 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14544 | _PyObject_GC_TRACK(it); |
| 14545 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14546 | } |
| 14547 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14548 | |
| 14549 | size_t |
| 14550 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14551 | { |
| 14552 | int res = 0; |
| 14553 | while(*u++) |
| 14554 | res++; |
| 14555 | return res; |
| 14556 | } |
| 14557 | |
| 14558 | Py_UNICODE* |
| 14559 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14560 | { |
| 14561 | Py_UNICODE *u = s1; |
| 14562 | while ((*u++ = *s2++)); |
| 14563 | return s1; |
| 14564 | } |
| 14565 | |
| 14566 | Py_UNICODE* |
| 14567 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14568 | { |
| 14569 | Py_UNICODE *u = s1; |
| 14570 | while ((*u++ = *s2++)) |
| 14571 | if (n-- == 0) |
| 14572 | break; |
| 14573 | return s1; |
| 14574 | } |
| 14575 | |
| 14576 | Py_UNICODE* |
| 14577 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14578 | { |
| 14579 | Py_UNICODE *u1 = s1; |
| 14580 | u1 += Py_UNICODE_strlen(u1); |
| 14581 | Py_UNICODE_strcpy(u1, s2); |
| 14582 | return s1; |
| 14583 | } |
| 14584 | |
| 14585 | int |
| 14586 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14587 | { |
| 14588 | while (*s1 && *s2 && *s1 == *s2) |
| 14589 | s1++, s2++; |
| 14590 | if (*s1 && *s2) |
| 14591 | return (*s1 < *s2) ? -1 : +1; |
| 14592 | if (*s1) |
| 14593 | return 1; |
| 14594 | if (*s2) |
| 14595 | return -1; |
| 14596 | return 0; |
| 14597 | } |
| 14598 | |
| 14599 | int |
| 14600 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14601 | { |
| 14602 | register Py_UNICODE u1, u2; |
| 14603 | for (; n != 0; n--) { |
| 14604 | u1 = *s1; |
| 14605 | u2 = *s2; |
| 14606 | if (u1 != u2) |
| 14607 | return (u1 < u2) ? -1 : +1; |
| 14608 | if (u1 == '\0') |
| 14609 | return 0; |
| 14610 | s1++; |
| 14611 | s2++; |
| 14612 | } |
| 14613 | return 0; |
| 14614 | } |
| 14615 | |
| 14616 | Py_UNICODE* |
| 14617 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14618 | { |
| 14619 | const Py_UNICODE *p; |
| 14620 | for (p = s; *p; p++) |
| 14621 | if (*p == c) |
| 14622 | return (Py_UNICODE*)p; |
| 14623 | return NULL; |
| 14624 | } |
| 14625 | |
| 14626 | Py_UNICODE* |
| 14627 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14628 | { |
| 14629 | const Py_UNICODE *p; |
| 14630 | p = s + Py_UNICODE_strlen(s); |
| 14631 | while (p != s) { |
| 14632 | p--; |
| 14633 | if (*p == c) |
| 14634 | return (Py_UNICODE*)p; |
| 14635 | } |
| 14636 | return NULL; |
| 14637 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14638 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14639 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14640 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14641 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14642 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14643 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14645 | if (!PyUnicode_Check(unicode)) { |
| 14646 | PyErr_BadArgument(); |
| 14647 | return NULL; |
| 14648 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14649 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14650 | if (u == NULL) |
| 14651 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14652 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14653 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14654 | PyErr_NoMemory(); |
| 14655 | return NULL; |
| 14656 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14657 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14658 | size *= sizeof(Py_UNICODE); |
| 14659 | copy = PyMem_Malloc(size); |
| 14660 | if (copy == NULL) { |
| 14661 | PyErr_NoMemory(); |
| 14662 | return NULL; |
| 14663 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14664 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14665 | return copy; |
| 14666 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14667 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14668 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14669 | to the string.Formatter class implemented in Python. */ |
| 14670 | |
| 14671 | static PyMethodDef _string_methods[] = { |
| 14672 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14673 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14674 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14675 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14676 | {NULL, NULL} |
| 14677 | }; |
| 14678 | |
| 14679 | static struct PyModuleDef _string_module = { |
| 14680 | PyModuleDef_HEAD_INIT, |
| 14681 | "_string", |
| 14682 | PyDoc_STR("string helper module"), |
| 14683 | 0, |
| 14684 | _string_methods, |
| 14685 | NULL, |
| 14686 | NULL, |
| 14687 | NULL, |
| 14688 | NULL |
| 14689 | }; |
| 14690 | |
| 14691 | PyMODINIT_FUNC |
| 14692 | PyInit__string(void) |
| 14693 | { |
| 14694 | return PyModule_Create(&_string_module); |
| 14695 | } |
| 14696 | |
| 14697 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14698 | #ifdef __cplusplus |
| 14699 | } |
| 14700 | #endif |