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 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 50 | /* --- Globals ------------------------------------------------------------ |
| 51 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 52 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 53 | initialized dynamically as needed. In the process Unicode objects may |
| 54 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 55 | |
| 56 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | |
| 59 | #ifdef __cplusplus |
| 60 | extern "C" { |
| 61 | #endif |
| 62 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 63 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 64 | #define MAX_UNICODE 0x10ffff |
| 65 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 66 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 67 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 68 | #else |
| 69 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 70 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 71 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 72 | #define _PyUnicode_UTF8(op) \ |
| 73 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 74 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 75 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 76 | assert(PyUnicode_IS_READY(op)), \ |
| 77 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 78 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 79 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 80 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 81 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 82 | #define PyUnicode_UTF8_LENGTH(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 | ((PyASCIIObject*)(op))->length : \ |
| 87 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 88 | #define _PyUnicode_WSTR(op) \ |
| 89 | (((PyASCIIObject*)(op))->wstr) |
| 90 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 91 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 92 | #define _PyUnicode_LENGTH(op) \ |
| 93 | (((PyASCIIObject *)(op))->length) |
| 94 | #define _PyUnicode_STATE(op) \ |
| 95 | (((PyASCIIObject *)(op))->state) |
| 96 | #define _PyUnicode_HASH(op) \ |
| 97 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 98 | #define _PyUnicode_KIND(op) \ |
| 99 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 100 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 101 | #define _PyUnicode_GET_LENGTH(op) \ |
| 102 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 103 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 104 | #define _PyUnicode_DATA_ANY(op) \ |
| 105 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 107 | #undef PyUnicode_READY |
| 108 | #define PyUnicode_READY(op) \ |
| 109 | (assert(_PyUnicode_CHECK(op)), \ |
| 110 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 111 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 112 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 114 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 115 | (assert(_PyUnicode_CHECK(op)), \ |
| 116 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 117 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 118 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 119 | (assert(_PyUnicode_CHECK(op)), \ |
| 120 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 121 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 122 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 123 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 124 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 125 | ((!PyUnicode_IS_COMPACT_ASCII(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 126 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 127 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 128 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 129 | /* true if the Unicode object has an allocated wstr memory block |
| 130 | (not shared with other data) */ |
| 131 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 132 | ((_PyUnicode_WSTR(op) && \ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 133 | (!PyUnicode_IS_READY(op) || \ |
| 134 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 135 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 136 | /* Generic helper macro to convert characters of different types. |
| 137 | from_type and to_type have to be valid type names, begin and end |
| 138 | are pointers to the source characters which should be of type |
| 139 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 140 | buffer where the result characters are written to. */ |
| 141 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 142 | do { \ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 143 | to_type *_to = (to_type *)(to); \ |
| 144 | const from_type *_iter = (from_type *)(begin); \ |
| 145 | const from_type *_end = (from_type *)(end); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 146 | Py_ssize_t n = (_end) - (_iter); \ |
| 147 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 148 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 149 | while (_iter < (_unrolled_end)) { \ |
| 150 | _to[0] = (to_type) _iter[0]; \ |
| 151 | _to[1] = (to_type) _iter[1]; \ |
| 152 | _to[2] = (to_type) _iter[2]; \ |
| 153 | _to[3] = (to_type) _iter[3]; \ |
| 154 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 155 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 156 | while (_iter < (_end)) \ |
| 157 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 158 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 159 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 160 | /* This dictionary holds all interned unicode strings. Note that references |
| 161 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 162 | When the interned string reaches a refcnt of 0 the string deallocation |
| 163 | function will delete the reference from this dictionary. |
| 164 | |
| 165 | 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] | 166 | 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] | 167 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 168 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 169 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 170 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 171 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 172 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 173 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 174 | do { \ |
| 175 | if (unicode_empty != NULL) \ |
| 176 | Py_INCREF(unicode_empty); \ |
| 177 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 178 | unicode_empty = PyUnicode_New(0, 0); \ |
| 179 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 180 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 181 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 182 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 183 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 184 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 185 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 186 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 187 | do { \ |
| 188 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 189 | return unicode_empty; \ |
| 190 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 192 | /* Forward declaration */ |
| 193 | Py_LOCAL_INLINE(int) |
| 194 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 195 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 196 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 197 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 198 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | /* Single character Unicode strings in the Latin-1 range are being |
| 200 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 201 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 202 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 203 | /* Fast detection of the most frequent whitespace characters */ |
| 204 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 206 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 207 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 208 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /* case 0x000C: * FORM FEED */ |
| 210 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 213 | /* case 0x001C: * FILE SEPARATOR */ |
| 214 | /* case 0x001D: * GROUP SEPARATOR */ |
| 215 | /* case 0x001E: * RECORD SEPARATOR */ |
| 216 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 217 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 219 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 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, |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 234 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 235 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 236 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 237 | static int unicode_modifiable(PyObject *unicode); |
| 238 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 240 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 241 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 242 | static PyObject * |
| 243 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 244 | static PyObject * |
| 245 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 246 | |
| 247 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 248 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 249 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 250 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 251 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 252 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 253 | static void |
| 254 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 255 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 256 | PyObject *unicode, |
| 257 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 258 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 259 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 260 | /* Same for linebreaks */ |
| 261 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 262 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 263 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 264 | /* 0x000B, * LINE TABULATION */ |
| 265 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 267 | 0, 0, 1, 1, 1, 1, 0, 0, |
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 | /* 0x001C, * FILE SEPARATOR */ |
| 270 | /* 0x001D, * GROUP SEPARATOR */ |
| 271 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 272 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 277 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 278 | 0, 0, 0, 0, 0, 0, 0, 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, |
| 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 288 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 289 | 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] | 290 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 291 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 292 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 293 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 294 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 296 | /* This is actually an illegal character, so it should |
| 297 | not be passed to unichr. */ |
| 298 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 299 | #endif |
| 300 | } |
| 301 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 302 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 303 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 304 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | { |
| 306 | PyASCIIObject *ascii; |
| 307 | unsigned int kind; |
| 308 | |
| 309 | assert(PyUnicode_Check(op)); |
| 310 | |
| 311 | ascii = (PyASCIIObject *)op; |
| 312 | kind = ascii->state.kind; |
| 313 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 314 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 315 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 | assert(ascii->state.ready == 1); |
| 317 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 318 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 319 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 320 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 322 | if (ascii->state.compact == 1) { |
| 323 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 324 | assert(kind == PyUnicode_1BYTE_KIND |
| 325 | || kind == PyUnicode_2BYTE_KIND |
| 326 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 327 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 330 | } |
| 331 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 332 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 333 | |
| 334 | data = unicode->data.any; |
| 335 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 336 | assert(ascii->length == 0); |
| 337 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 338 | assert(ascii->state.compact == 0); |
| 339 | assert(ascii->state.ascii == 0); |
| 340 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 341 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 342 | assert(ascii->wstr != NULL); |
| 343 | assert(data == NULL); |
| 344 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 | } |
| 346 | else { |
| 347 | assert(kind == PyUnicode_1BYTE_KIND |
| 348 | || kind == PyUnicode_2BYTE_KIND |
| 349 | || kind == PyUnicode_4BYTE_KIND); |
| 350 | assert(ascii->state.compact == 0); |
| 351 | assert(ascii->state.ready == 1); |
| 352 | assert(data != NULL); |
| 353 | if (ascii->state.ascii) { |
| 354 | assert (compact->utf8 == data); |
| 355 | assert (compact->utf8_length == ascii->length); |
| 356 | } |
| 357 | else |
| 358 | assert (compact->utf8 != data); |
| 359 | } |
| 360 | } |
| 361 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 362 | if ( |
| 363 | #if SIZEOF_WCHAR_T == 2 |
| 364 | kind == PyUnicode_2BYTE_KIND |
| 365 | #else |
| 366 | kind == PyUnicode_4BYTE_KIND |
| 367 | #endif |
| 368 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 369 | { |
| 370 | assert(ascii->wstr == data); |
| 371 | assert(compact->wstr_length == ascii->length); |
| 372 | } else |
| 373 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 374 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 375 | |
| 376 | if (compact->utf8 == NULL) |
| 377 | assert(compact->utf8_length == 0); |
| 378 | if (ascii->wstr == NULL) |
| 379 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 380 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 381 | /* check that the best kind is used */ |
| 382 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 383 | { |
| 384 | Py_ssize_t i; |
| 385 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 386 | void *data; |
| 387 | Py_UCS4 ch; |
| 388 | |
| 389 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 | for (i=0; i < ascii->length; i++) |
| 391 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 392 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 393 | if (ch > maxchar) |
| 394 | maxchar = ch; |
| 395 | } |
| 396 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 397 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 399 | assert(maxchar <= 255); |
| 400 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | else |
| 402 | assert(maxchar < 128); |
| 403 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 404 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 405 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 406 | assert(maxchar <= 0xFFFF); |
| 407 | } |
| 408 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 409 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 410 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 411 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 412 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 413 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 414 | return 1; |
| 415 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 416 | #endif |
| 417 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 418 | static PyObject* |
| 419 | unicode_result_wchar(PyObject *unicode) |
| 420 | { |
| 421 | #ifndef Py_DEBUG |
| 422 | Py_ssize_t len; |
| 423 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 424 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 425 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 426 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 427 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (len == 1) { |
| 431 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 432 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 433 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 434 | Py_DECREF(unicode); |
| 435 | return latin1_char; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 440 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 444 | assert(Py_REFCNT(unicode) == 1); |
| 445 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 446 | /* don't make the result ready in debug mode to ensure that the caller |
| 447 | makes the string ready before using it */ |
| 448 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 449 | #endif |
| 450 | return unicode; |
| 451 | } |
| 452 | |
| 453 | static PyObject* |
| 454 | unicode_result_ready(PyObject *unicode) |
| 455 | { |
| 456 | Py_ssize_t length; |
| 457 | |
| 458 | length = PyUnicode_GET_LENGTH(unicode); |
| 459 | if (length == 0) { |
| 460 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 461 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 462 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 463 | } |
| 464 | return unicode_empty; |
| 465 | } |
| 466 | |
| 467 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 468 | void *data = PyUnicode_DATA(unicode); |
| 469 | int kind = PyUnicode_KIND(unicode); |
| 470 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 471 | if (ch < 256) { |
| 472 | PyObject *latin1_char = unicode_latin1[ch]; |
| 473 | if (latin1_char != NULL) { |
| 474 | if (unicode != latin1_char) { |
| 475 | Py_INCREF(latin1_char); |
| 476 | Py_DECREF(unicode); |
| 477 | } |
| 478 | return latin1_char; |
| 479 | } |
| 480 | else { |
| 481 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 482 | Py_INCREF(unicode); |
| 483 | unicode_latin1[ch] = unicode; |
| 484 | return unicode; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 490 | return unicode; |
| 491 | } |
| 492 | |
| 493 | static PyObject* |
| 494 | unicode_result(PyObject *unicode) |
| 495 | { |
| 496 | assert(_PyUnicode_CHECK(unicode)); |
| 497 | if (PyUnicode_IS_READY(unicode)) |
| 498 | return unicode_result_ready(unicode); |
| 499 | else |
| 500 | return unicode_result_wchar(unicode); |
| 501 | } |
| 502 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 503 | static PyObject* |
| 504 | unicode_result_unchanged(PyObject *unicode) |
| 505 | { |
| 506 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 507 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 508 | return NULL; |
| 509 | Py_INCREF(unicode); |
| 510 | return unicode; |
| 511 | } |
| 512 | else |
| 513 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 514 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 517 | #ifdef HAVE_MBCS |
| 518 | static OSVERSIONINFOEX winver; |
| 519 | #endif |
| 520 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 522 | |
| 523 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 524 | to keep things simple, we use a single bitmask, using the least 5 |
| 525 | bits from each unicode characters as the bit index. */ |
| 526 | |
| 527 | /* the linebreak mask is set up by Unicode_Init below */ |
| 528 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 529 | #if LONG_BIT >= 128 |
| 530 | #define BLOOM_WIDTH 128 |
| 531 | #elif LONG_BIT >= 64 |
| 532 | #define BLOOM_WIDTH 64 |
| 533 | #elif LONG_BIT >= 32 |
| 534 | #define BLOOM_WIDTH 32 |
| 535 | #else |
| 536 | #error "LONG_BIT is smaller than 32" |
| 537 | #endif |
| 538 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | #define BLOOM_MASK unsigned long |
| 540 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 541 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 543 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 545 | #define BLOOM_LINEBREAK(ch) \ |
| 546 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 547 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 549 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 550 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 552 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 553 | do { \ |
| 554 | TYPE *data = (TYPE *)PTR; \ |
| 555 | TYPE *end = data + LEN; \ |
| 556 | Py_UCS4 ch; \ |
| 557 | for (; data != end; data++) { \ |
| 558 | ch = *data; \ |
| 559 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 560 | } \ |
| 561 | break; \ |
| 562 | } while (0) |
| 563 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 564 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 565 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 566 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | |
| 568 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 569 | switch (kind) { |
| 570 | case PyUnicode_1BYTE_KIND: |
| 571 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 572 | break; |
| 573 | case PyUnicode_2BYTE_KIND: |
| 574 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 575 | break; |
| 576 | case PyUnicode_4BYTE_KIND: |
| 577 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 578 | break; |
| 579 | default: |
| 580 | assert(0); |
| 581 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 582 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 583 | |
| 584 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 587 | /* Compilation of templated routines */ |
| 588 | |
| 589 | #include "stringlib/asciilib.h" |
| 590 | #include "stringlib/fastsearch.h" |
| 591 | #include "stringlib/partition.h" |
| 592 | #include "stringlib/split.h" |
| 593 | #include "stringlib/count.h" |
| 594 | #include "stringlib/find.h" |
| 595 | #include "stringlib/find_max_char.h" |
| 596 | #include "stringlib/localeutil.h" |
| 597 | #include "stringlib/undef.h" |
| 598 | |
| 599 | #include "stringlib/ucs1lib.h" |
| 600 | #include "stringlib/fastsearch.h" |
| 601 | #include "stringlib/partition.h" |
| 602 | #include "stringlib/split.h" |
| 603 | #include "stringlib/count.h" |
| 604 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 605 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 606 | #include "stringlib/find_max_char.h" |
| 607 | #include "stringlib/localeutil.h" |
| 608 | #include "stringlib/undef.h" |
| 609 | |
| 610 | #include "stringlib/ucs2lib.h" |
| 611 | #include "stringlib/fastsearch.h" |
| 612 | #include "stringlib/partition.h" |
| 613 | #include "stringlib/split.h" |
| 614 | #include "stringlib/count.h" |
| 615 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 616 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 617 | #include "stringlib/find_max_char.h" |
| 618 | #include "stringlib/localeutil.h" |
| 619 | #include "stringlib/undef.h" |
| 620 | |
| 621 | #include "stringlib/ucs4lib.h" |
| 622 | #include "stringlib/fastsearch.h" |
| 623 | #include "stringlib/partition.h" |
| 624 | #include "stringlib/split.h" |
| 625 | #include "stringlib/count.h" |
| 626 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 627 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 628 | #include "stringlib/find_max_char.h" |
| 629 | #include "stringlib/localeutil.h" |
| 630 | #include "stringlib/undef.h" |
| 631 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 632 | #include "stringlib/unicodedefs.h" |
| 633 | #include "stringlib/fastsearch.h" |
| 634 | #include "stringlib/count.h" |
| 635 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 636 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 638 | /* --- Unicode Object ----------------------------------------------------- */ |
| 639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 641 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 642 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 643 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 644 | Py_ssize_t size, Py_UCS4 ch, |
| 645 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 647 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 648 | |
| 649 | switch (kind) { |
| 650 | case PyUnicode_1BYTE_KIND: |
| 651 | { |
| 652 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 653 | if (ch1 == ch) |
| 654 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 655 | else |
| 656 | return -1; |
| 657 | } |
| 658 | case PyUnicode_2BYTE_KIND: |
| 659 | { |
| 660 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 661 | if (ch2 == ch) |
| 662 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 663 | else |
| 664 | return -1; |
| 665 | } |
| 666 | case PyUnicode_4BYTE_KIND: |
| 667 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 668 | default: |
| 669 | assert(0); |
| 670 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 671 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 674 | #ifdef Py_DEBUG |
| 675 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 676 | earlier. |
| 677 | |
| 678 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 679 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 680 | invalid character in Unicode 6.0. */ |
| 681 | static void |
| 682 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 683 | { |
| 684 | int kind = PyUnicode_KIND(unicode); |
| 685 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 686 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 687 | if (length <= old_length) |
| 688 | return; |
| 689 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 690 | } |
| 691 | #endif |
| 692 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 693 | static PyObject* |
| 694 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 695 | { |
| 696 | Py_ssize_t char_size; |
| 697 | Py_ssize_t struct_size; |
| 698 | Py_ssize_t new_size; |
| 699 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 700 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 701 | #ifdef Py_DEBUG |
| 702 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 703 | #endif |
| 704 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 705 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 706 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 707 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 708 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 709 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 710 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 711 | struct_size = sizeof(PyASCIIObject); |
| 712 | else |
| 713 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 714 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 715 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 717 | PyErr_NoMemory(); |
| 718 | return NULL; |
| 719 | } |
| 720 | new_size = (struct_size + (length + 1) * char_size); |
| 721 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 722 | _Py_DEC_REFTOTAL; |
| 723 | _Py_ForgetReference(unicode); |
| 724 | |
| 725 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 726 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 727 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 728 | PyErr_NoMemory(); |
| 729 | return NULL; |
| 730 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 731 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 733 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 734 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 735 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 737 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 738 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 739 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 740 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 741 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 742 | _PyUnicode_WSTR(unicode) = NULL; |
| 743 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 744 | #ifdef Py_DEBUG |
| 745 | unicode_fill_invalid(unicode, old_length); |
| 746 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 747 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 748 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 749 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 750 | return unicode; |
| 751 | } |
| 752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 753 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 754 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 755 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 756 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 757 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 758 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 759 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 760 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 761 | if (PyUnicode_IS_READY(unicode)) { |
| 762 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 763 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 764 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 765 | #ifdef Py_DEBUG |
| 766 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 767 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 | |
| 769 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 770 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 771 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 772 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 773 | |
| 774 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 775 | PyErr_NoMemory(); |
| 776 | return -1; |
| 777 | } |
| 778 | new_size = (length + 1) * char_size; |
| 779 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 780 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 781 | { |
| 782 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 783 | _PyUnicode_UTF8(unicode) = NULL; |
| 784 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 785 | } |
| 786 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 787 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 788 | if (data == NULL) { |
| 789 | PyErr_NoMemory(); |
| 790 | return -1; |
| 791 | } |
| 792 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 793 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 794 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 795 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 796 | } |
| 797 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 798 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 799 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 800 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 801 | _PyUnicode_LENGTH(unicode) = length; |
| 802 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 803 | #ifdef Py_DEBUG |
| 804 | unicode_fill_invalid(unicode, old_length); |
| 805 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 806 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 807 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 808 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 809 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 810 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 811 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 812 | |
| 813 | /* check for integer overflow */ |
| 814 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 815 | PyErr_NoMemory(); |
| 816 | return -1; |
| 817 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 818 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 819 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 820 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 821 | if (!wstr) { |
| 822 | PyErr_NoMemory(); |
| 823 | return -1; |
| 824 | } |
| 825 | _PyUnicode_WSTR(unicode) = wstr; |
| 826 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 827 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 828 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 829 | return 0; |
| 830 | } |
| 831 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 832 | static PyObject* |
| 833 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 834 | { |
| 835 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 836 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 837 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 838 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 839 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 840 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 841 | |
| 842 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 843 | if (copy == NULL) |
| 844 | return NULL; |
| 845 | |
| 846 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 847 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 848 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 849 | } |
| 850 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 851 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 852 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 853 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 854 | if (w == NULL) |
| 855 | return NULL; |
| 856 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 857 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 858 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 859 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 860 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 864 | /* 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] | 865 | Ux0000 terminated; some code (e.g. new_identifier) |
| 866 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 867 | |
| 868 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 869 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 870 | |
| 871 | */ |
| 872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 873 | static PyUnicodeObject * |
| 874 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 875 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 876 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 877 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 878 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 879 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 880 | if (length == 0 && unicode_empty != NULL) { |
| 881 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 882 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 885 | /* Ensure we won't overflow the size. */ |
| 886 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 887 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 888 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 889 | if (length < 0) { |
| 890 | PyErr_SetString(PyExc_SystemError, |
| 891 | "Negative size passed to _PyUnicode_New"); |
| 892 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 896 | if (unicode == NULL) |
| 897 | return NULL; |
| 898 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 899 | |
| 900 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 901 | _PyUnicode_HASH(unicode) = -1; |
| 902 | _PyUnicode_STATE(unicode).interned = 0; |
| 903 | _PyUnicode_STATE(unicode).kind = 0; |
| 904 | _PyUnicode_STATE(unicode).compact = 0; |
| 905 | _PyUnicode_STATE(unicode).ready = 0; |
| 906 | _PyUnicode_STATE(unicode).ascii = 0; |
| 907 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 908 | _PyUnicode_LENGTH(unicode) = 0; |
| 909 | _PyUnicode_UTF8(unicode) = NULL; |
| 910 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 913 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 914 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 915 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 916 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 917 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 919 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 920 | * the caller fails before initializing str -- unicode_resize() |
| 921 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 922 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 923 | * We don't want unicode_resize to read uninitialized memory in |
| 924 | * that case. |
| 925 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 926 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 927 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 928 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 929 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 930 | return unicode; |
| 931 | } |
| 932 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 933 | static const char* |
| 934 | unicode_kind_name(PyObject *unicode) |
| 935 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 936 | /* don't check consistency: unicode_kind_name() is called from |
| 937 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 938 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 939 | { |
| 940 | if (!PyUnicode_IS_READY(unicode)) |
| 941 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 942 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | { |
| 944 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 945 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 946 | return "legacy ascii"; |
| 947 | else |
| 948 | return "legacy latin1"; |
| 949 | case PyUnicode_2BYTE_KIND: |
| 950 | return "legacy UCS2"; |
| 951 | case PyUnicode_4BYTE_KIND: |
| 952 | return "legacy UCS4"; |
| 953 | default: |
| 954 | return "<legacy invalid kind>"; |
| 955 | } |
| 956 | } |
| 957 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 958 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 959 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 960 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 961 | return "ascii"; |
| 962 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 963 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 964 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 965 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 966 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 967 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 968 | default: |
| 969 | return "<invalid compact kind>"; |
| 970 | } |
| 971 | } |
| 972 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 973 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | /* Functions wrapping macros for use in debugger */ |
| 975 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 976 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void *_PyUnicode_compact_data(void *unicode) { |
| 980 | return _PyUnicode_COMPACT_DATA(unicode); |
| 981 | } |
| 982 | void *_PyUnicode_data(void *unicode){ |
| 983 | printf("obj %p\n", unicode); |
| 984 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 985 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 986 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 987 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 988 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 989 | return PyUnicode_DATA(unicode); |
| 990 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 991 | |
| 992 | void |
| 993 | _PyUnicode_Dump(PyObject *op) |
| 994 | { |
| 995 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 996 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 997 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 998 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 999 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1000 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1001 | { |
| 1002 | if (ascii->state.ascii) |
| 1003 | data = (ascii + 1); |
| 1004 | else |
| 1005 | data = (compact + 1); |
| 1006 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1007 | else |
| 1008 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1009 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1010 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1011 | if (ascii->wstr == data) |
| 1012 | printf("shared "); |
| 1013 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1014 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1015 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1016 | printf(" (%zu), ", compact->wstr_length); |
| 1017 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1018 | printf("shared "); |
| 1019 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1020 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1021 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1023 | #endif |
| 1024 | |
| 1025 | PyObject * |
| 1026 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1027 | { |
| 1028 | PyObject *obj; |
| 1029 | PyCompactUnicodeObject *unicode; |
| 1030 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1031 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1032 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1033 | Py_ssize_t char_size; |
| 1034 | Py_ssize_t struct_size; |
| 1035 | |
| 1036 | /* Optimization for empty strings */ |
| 1037 | if (size == 0 && unicode_empty != NULL) { |
| 1038 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1039 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1042 | is_ascii = 0; |
| 1043 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1045 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1046 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | char_size = 1; |
| 1048 | is_ascii = 1; |
| 1049 | struct_size = sizeof(PyASCIIObject); |
| 1050 | } |
| 1051 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1052 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | char_size = 1; |
| 1054 | } |
| 1055 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1056 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | char_size = 2; |
| 1058 | if (sizeof(wchar_t) == 2) |
| 1059 | is_sharing = 1; |
| 1060 | } |
| 1061 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1062 | if (maxchar > MAX_UNICODE) { |
| 1063 | PyErr_SetString(PyExc_SystemError, |
| 1064 | "invalid maximum character passed to PyUnicode_New"); |
| 1065 | return NULL; |
| 1066 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1067 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | char_size = 4; |
| 1069 | if (sizeof(wchar_t) == 4) |
| 1070 | is_sharing = 1; |
| 1071 | } |
| 1072 | |
| 1073 | /* Ensure we won't overflow the size. */ |
| 1074 | if (size < 0) { |
| 1075 | PyErr_SetString(PyExc_SystemError, |
| 1076 | "Negative size passed to PyUnicode_New"); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1080 | return PyErr_NoMemory(); |
| 1081 | |
| 1082 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1083 | * PyObject_New() so we are able to allocate space for the object and |
| 1084 | * it's data buffer. |
| 1085 | */ |
| 1086 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1087 | if (obj == NULL) |
| 1088 | return PyErr_NoMemory(); |
| 1089 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1090 | if (obj == NULL) |
| 1091 | return NULL; |
| 1092 | |
| 1093 | unicode = (PyCompactUnicodeObject *)obj; |
| 1094 | if (is_ascii) |
| 1095 | data = ((PyASCIIObject*)obj) + 1; |
| 1096 | else |
| 1097 | data = unicode + 1; |
| 1098 | _PyUnicode_LENGTH(unicode) = size; |
| 1099 | _PyUnicode_HASH(unicode) = -1; |
| 1100 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1101 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1102 | _PyUnicode_STATE(unicode).compact = 1; |
| 1103 | _PyUnicode_STATE(unicode).ready = 1; |
| 1104 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1105 | if (is_ascii) { |
| 1106 | ((char*)data)[size] = 0; |
| 1107 | _PyUnicode_WSTR(unicode) = NULL; |
| 1108 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1109 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | ((char*)data)[size] = 0; |
| 1111 | _PyUnicode_WSTR(unicode) = NULL; |
| 1112 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1114 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1115 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | else { |
| 1117 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1118 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1119 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1121 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1122 | ((Py_UCS4*)data)[size] = 0; |
| 1123 | if (is_sharing) { |
| 1124 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1125 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1126 | } |
| 1127 | else { |
| 1128 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1129 | _PyUnicode_WSTR(unicode) = NULL; |
| 1130 | } |
| 1131 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1132 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1133 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1134 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1135 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | return obj; |
| 1137 | } |
| 1138 | |
| 1139 | #if SIZEOF_WCHAR_T == 2 |
| 1140 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1141 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1142 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | |
| 1144 | This function assumes that unicode can hold one more code point than wstr |
| 1145 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1146 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | 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] | 1148 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | { |
| 1150 | const wchar_t *iter; |
| 1151 | Py_UCS4 *ucs4_out; |
| 1152 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1153 | assert(unicode != NULL); |
| 1154 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1156 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1157 | |
| 1158 | for (iter = begin; iter < end; ) { |
| 1159 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1160 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1161 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1162 | && (iter+1) < end |
| 1163 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1164 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1165 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1166 | iter += 2; |
| 1167 | } |
| 1168 | else { |
| 1169 | *ucs4_out++ = *iter; |
| 1170 | iter++; |
| 1171 | } |
| 1172 | } |
| 1173 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1174 | _PyUnicode_GET_LENGTH(unicode))); |
| 1175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1176 | } |
| 1177 | #endif |
| 1178 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1179 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1180 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1181 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1182 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1183 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1184 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1185 | return -1; |
| 1186 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1190 | static int |
| 1191 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1192 | PyObject *from, Py_ssize_t from_start, |
| 1193 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1195 | unsigned int from_kind, to_kind; |
| 1196 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1197 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1198 | assert(0 <= how_many); |
| 1199 | assert(0 <= from_start); |
| 1200 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1201 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1202 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1203 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1205 | assert(PyUnicode_Check(to)); |
| 1206 | assert(PyUnicode_IS_READY(to)); |
| 1207 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1208 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1209 | if (how_many == 0) |
| 1210 | return 0; |
| 1211 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1212 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1213 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1214 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1215 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1217 | #ifdef Py_DEBUG |
| 1218 | if (!check_maxchar |
| 1219 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1220 | { |
| 1221 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1222 | Py_UCS4 ch; |
| 1223 | Py_ssize_t i; |
| 1224 | for (i=0; i < how_many; i++) { |
| 1225 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1226 | assert(ch <= to_maxchar); |
| 1227 | } |
| 1228 | } |
| 1229 | #endif |
| 1230 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1231 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1232 | if (check_maxchar |
| 1233 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1234 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1235 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1236 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1237 | Py_UCS4 max_char; |
| 1238 | max_char = ucs1lib_find_max_char(from_data, |
| 1239 | (Py_UCS1*)from_data + how_many); |
| 1240 | if (max_char >= 128) |
| 1241 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1242 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1243 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1244 | (char*)from_data + from_kind * from_start, |
| 1245 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1246 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1247 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1248 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1249 | { |
| 1250 | _PyUnicode_CONVERT_BYTES( |
| 1251 | Py_UCS1, Py_UCS2, |
| 1252 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1253 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1254 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1255 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1256 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1257 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1258 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1259 | { |
| 1260 | _PyUnicode_CONVERT_BYTES( |
| 1261 | Py_UCS1, Py_UCS4, |
| 1262 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1263 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1264 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1265 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1266 | } |
| 1267 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1268 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1269 | { |
| 1270 | _PyUnicode_CONVERT_BYTES( |
| 1271 | Py_UCS2, Py_UCS4, |
| 1272 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1273 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1274 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1275 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1276 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1277 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1278 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1279 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1280 | if (!check_maxchar) { |
| 1281 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1282 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1283 | { |
| 1284 | _PyUnicode_CONVERT_BYTES( |
| 1285 | Py_UCS2, Py_UCS1, |
| 1286 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1287 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1288 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1289 | ); |
| 1290 | } |
| 1291 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1292 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1293 | { |
| 1294 | _PyUnicode_CONVERT_BYTES( |
| 1295 | Py_UCS4, Py_UCS1, |
| 1296 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1297 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1298 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1299 | ); |
| 1300 | } |
| 1301 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1302 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1303 | { |
| 1304 | _PyUnicode_CONVERT_BYTES( |
| 1305 | Py_UCS4, Py_UCS2, |
| 1306 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1307 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1308 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1309 | ); |
| 1310 | } |
| 1311 | else { |
| 1312 | assert(0); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1316 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1317 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1318 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1319 | Py_ssize_t i; |
| 1320 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1321 | for (i=0; i < how_many; i++) { |
| 1322 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1323 | if (ch > to_maxchar) |
| 1324 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1325 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1326 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1327 | } |
| 1328 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1332 | void |
| 1333 | _PyUnicode_FastCopyCharacters( |
| 1334 | PyObject *to, Py_ssize_t to_start, |
| 1335 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1336 | { |
| 1337 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1338 | } |
| 1339 | |
| 1340 | Py_ssize_t |
| 1341 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1342 | PyObject *from, Py_ssize_t from_start, |
| 1343 | Py_ssize_t how_many) |
| 1344 | { |
| 1345 | int err; |
| 1346 | |
| 1347 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1348 | PyErr_BadInternalCall(); |
| 1349 | return -1; |
| 1350 | } |
| 1351 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1352 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1353 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1354 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1355 | return -1; |
| 1356 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1357 | if (from_start < 0) { |
| 1358 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1359 | return -1; |
| 1360 | } |
| 1361 | if (to_start < 0) { |
| 1362 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1363 | return -1; |
| 1364 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1365 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1366 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1367 | PyErr_Format(PyExc_SystemError, |
| 1368 | "Cannot write %zi characters at %zi " |
| 1369 | "in a string of %zi characters", |
| 1370 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1371 | return -1; |
| 1372 | } |
| 1373 | |
| 1374 | if (how_many == 0) |
| 1375 | return 0; |
| 1376 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1377 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1378 | return -1; |
| 1379 | |
| 1380 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1381 | if (err) { |
| 1382 | PyErr_Format(PyExc_SystemError, |
| 1383 | "Cannot copy %s characters " |
| 1384 | "into a string of %s characters", |
| 1385 | unicode_kind_name(from), |
| 1386 | unicode_kind_name(to)); |
| 1387 | return -1; |
| 1388 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1389 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1392 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1393 | correct string length can be computed before converting a string to UCS4. |
| 1394 | This function counts single surrogates as a character and not as a pair. |
| 1395 | |
| 1396 | Return 0 on success, or -1 on error. */ |
| 1397 | static int |
| 1398 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1399 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | { |
| 1401 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1402 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1403 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1404 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | *num_surrogates = 0; |
| 1406 | *maxchar = 0; |
| 1407 | |
| 1408 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1410 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1411 | && (iter+1) < end |
| 1412 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1413 | { |
| 1414 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1415 | ++(*num_surrogates); |
| 1416 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | } |
| 1418 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1419 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1420 | { |
| 1421 | ch = *iter; |
| 1422 | iter++; |
| 1423 | } |
| 1424 | if (ch > *maxchar) { |
| 1425 | *maxchar = ch; |
| 1426 | if (*maxchar > MAX_UNICODE) { |
| 1427 | PyErr_Format(PyExc_ValueError, |
| 1428 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1429 | ch); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1433 | } |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1437 | int |
| 1438 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | { |
| 1440 | wchar_t *end; |
| 1441 | Py_UCS4 maxchar = 0; |
| 1442 | Py_ssize_t num_surrogates; |
| 1443 | #if SIZEOF_WCHAR_T == 2 |
| 1444 | Py_ssize_t length_wo_surrogates; |
| 1445 | #endif |
| 1446 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1447 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1448 | strings were created using _PyObject_New() and where no canonical |
| 1449 | representation (the str field) has been set yet aka strings |
| 1450 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1451 | assert(_PyUnicode_CHECK(unicode)); |
| 1452 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1453 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1454 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1455 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1456 | /* Actually, it should neither be interned nor be anything else: */ |
| 1457 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1460 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1461 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | |
| 1464 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1465 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1466 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | PyErr_NoMemory(); |
| 1468 | return -1; |
| 1469 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1470 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | _PyUnicode_WSTR(unicode), end, |
| 1472 | PyUnicode_1BYTE_DATA(unicode)); |
| 1473 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1474 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1475 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1476 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1477 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1478 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1479 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | } |
| 1481 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1482 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1483 | _PyUnicode_UTF8(unicode) = NULL; |
| 1484 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | } |
| 1486 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1487 | _PyUnicode_WSTR(unicode) = NULL; |
| 1488 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1489 | } |
| 1490 | /* In this case we might have to convert down from 4-byte native |
| 1491 | wchar_t to 2-byte unicode. */ |
| 1492 | else if (maxchar < 65536) { |
| 1493 | assert(num_surrogates == 0 && |
| 1494 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1495 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1496 | #if SIZEOF_WCHAR_T == 2 |
| 1497 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1498 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1499 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1500 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1501 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1502 | _PyUnicode_UTF8(unicode) = NULL; |
| 1503 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1504 | #else |
| 1505 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1506 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1507 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1508 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1509 | PyErr_NoMemory(); |
| 1510 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1511 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1512 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1513 | _PyUnicode_WSTR(unicode), end, |
| 1514 | PyUnicode_2BYTE_DATA(unicode)); |
| 1515 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1516 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1517 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1518 | _PyUnicode_UTF8(unicode) = NULL; |
| 1519 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1520 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1521 | _PyUnicode_WSTR(unicode) = NULL; |
| 1522 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1523 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | } |
| 1525 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1526 | else { |
| 1527 | #if SIZEOF_WCHAR_T == 2 |
| 1528 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1529 | new normalized 4-byte version. */ |
| 1530 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1531 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1532 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1533 | PyErr_NoMemory(); |
| 1534 | return -1; |
| 1535 | } |
| 1536 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1537 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1538 | _PyUnicode_UTF8(unicode) = NULL; |
| 1539 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1540 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1541 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1542 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1544 | _PyUnicode_WSTR(unicode) = NULL; |
| 1545 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1546 | #else |
| 1547 | assert(num_surrogates == 0); |
| 1548 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1549 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1550 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1551 | _PyUnicode_UTF8(unicode) = NULL; |
| 1552 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1554 | #endif |
| 1555 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1556 | } |
| 1557 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1558 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1562 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1563 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1564 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1565 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1566 | case SSTATE_NOT_INTERNED: |
| 1567 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1569 | case SSTATE_INTERNED_MORTAL: |
| 1570 | /* revive dead object temporarily for DelItem */ |
| 1571 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1572 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1573 | Py_FatalError( |
| 1574 | "deletion of interned string failed"); |
| 1575 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1576 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | case SSTATE_INTERNED_IMMORTAL: |
| 1578 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1579 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1580 | default: |
| 1581 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1584 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1585 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1586 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1587 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1588 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1589 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1590 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1591 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1594 | #ifdef Py_DEBUG |
| 1595 | static int |
| 1596 | unicode_is_singleton(PyObject *unicode) |
| 1597 | { |
| 1598 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1599 | if (unicode == unicode_empty) |
| 1600 | return 1; |
| 1601 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1602 | { |
| 1603 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1604 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1605 | return 1; |
| 1606 | } |
| 1607 | return 0; |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1611 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1612 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1613 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1614 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | if (Py_REFCNT(unicode) != 1) |
| 1616 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1617 | if (_PyUnicode_HASH(unicode) != -1) |
| 1618 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1619 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1620 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1621 | if (!PyUnicode_CheckExact(unicode)) |
| 1622 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1623 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1624 | /* singleton refcount is greater than 1 */ |
| 1625 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1626 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1627 | return 1; |
| 1628 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1629 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1630 | static int |
| 1631 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1632 | { |
| 1633 | PyObject *unicode; |
| 1634 | Py_ssize_t old_length; |
| 1635 | |
| 1636 | assert(p_unicode != NULL); |
| 1637 | unicode = *p_unicode; |
| 1638 | |
| 1639 | assert(unicode != NULL); |
| 1640 | assert(PyUnicode_Check(unicode)); |
| 1641 | assert(0 <= length); |
| 1642 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1643 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1644 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1645 | else |
| 1646 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1647 | if (old_length == length) |
| 1648 | return 0; |
| 1649 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1650 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1651 | _Py_INCREF_UNICODE_EMPTY(); |
| 1652 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1653 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1654 | Py_DECREF(*p_unicode); |
| 1655 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1659 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1660 | PyObject *copy = resize_copy(unicode, length); |
| 1661 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1662 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1663 | Py_DECREF(*p_unicode); |
| 1664 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1665 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1668 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1669 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1670 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1671 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1672 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1673 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1674 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1675 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1678 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1679 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1680 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1681 | PyObject *unicode; |
| 1682 | if (p_unicode == NULL) { |
| 1683 | PyErr_BadInternalCall(); |
| 1684 | return -1; |
| 1685 | } |
| 1686 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1687 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1688 | { |
| 1689 | PyErr_BadInternalCall(); |
| 1690 | return -1; |
| 1691 | } |
| 1692 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1693 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1694 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1695 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1696 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1697 | WARNING: The function doesn't copy the terminating null character and |
| 1698 | doesn't check the maximum character (may write a latin1 character in an |
| 1699 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1700 | static void |
| 1701 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1702 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1703 | { |
| 1704 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1705 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1706 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 | |
| 1708 | switch (kind) { |
| 1709 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1710 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1711 | #ifdef Py_DEBUG |
| 1712 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1713 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1714 | (const Py_UCS1*)str, |
| 1715 | (const Py_UCS1*)str + len); |
| 1716 | assert(maxchar < 128); |
| 1717 | } |
| 1718 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1719 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1720 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1721 | } |
| 1722 | case PyUnicode_2BYTE_KIND: { |
| 1723 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1724 | Py_UCS2 *ucs2 = start; |
| 1725 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1726 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1727 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1728 | *ucs2 = (Py_UCS2)*str; |
| 1729 | |
| 1730 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1731 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1732 | } |
| 1733 | default: { |
| 1734 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1735 | Py_UCS4 *ucs4 = start; |
| 1736 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1737 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1738 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1739 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1740 | *ucs4 = (Py_UCS4)*str; |
| 1741 | |
| 1742 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | static PyObject* |
| 1749 | get_latin1_char(unsigned char ch) |
| 1750 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1751 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1753 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | if (!unicode) |
| 1755 | return NULL; |
| 1756 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1757 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | unicode_latin1[ch] = unicode; |
| 1759 | } |
| 1760 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1761 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1764 | PyObject * |
| 1765 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1766 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1767 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | Py_UCS4 maxchar = 0; |
| 1769 | Py_ssize_t num_surrogates; |
| 1770 | |
| 1771 | if (u == NULL) |
| 1772 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1774 | /* If the Unicode data is known at construction time, we can apply |
| 1775 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1777 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1778 | if (size == 0) |
| 1779 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | /* Single character Unicode objects in the Latin-1 range are |
| 1782 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1783 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1784 | return get_latin1_char((unsigned char)*u); |
| 1785 | |
| 1786 | /* If not empty and not single character, copy the Unicode data |
| 1787 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1788 | if (find_maxchar_surrogates(u, u + size, |
| 1789 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1790 | return NULL; |
| 1791 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1792 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | if (!unicode) |
| 1794 | return NULL; |
| 1795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | switch (PyUnicode_KIND(unicode)) { |
| 1797 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1798 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1799 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1800 | break; |
| 1801 | case PyUnicode_2BYTE_KIND: |
| 1802 | #if Py_UNICODE_SIZE == 2 |
| 1803 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1804 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1805 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1806 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1807 | #endif |
| 1808 | break; |
| 1809 | case PyUnicode_4BYTE_KIND: |
| 1810 | #if SIZEOF_WCHAR_T == 2 |
| 1811 | /* This is the only case which has to process surrogates, thus |
| 1812 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1813 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1814 | #else |
| 1815 | assert(num_surrogates == 0); |
| 1816 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1817 | #endif |
| 1818 | break; |
| 1819 | default: |
| 1820 | assert(0 && "Impossible state"); |
| 1821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1822 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1823 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1826 | PyObject * |
| 1827 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1828 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1829 | if (size < 0) { |
| 1830 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1831 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1832 | return NULL; |
| 1833 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1834 | if (u != NULL) |
| 1835 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1836 | else |
| 1837 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1840 | PyObject * |
| 1841 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1842 | { |
| 1843 | size_t size = strlen(u); |
| 1844 | if (size > PY_SSIZE_T_MAX) { |
| 1845 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1846 | return NULL; |
| 1847 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1848 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1851 | PyObject * |
| 1852 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1853 | { |
| 1854 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1855 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1856 | strlen(id->string), |
| 1857 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1858 | if (!id->object) |
| 1859 | return NULL; |
| 1860 | PyUnicode_InternInPlace(&id->object); |
| 1861 | assert(!id->next); |
| 1862 | id->next = static_strings; |
| 1863 | static_strings = id; |
| 1864 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1865 | return id->object; |
| 1866 | } |
| 1867 | |
| 1868 | void |
| 1869 | _PyUnicode_ClearStaticStrings() |
| 1870 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1871 | _Py_Identifier *tmp, *s = static_strings; |
| 1872 | while (s) { |
| 1873 | Py_DECREF(s->object); |
| 1874 | s->object = NULL; |
| 1875 | tmp = s->next; |
| 1876 | s->next = NULL; |
| 1877 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1878 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1879 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1882 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1883 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1884 | PyObject* |
| 1885 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1886 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1887 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1888 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1889 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1890 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1891 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1892 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1893 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1894 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1895 | unicode = PyUnicode_New(size, 127); |
| 1896 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1897 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1898 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1899 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1900 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1903 | static Py_UCS4 |
| 1904 | kind_maxchar_limit(unsigned int kind) |
| 1905 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1906 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1907 | case PyUnicode_1BYTE_KIND: |
| 1908 | return 0x80; |
| 1909 | case PyUnicode_2BYTE_KIND: |
| 1910 | return 0x100; |
| 1911 | case PyUnicode_4BYTE_KIND: |
| 1912 | return 0x10000; |
| 1913 | default: |
| 1914 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1915 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1919 | Py_LOCAL_INLINE(Py_UCS4) |
| 1920 | align_maxchar(Py_UCS4 maxchar) |
| 1921 | { |
| 1922 | if (maxchar <= 127) |
| 1923 | return 127; |
| 1924 | else if (maxchar <= 255) |
| 1925 | return 255; |
| 1926 | else if (maxchar <= 65535) |
| 1927 | return 65535; |
| 1928 | else |
| 1929 | return MAX_UNICODE; |
| 1930 | } |
| 1931 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1932 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1933 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1934 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1935 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1936 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1937 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1938 | if (size == 0) |
| 1939 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1940 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1941 | if (size == 1) |
| 1942 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1943 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1944 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1945 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | if (!res) |
| 1947 | return NULL; |
| 1948 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1949 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1953 | static PyObject* |
| 1954 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1955 | { |
| 1956 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1957 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1958 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1959 | if (size == 0) |
| 1960 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1961 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1962 | if (size == 1) { |
| 1963 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1964 | int kind; |
| 1965 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1966 | if (ch < 256) |
| 1967 | return get_latin1_char((unsigned char)ch); |
| 1968 | |
| 1969 | res = PyUnicode_New(1, ch); |
| 1970 | if (res == NULL) |
| 1971 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1972 | kind = PyUnicode_KIND(res); |
| 1973 | data = PyUnicode_DATA(res); |
| 1974 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1975 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1976 | return res; |
| 1977 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1978 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1979 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1980 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | if (!res) |
| 1982 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1983 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1985 | else { |
| 1986 | _PyUnicode_CONVERT_BYTES( |
| 1987 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1988 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1989 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | return res; |
| 1991 | } |
| 1992 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1993 | static PyObject* |
| 1994 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | { |
| 1996 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1997 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1998 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1999 | if (size == 0) |
| 2000 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2001 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2002 | if (size == 1) { |
| 2003 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2004 | int kind; |
| 2005 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2006 | if (ch < 256) |
| 2007 | return get_latin1_char((unsigned char)ch); |
| 2008 | |
| 2009 | res = PyUnicode_New(1, ch); |
| 2010 | if (res == NULL) |
| 2011 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2012 | kind = PyUnicode_KIND(res); |
| 2013 | data = PyUnicode_DATA(res); |
| 2014 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2015 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 2016 | return res; |
| 2017 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2018 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2019 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2020 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | if (!res) |
| 2022 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2023 | if (max_char < 256) |
| 2024 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2025 | PyUnicode_1BYTE_DATA(res)); |
| 2026 | else if (max_char < 0x10000) |
| 2027 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2028 | PyUnicode_2BYTE_DATA(res)); |
| 2029 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2030 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2031 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2032 | return res; |
| 2033 | } |
| 2034 | |
| 2035 | PyObject* |
| 2036 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2037 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2038 | if (size < 0) { |
| 2039 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2040 | return NULL; |
| 2041 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2042 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2043 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2044 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2045 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2046 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2048 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2049 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2051 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2055 | Py_UCS4 |
| 2056 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2057 | { |
| 2058 | enum PyUnicode_Kind kind; |
| 2059 | void *startptr, *endptr; |
| 2060 | |
| 2061 | assert(PyUnicode_IS_READY(unicode)); |
| 2062 | assert(0 <= start); |
| 2063 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2064 | assert(start <= end); |
| 2065 | |
| 2066 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2067 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2068 | |
| 2069 | if (start == end) |
| 2070 | return 127; |
| 2071 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2072 | if (PyUnicode_IS_ASCII(unicode)) |
| 2073 | return 127; |
| 2074 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2075 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2076 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2077 | endptr = (char *)startptr + end * kind; |
| 2078 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2079 | switch(kind) { |
| 2080 | case PyUnicode_1BYTE_KIND: |
| 2081 | return ucs1lib_find_max_char(startptr, endptr); |
| 2082 | case PyUnicode_2BYTE_KIND: |
| 2083 | return ucs2lib_find_max_char(startptr, endptr); |
| 2084 | case PyUnicode_4BYTE_KIND: |
| 2085 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2086 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2087 | assert(0); |
| 2088 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2092 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2093 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2094 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2095 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2096 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2097 | { |
| 2098 | PyObject *unicode, *copy; |
| 2099 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2100 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2101 | unsigned int kind; |
| 2102 | |
| 2103 | assert(p_unicode != NULL); |
| 2104 | unicode = *p_unicode; |
| 2105 | assert(PyUnicode_IS_READY(unicode)); |
| 2106 | if (PyUnicode_IS_ASCII(unicode)) |
| 2107 | return; |
| 2108 | |
| 2109 | len = PyUnicode_GET_LENGTH(unicode); |
| 2110 | kind = PyUnicode_KIND(unicode); |
| 2111 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2112 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2113 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2114 | if (max_char >= 128) |
| 2115 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2116 | } |
| 2117 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2118 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2119 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2120 | if (max_char >= 256) |
| 2121 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2122 | } |
| 2123 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2124 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2125 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2126 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2127 | if (max_char >= 0x10000) |
| 2128 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2129 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2130 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2131 | if (copy != NULL) |
| 2132 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2133 | Py_DECREF(unicode); |
| 2134 | *p_unicode = copy; |
| 2135 | } |
| 2136 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2137 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2138 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2139 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2140 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2141 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2142 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2143 | if (!PyUnicode_Check(unicode)) { |
| 2144 | PyErr_BadInternalCall(); |
| 2145 | return NULL; |
| 2146 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2147 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2148 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2149 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2150 | length = PyUnicode_GET_LENGTH(unicode); |
| 2151 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2152 | if (!copy) |
| 2153 | return NULL; |
| 2154 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2155 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2156 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2157 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2158 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2159 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2163 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2164 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2165 | |
| 2166 | void* |
| 2167 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2168 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2169 | Py_ssize_t len; |
| 2170 | void *result; |
| 2171 | unsigned int skind; |
| 2172 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2173 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2174 | return NULL; |
| 2175 | |
| 2176 | len = PyUnicode_GET_LENGTH(s); |
| 2177 | skind = PyUnicode_KIND(s); |
| 2178 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | return NULL; |
| 2181 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2182 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2183 | case PyUnicode_2BYTE_KIND: |
| 2184 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2185 | if (!result) |
| 2186 | return PyErr_NoMemory(); |
| 2187 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2188 | _PyUnicode_CONVERT_BYTES( |
| 2189 | Py_UCS1, Py_UCS2, |
| 2190 | PyUnicode_1BYTE_DATA(s), |
| 2191 | PyUnicode_1BYTE_DATA(s) + len, |
| 2192 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2194 | case PyUnicode_4BYTE_KIND: |
| 2195 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2196 | if (!result) |
| 2197 | return PyErr_NoMemory(); |
| 2198 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2199 | _PyUnicode_CONVERT_BYTES( |
| 2200 | Py_UCS2, Py_UCS4, |
| 2201 | PyUnicode_2BYTE_DATA(s), |
| 2202 | PyUnicode_2BYTE_DATA(s) + len, |
| 2203 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2204 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2205 | else { |
| 2206 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2207 | _PyUnicode_CONVERT_BYTES( |
| 2208 | Py_UCS1, Py_UCS4, |
| 2209 | PyUnicode_1BYTE_DATA(s), |
| 2210 | PyUnicode_1BYTE_DATA(s) + len, |
| 2211 | result); |
| 2212 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2213 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2214 | default: |
| 2215 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2217 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | static Py_UCS4* |
| 2222 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2223 | int copy_null) |
| 2224 | { |
| 2225 | int kind; |
| 2226 | void *data; |
| 2227 | Py_ssize_t len, targetlen; |
| 2228 | if (PyUnicode_READY(string) == -1) |
| 2229 | return NULL; |
| 2230 | kind = PyUnicode_KIND(string); |
| 2231 | data = PyUnicode_DATA(string); |
| 2232 | len = PyUnicode_GET_LENGTH(string); |
| 2233 | targetlen = len; |
| 2234 | if (copy_null) |
| 2235 | targetlen++; |
| 2236 | if (!target) { |
| 2237 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2238 | PyErr_NoMemory(); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2242 | if (!target) { |
| 2243 | PyErr_NoMemory(); |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | } |
| 2247 | else { |
| 2248 | if (targetsize < targetlen) { |
| 2249 | PyErr_Format(PyExc_SystemError, |
| 2250 | "string is longer than the buffer"); |
| 2251 | if (copy_null && 0 < targetsize) |
| 2252 | target[0] = 0; |
| 2253 | return NULL; |
| 2254 | } |
| 2255 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2256 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2257 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2258 | _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] | 2259 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2261 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2262 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2263 | } |
| 2264 | else { |
| 2265 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2266 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2267 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2268 | if (copy_null) |
| 2269 | target[len] = 0; |
| 2270 | return target; |
| 2271 | } |
| 2272 | |
| 2273 | Py_UCS4* |
| 2274 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2275 | int copy_null) |
| 2276 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2277 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2278 | PyErr_BadInternalCall(); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | return as_ucs4(string, target, targetsize, copy_null); |
| 2282 | } |
| 2283 | |
| 2284 | Py_UCS4* |
| 2285 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2286 | { |
| 2287 | return as_ucs4(string, NULL, 0, 1); |
| 2288 | } |
| 2289 | |
| 2290 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2292 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2293 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2294 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2296 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2297 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2298 | PyErr_BadInternalCall(); |
| 2299 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2302 | if (size == -1) { |
| 2303 | size = wcslen(w); |
| 2304 | } |
| 2305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2309 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2310 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2311 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2312 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2313 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2314 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2315 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2316 | if (longflag) |
| 2317 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2318 | else if (longlongflag) { |
| 2319 | /* longlongflag should only ever be nonzero on machines with |
| 2320 | HAVE_LONG_LONG defined */ |
| 2321 | #ifdef HAVE_LONG_LONG |
| 2322 | char *f = PY_FORMAT_LONG_LONG; |
| 2323 | while (*f) |
| 2324 | *fmt++ = *f++; |
| 2325 | #else |
| 2326 | /* we shouldn't ever get here */ |
| 2327 | assert(0); |
| 2328 | *fmt++ = 'l'; |
| 2329 | #endif |
| 2330 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2331 | else if (size_tflag) { |
| 2332 | char *f = PY_FORMAT_SIZE_T; |
| 2333 | while (*f) |
| 2334 | *fmt++ = *f++; |
| 2335 | } |
| 2336 | *fmt++ = c; |
| 2337 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2340 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2341 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2342 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2343 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2344 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2345 | static int |
| 2346 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2347 | Py_ssize_t width, Py_ssize_t precision) |
| 2348 | { |
| 2349 | Py_ssize_t length, fill, arglen; |
| 2350 | Py_UCS4 maxchar; |
| 2351 | |
| 2352 | if (PyUnicode_READY(str) == -1) |
| 2353 | return -1; |
| 2354 | |
| 2355 | length = PyUnicode_GET_LENGTH(str); |
| 2356 | if ((precision == -1 || precision >= length) |
| 2357 | && width <= length) |
| 2358 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2359 | |
| 2360 | if (precision != -1) |
| 2361 | length = Py_MIN(precision, length); |
| 2362 | |
| 2363 | arglen = Py_MAX(length, width); |
| 2364 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2365 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2366 | else |
| 2367 | maxchar = writer->maxchar; |
| 2368 | |
| 2369 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2370 | return -1; |
| 2371 | |
| 2372 | if (width > length) { |
| 2373 | fill = width - length; |
| 2374 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2375 | return -1; |
| 2376 | writer->pos += fill; |
| 2377 | } |
| 2378 | |
| 2379 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2380 | str, 0, length); |
| 2381 | writer->pos += length; |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
| 2385 | static int |
| 2386 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2387 | Py_ssize_t width, Py_ssize_t precision) |
| 2388 | { |
| 2389 | /* UTF-8 */ |
| 2390 | Py_ssize_t length; |
| 2391 | PyObject *unicode; |
| 2392 | int res; |
| 2393 | |
| 2394 | length = strlen(str); |
| 2395 | if (precision != -1) |
| 2396 | length = Py_MIN(length, precision); |
| 2397 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2398 | if (unicode == NULL) |
| 2399 | return -1; |
| 2400 | |
| 2401 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2402 | Py_DECREF(unicode); |
| 2403 | return res; |
| 2404 | } |
| 2405 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2406 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2407 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2408 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2409 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2410 | const char *p; |
| 2411 | Py_ssize_t len; |
| 2412 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2413 | Py_ssize_t width; |
| 2414 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2415 | int longflag; |
| 2416 | int longlongflag; |
| 2417 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2418 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2419 | |
| 2420 | p = f; |
| 2421 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2422 | zeropad = 0; |
| 2423 | if (*f == '0') { |
| 2424 | zeropad = 1; |
| 2425 | f++; |
| 2426 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2427 | |
| 2428 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2429 | width = -1; |
| 2430 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2431 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2432 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2433 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2434 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2435 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2436 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2437 | return NULL; |
| 2438 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2439 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2440 | f++; |
| 2441 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2442 | } |
| 2443 | precision = -1; |
| 2444 | if (*f == '.') { |
| 2445 | f++; |
| 2446 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2447 | precision = (*f - '0'); |
| 2448 | f++; |
| 2449 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2450 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2451 | PyErr_SetString(PyExc_ValueError, |
| 2452 | "precision too big"); |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | precision = (precision * 10) + (*f - '0'); |
| 2456 | f++; |
| 2457 | } |
| 2458 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2459 | if (*f == '%') { |
| 2460 | /* "%.3%s" => f points to "3" */ |
| 2461 | f--; |
| 2462 | } |
| 2463 | } |
| 2464 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2465 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2466 | f--; |
| 2467 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2468 | |
| 2469 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2470 | longflag = 0; |
| 2471 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2472 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2473 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2474 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2475 | longflag = 1; |
| 2476 | ++f; |
| 2477 | } |
| 2478 | #ifdef HAVE_LONG_LONG |
| 2479 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2480 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2481 | longlongflag = 1; |
| 2482 | f += 2; |
| 2483 | } |
| 2484 | #endif |
| 2485 | } |
| 2486 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2487 | 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] | 2488 | size_tflag = 1; |
| 2489 | ++f; |
| 2490 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (f[1] == '\0') |
| 2493 | writer->overallocate = 0; |
| 2494 | |
| 2495 | switch (*f) { |
| 2496 | case 'c': |
| 2497 | { |
| 2498 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2499 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2500 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2501 | "character argument not in range(0x110000)"); |
| 2502 | return NULL; |
| 2503 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2504 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2505 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | case 'i': |
| 2510 | case 'd': |
| 2511 | case 'u': |
| 2512 | case 'x': |
| 2513 | { |
| 2514 | /* used by sprintf */ |
| 2515 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2516 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2517 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2518 | |
| 2519 | if (*f == 'u') { |
| 2520 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2521 | |
| 2522 | if (longflag) |
| 2523 | len = sprintf(buffer, fmt, |
| 2524 | va_arg(*vargs, unsigned long)); |
| 2525 | #ifdef HAVE_LONG_LONG |
| 2526 | else if (longlongflag) |
| 2527 | len = sprintf(buffer, fmt, |
| 2528 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2529 | #endif |
| 2530 | else if (size_tflag) |
| 2531 | len = sprintf(buffer, fmt, |
| 2532 | va_arg(*vargs, size_t)); |
| 2533 | else |
| 2534 | len = sprintf(buffer, fmt, |
| 2535 | va_arg(*vargs, unsigned int)); |
| 2536 | } |
| 2537 | else if (*f == 'x') { |
| 2538 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2539 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2540 | } |
| 2541 | else { |
| 2542 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2543 | |
| 2544 | if (longflag) |
| 2545 | len = sprintf(buffer, fmt, |
| 2546 | va_arg(*vargs, long)); |
| 2547 | #ifdef HAVE_LONG_LONG |
| 2548 | else if (longlongflag) |
| 2549 | len = sprintf(buffer, fmt, |
| 2550 | va_arg(*vargs, PY_LONG_LONG)); |
| 2551 | #endif |
| 2552 | else if (size_tflag) |
| 2553 | len = sprintf(buffer, fmt, |
| 2554 | va_arg(*vargs, Py_ssize_t)); |
| 2555 | else |
| 2556 | len = sprintf(buffer, fmt, |
| 2557 | va_arg(*vargs, int)); |
| 2558 | } |
| 2559 | assert(len >= 0); |
| 2560 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2561 | if (precision < len) |
| 2562 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2563 | |
| 2564 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2565 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2566 | return NULL; |
| 2567 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2568 | if (width > precision) { |
| 2569 | Py_UCS4 fillchar; |
| 2570 | fill = width - precision; |
| 2571 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2572 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2573 | return NULL; |
| 2574 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2575 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2576 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2577 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2578 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2579 | return NULL; |
| 2580 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2581 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2582 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2583 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2584 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2585 | break; |
| 2586 | } |
| 2587 | |
| 2588 | case 'p': |
| 2589 | { |
| 2590 | char number[MAX_LONG_LONG_CHARS]; |
| 2591 | |
| 2592 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2593 | assert(len >= 0); |
| 2594 | |
| 2595 | /* %p is ill-defined: ensure leading 0x. */ |
| 2596 | if (number[1] == 'X') |
| 2597 | number[1] = 'x'; |
| 2598 | else if (number[1] != 'x') { |
| 2599 | memmove(number + 2, number, |
| 2600 | strlen(number) + 1); |
| 2601 | number[0] = '0'; |
| 2602 | number[1] = 'x'; |
| 2603 | len += 2; |
| 2604 | } |
| 2605 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2606 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2607 | return NULL; |
| 2608 | break; |
| 2609 | } |
| 2610 | |
| 2611 | case 's': |
| 2612 | { |
| 2613 | /* UTF-8 */ |
| 2614 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2615 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2616 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2617 | break; |
| 2618 | } |
| 2619 | |
| 2620 | case 'U': |
| 2621 | { |
| 2622 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2623 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2624 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2625 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2626 | return NULL; |
| 2627 | break; |
| 2628 | } |
| 2629 | |
| 2630 | case 'V': |
| 2631 | { |
| 2632 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2633 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2634 | if (obj) { |
| 2635 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2636 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2637 | return NULL; |
| 2638 | } |
| 2639 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2640 | assert(str != NULL); |
| 2641 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2642 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2643 | } |
| 2644 | break; |
| 2645 | } |
| 2646 | |
| 2647 | case 'S': |
| 2648 | { |
| 2649 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2650 | PyObject *str; |
| 2651 | assert(obj); |
| 2652 | str = PyObject_Str(obj); |
| 2653 | if (!str) |
| 2654 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2655 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2656 | Py_DECREF(str); |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | Py_DECREF(str); |
| 2660 | break; |
| 2661 | } |
| 2662 | |
| 2663 | case 'R': |
| 2664 | { |
| 2665 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2666 | PyObject *repr; |
| 2667 | assert(obj); |
| 2668 | repr = PyObject_Repr(obj); |
| 2669 | if (!repr) |
| 2670 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2671 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2672 | Py_DECREF(repr); |
| 2673 | return NULL; |
| 2674 | } |
| 2675 | Py_DECREF(repr); |
| 2676 | break; |
| 2677 | } |
| 2678 | |
| 2679 | case 'A': |
| 2680 | { |
| 2681 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2682 | PyObject *ascii; |
| 2683 | assert(obj); |
| 2684 | ascii = PyObject_ASCII(obj); |
| 2685 | if (!ascii) |
| 2686 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2687 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2688 | Py_DECREF(ascii); |
| 2689 | return NULL; |
| 2690 | } |
| 2691 | Py_DECREF(ascii); |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2696 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2697 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2698 | break; |
| 2699 | |
| 2700 | default: |
| 2701 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2702 | of the format string to the output string. (we cannot just |
| 2703 | skip the code, since there's no way to know what's in the |
| 2704 | argument list) */ |
| 2705 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2706 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2707 | return NULL; |
| 2708 | f = p+len; |
| 2709 | return f; |
| 2710 | } |
| 2711 | |
| 2712 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2713 | return f; |
| 2714 | } |
| 2715 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | PyObject * |
| 2717 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2718 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2719 | va_list vargs2; |
| 2720 | const char *f; |
| 2721 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2722 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2723 | _PyUnicodeWriter_Init(&writer); |
| 2724 | writer.min_length = strlen(format) + 100; |
| 2725 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2728 | Copy it to be able to pass a reference to a subfunction. */ |
| 2729 | Py_VA_COPY(vargs2, vargs); |
| 2730 | |
| 2731 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2732 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2733 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2734 | if (f == NULL) |
| 2735 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2736 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2737 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2738 | const char *p; |
| 2739 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2740 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2741 | p = f; |
| 2742 | do |
| 2743 | { |
| 2744 | if ((unsigned char)*p > 127) { |
| 2745 | PyErr_Format(PyExc_ValueError, |
| 2746 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2747 | "string, got a non-ASCII byte: 0x%02x", |
| 2748 | (unsigned char)*p); |
| 2749 | return NULL; |
| 2750 | } |
| 2751 | p++; |
| 2752 | } |
| 2753 | while (*p != '\0' && *p != '%'); |
| 2754 | len = p - f; |
| 2755 | |
| 2756 | if (*p == '\0') |
| 2757 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2758 | |
| 2759 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2760 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2761 | |
| 2762 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2763 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2764 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2765 | return _PyUnicodeWriter_Finish(&writer); |
| 2766 | |
| 2767 | fail: |
| 2768 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2769 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2772 | PyObject * |
| 2773 | PyUnicode_FromFormat(const char *format, ...) |
| 2774 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2775 | PyObject* ret; |
| 2776 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2777 | |
| 2778 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2779 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2780 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2781 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2782 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2784 | va_end(vargs); |
| 2785 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2788 | #ifdef HAVE_WCHAR_H |
| 2789 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2790 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2791 | convert a Unicode object to a wide character string. |
| 2792 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2793 | - 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] | 2794 | character) required to convert the unicode object. Ignore size argument. |
| 2795 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2796 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2797 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2798 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2799 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2800 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2801 | wchar_t *w, |
| 2802 | Py_ssize_t size) |
| 2803 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2804 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2805 | const wchar_t *wstr; |
| 2806 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2807 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2808 | if (wstr == NULL) |
| 2809 | return -1; |
| 2810 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2811 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2812 | if (size > res) |
| 2813 | size = res + 1; |
| 2814 | else |
| 2815 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2816 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2817 | return res; |
| 2818 | } |
| 2819 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2824 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2825 | wchar_t *w, |
| 2826 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2827 | { |
| 2828 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2829 | PyErr_BadInternalCall(); |
| 2830 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2832 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2835 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2836 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2837 | Py_ssize_t *size) |
| 2838 | { |
| 2839 | wchar_t* buffer; |
| 2840 | Py_ssize_t buflen; |
| 2841 | |
| 2842 | if (unicode == NULL) { |
| 2843 | PyErr_BadInternalCall(); |
| 2844 | return NULL; |
| 2845 | } |
| 2846 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2847 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2848 | if (buflen == -1) |
| 2849 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2850 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2851 | PyErr_NoMemory(); |
| 2852 | return NULL; |
| 2853 | } |
| 2854 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2855 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2856 | if (buffer == NULL) { |
| 2857 | PyErr_NoMemory(); |
| 2858 | return NULL; |
| 2859 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2860 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2861 | if (buflen == -1) { |
| 2862 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2863 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2864 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2865 | if (size != NULL) |
| 2866 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2867 | return buffer; |
| 2868 | } |
| 2869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2870 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2872 | PyObject * |
| 2873 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2874 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2875 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2876 | void *data; |
| 2877 | int kind; |
| 2878 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2879 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2880 | PyErr_SetString(PyExc_ValueError, |
| 2881 | "chr() arg not in range(0x110000)"); |
| 2882 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2883 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2884 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2885 | if ((Py_UCS4)ordinal < 256) |
| 2886 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2887 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2888 | v = PyUnicode_New(1, ordinal); |
| 2889 | if (v == NULL) |
| 2890 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2891 | kind = PyUnicode_KIND(v); |
| 2892 | data = PyUnicode_DATA(v); |
| 2893 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2894 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2895 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2898 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2899 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2901 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2902 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2903 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2904 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2905 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2906 | Py_INCREF(obj); |
| 2907 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2908 | } |
| 2909 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2910 | /* For a Unicode subtype that's not a Unicode object, |
| 2911 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2912 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2913 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2914 | PyErr_Format(PyExc_TypeError, |
| 2915 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2916 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2917 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2920 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2921 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2922 | const char *encoding, |
| 2923 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2924 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2925 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2926 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2928 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2929 | PyErr_BadInternalCall(); |
| 2930 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2931 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2932 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2933 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2934 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2935 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2936 | _Py_RETURN_UNICODE_EMPTY(); |
| 2937 | v = PyUnicode_Decode( |
| 2938 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2939 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2940 | return v; |
| 2941 | } |
| 2942 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2943 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2944 | PyErr_SetString(PyExc_TypeError, |
| 2945 | "decoding str is not supported"); |
| 2946 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2947 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2949 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2950 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2951 | PyErr_Format(PyExc_TypeError, |
| 2952 | "coercing to str: need bytes, bytearray " |
| 2953 | "or buffer-like object, %.80s found", |
| 2954 | Py_TYPE(obj)->tp_name); |
| 2955 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2956 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2957 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2958 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2959 | PyBuffer_Release(&buffer); |
| 2960 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2961 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2962 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2963 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2964 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2965 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2968 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2969 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2970 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2971 | int |
| 2972 | _Py_normalize_encoding(const char *encoding, |
| 2973 | char *lower, |
| 2974 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2975 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2976 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2977 | char *l; |
| 2978 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2979 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2980 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 2981 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 2982 | if (lower_len < 6) |
| 2983 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2984 | strcpy(lower, "utf-8"); |
| 2985 | return 1; |
| 2986 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2987 | e = encoding; |
| 2988 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2989 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2990 | while (*e) { |
| 2991 | if (l == l_end) |
| 2992 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2993 | if (Py_ISUPPER(*e)) { |
| 2994 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2995 | } |
| 2996 | else if (*e == '_') { |
| 2997 | *l++ = '-'; |
| 2998 | e++; |
| 2999 | } |
| 3000 | else { |
| 3001 | *l++ = *e++; |
| 3002 | } |
| 3003 | } |
| 3004 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3005 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3008 | PyObject * |
| 3009 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3010 | Py_ssize_t size, |
| 3011 | const char *encoding, |
| 3012 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3013 | { |
| 3014 | PyObject *buffer = NULL, *unicode; |
| 3015 | Py_buffer info; |
| 3016 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3017 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3018 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3019 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3020 | if ((strcmp(lower, "utf-8") == 0) || |
| 3021 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3022 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3023 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3024 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3025 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3026 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3027 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3028 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3029 | else if (strcmp(lower, "mbcs") == 0) |
| 3030 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3031 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3032 | else if (strcmp(lower, "ascii") == 0) |
| 3033 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3034 | else if (strcmp(lower, "utf-16") == 0) |
| 3035 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3036 | else if (strcmp(lower, "utf-32") == 0) |
| 3037 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | |
| 3040 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3041 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3042 | 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] | 3043 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3044 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | if (buffer == NULL) |
| 3046 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3047 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | if (unicode == NULL) |
| 3049 | goto onError; |
| 3050 | if (!PyUnicode_Check(unicode)) { |
| 3051 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3052 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3053 | "use codecs.decode() to decode to arbitrary types", |
| 3054 | encoding, |
| 3055 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | Py_DECREF(unicode); |
| 3057 | goto onError; |
| 3058 | } |
| 3059 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3060 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3062 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | Py_XDECREF(buffer); |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3067 | PyObject * |
| 3068 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3069 | const char *encoding, |
| 3070 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3071 | { |
| 3072 | PyObject *v; |
| 3073 | |
| 3074 | if (!PyUnicode_Check(unicode)) { |
| 3075 | PyErr_BadArgument(); |
| 3076 | goto onError; |
| 3077 | } |
| 3078 | |
| 3079 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3080 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* Decode via the codec registry */ |
| 3083 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3084 | if (v == NULL) |
| 3085 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3086 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3088 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3092 | PyObject * |
| 3093 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3094 | const char *encoding, |
| 3095 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3096 | { |
| 3097 | PyObject *v; |
| 3098 | |
| 3099 | if (!PyUnicode_Check(unicode)) { |
| 3100 | PyErr_BadArgument(); |
| 3101 | goto onError; |
| 3102 | } |
| 3103 | |
| 3104 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3105 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3106 | |
| 3107 | /* Decode via the codec registry */ |
| 3108 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3109 | if (v == NULL) |
| 3110 | goto onError; |
| 3111 | if (!PyUnicode_Check(v)) { |
| 3112 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3113 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3114 | "use codecs.decode() to decode to arbitrary types", |
| 3115 | encoding, |
| 3116 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3117 | Py_DECREF(v); |
| 3118 | goto onError; |
| 3119 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3120 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3121 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3122 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3123 | return NULL; |
| 3124 | } |
| 3125 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3126 | PyObject * |
| 3127 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3128 | Py_ssize_t size, |
| 3129 | const char *encoding, |
| 3130 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3131 | { |
| 3132 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3134 | unicode = PyUnicode_FromUnicode(s, size); |
| 3135 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3136 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3137 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3138 | Py_DECREF(unicode); |
| 3139 | return v; |
| 3140 | } |
| 3141 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3142 | PyObject * |
| 3143 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3144 | const char *encoding, |
| 3145 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3146 | { |
| 3147 | PyObject *v; |
| 3148 | |
| 3149 | if (!PyUnicode_Check(unicode)) { |
| 3150 | PyErr_BadArgument(); |
| 3151 | goto onError; |
| 3152 | } |
| 3153 | |
| 3154 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3155 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3156 | |
| 3157 | /* Encode via the codec registry */ |
| 3158 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3159 | if (v == NULL) |
| 3160 | goto onError; |
| 3161 | return v; |
| 3162 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3163 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3164 | return NULL; |
| 3165 | } |
| 3166 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3167 | static size_t |
| 3168 | wcstombs_errorpos(const wchar_t *wstr) |
| 3169 | { |
| 3170 | size_t len; |
| 3171 | #if SIZEOF_WCHAR_T == 2 |
| 3172 | wchar_t buf[3]; |
| 3173 | #else |
| 3174 | wchar_t buf[2]; |
| 3175 | #endif |
| 3176 | char outbuf[MB_LEN_MAX]; |
| 3177 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3178 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3179 | #if SIZEOF_WCHAR_T == 2 |
| 3180 | buf[2] = 0; |
| 3181 | #else |
| 3182 | buf[1] = 0; |
| 3183 | #endif |
| 3184 | start = wstr; |
| 3185 | while (*wstr != L'\0') |
| 3186 | { |
| 3187 | previous = wstr; |
| 3188 | #if SIZEOF_WCHAR_T == 2 |
| 3189 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3190 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3191 | { |
| 3192 | buf[0] = wstr[0]; |
| 3193 | buf[1] = wstr[1]; |
| 3194 | wstr += 2; |
| 3195 | } |
| 3196 | else { |
| 3197 | buf[0] = *wstr; |
| 3198 | buf[1] = 0; |
| 3199 | wstr++; |
| 3200 | } |
| 3201 | #else |
| 3202 | buf[0] = *wstr; |
| 3203 | wstr++; |
| 3204 | #endif |
| 3205 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3206 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3207 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3211 | return 0; |
| 3212 | } |
| 3213 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3214 | static int |
| 3215 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3216 | { |
| 3217 | if (errors == NULL) { |
| 3218 | *surrogateescape = 0; |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
| 3222 | if (strcmp(errors, "strict") == 0) { |
| 3223 | *surrogateescape = 0; |
| 3224 | return 0; |
| 3225 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3226 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3227 | *surrogateescape = 1; |
| 3228 | return 0; |
| 3229 | } |
| 3230 | PyErr_Format(PyExc_ValueError, |
| 3231 | "only 'strict' and 'surrogateescape' error handlers " |
| 3232 | "are supported, not '%s'", |
| 3233 | errors); |
| 3234 | return -1; |
| 3235 | } |
| 3236 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3237 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3238 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3239 | { |
| 3240 | Py_ssize_t wlen, wlen2; |
| 3241 | wchar_t *wstr; |
| 3242 | PyObject *bytes = NULL; |
| 3243 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3244 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3245 | PyObject *exc; |
| 3246 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3247 | int surrogateescape; |
| 3248 | |
| 3249 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3250 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3251 | |
| 3252 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3253 | if (wstr == NULL) |
| 3254 | return NULL; |
| 3255 | |
| 3256 | wlen2 = wcslen(wstr); |
| 3257 | if (wlen2 != wlen) { |
| 3258 | PyMem_Free(wstr); |
| 3259 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3260 | return NULL; |
| 3261 | } |
| 3262 | |
| 3263 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3264 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3265 | char *str; |
| 3266 | |
| 3267 | str = _Py_wchar2char(wstr, &error_pos); |
| 3268 | if (str == NULL) { |
| 3269 | if (error_pos == (size_t)-1) { |
| 3270 | PyErr_NoMemory(); |
| 3271 | PyMem_Free(wstr); |
| 3272 | return NULL; |
| 3273 | } |
| 3274 | else { |
| 3275 | goto encode_error; |
| 3276 | } |
| 3277 | } |
| 3278 | PyMem_Free(wstr); |
| 3279 | |
| 3280 | bytes = PyBytes_FromString(str); |
| 3281 | PyMem_Free(str); |
| 3282 | } |
| 3283 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3284 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3285 | size_t len, len2; |
| 3286 | |
| 3287 | len = wcstombs(NULL, wstr, 0); |
| 3288 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3289 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3290 | goto encode_error; |
| 3291 | } |
| 3292 | |
| 3293 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3294 | if (bytes == NULL) { |
| 3295 | PyMem_Free(wstr); |
| 3296 | return NULL; |
| 3297 | } |
| 3298 | |
| 3299 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3300 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3301 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3302 | goto encode_error; |
| 3303 | } |
| 3304 | PyMem_Free(wstr); |
| 3305 | } |
| 3306 | return bytes; |
| 3307 | |
| 3308 | encode_error: |
| 3309 | errmsg = strerror(errno); |
| 3310 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3311 | |
| 3312 | if (error_pos == (size_t)-1) |
| 3313 | error_pos = wcstombs_errorpos(wstr); |
| 3314 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3315 | PyMem_Free(wstr); |
| 3316 | Py_XDECREF(bytes); |
| 3317 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3318 | if (errmsg != NULL) { |
| 3319 | size_t errlen; |
| 3320 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3321 | if (wstr != NULL) { |
| 3322 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3323 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3324 | } else |
| 3325 | errmsg = NULL; |
| 3326 | } |
| 3327 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3328 | reason = PyUnicode_FromString( |
| 3329 | "wcstombs() encountered an unencodable " |
| 3330 | "wide character"); |
| 3331 | if (reason == NULL) |
| 3332 | return NULL; |
| 3333 | |
| 3334 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3335 | "locale", unicode, |
| 3336 | (Py_ssize_t)error_pos, |
| 3337 | (Py_ssize_t)(error_pos+1), |
| 3338 | reason); |
| 3339 | Py_DECREF(reason); |
| 3340 | if (exc != NULL) { |
| 3341 | PyCodec_StrictErrors(exc); |
| 3342 | Py_XDECREF(exc); |
| 3343 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3344 | return NULL; |
| 3345 | } |
| 3346 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3347 | PyObject * |
| 3348 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3349 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3350 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3351 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3352 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3353 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3354 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3355 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3356 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3357 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3358 | the Python codec requires to encode at least its own filename. Use the C |
| 3359 | version of the locale codec until the codec registry is initialized and |
| 3360 | the Python codec is loaded. |
| 3361 | |
| 3362 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3363 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3364 | subinterpreters. */ |
| 3365 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3366 | return PyUnicode_AsEncodedString(unicode, |
| 3367 | Py_FileSystemDefaultEncoding, |
| 3368 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3369 | } |
| 3370 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3371 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3372 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3373 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3376 | PyObject * |
| 3377 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3378 | const char *encoding, |
| 3379 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | { |
| 3381 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3382 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | if (!PyUnicode_Check(unicode)) { |
| 3385 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3387 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3388 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3389 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3390 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3391 | if ((strcmp(lower, "utf-8") == 0) || |
| 3392 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3393 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3394 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3395 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3396 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3397 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3398 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3399 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3400 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3401 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3402 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3403 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3404 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3405 | else if (strcmp(lower, "mbcs") == 0) |
| 3406 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3407 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3408 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3409 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3410 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3411 | |
| 3412 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3413 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3414 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3415 | return NULL; |
| 3416 | |
| 3417 | /* The normal path */ |
| 3418 | if (PyBytes_Check(v)) |
| 3419 | return v; |
| 3420 | |
| 3421 | /* 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] | 3422 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3423 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3424 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3425 | |
| 3426 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3427 | "encoder %s returned bytearray instead of bytes; " |
| 3428 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3429 | encoding); |
| 3430 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3431 | Py_DECREF(v); |
| 3432 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3433 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3434 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3435 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3436 | Py_DECREF(v); |
| 3437 | return b; |
| 3438 | } |
| 3439 | |
| 3440 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3441 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3442 | "use codecs.encode() to encode to arbitrary types", |
| 3443 | encoding, |
| 3444 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3445 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3446 | return NULL; |
| 3447 | } |
| 3448 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3449 | PyObject * |
| 3450 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3451 | const char *encoding, |
| 3452 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3453 | { |
| 3454 | PyObject *v; |
| 3455 | |
| 3456 | if (!PyUnicode_Check(unicode)) { |
| 3457 | PyErr_BadArgument(); |
| 3458 | goto onError; |
| 3459 | } |
| 3460 | |
| 3461 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3462 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3463 | |
| 3464 | /* Encode via the codec registry */ |
| 3465 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3466 | if (v == NULL) |
| 3467 | goto onError; |
| 3468 | if (!PyUnicode_Check(v)) { |
| 3469 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3470 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3471 | "use codecs.encode() to encode to arbitrary types", |
| 3472 | encoding, |
| 3473 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3474 | Py_DECREF(v); |
| 3475 | goto onError; |
| 3476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3477 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3478 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3479 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3480 | return NULL; |
| 3481 | } |
| 3482 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3483 | static size_t |
| 3484 | mbstowcs_errorpos(const char *str, size_t len) |
| 3485 | { |
| 3486 | #ifdef HAVE_MBRTOWC |
| 3487 | const char *start = str; |
| 3488 | mbstate_t mbs; |
| 3489 | size_t converted; |
| 3490 | wchar_t ch; |
| 3491 | |
| 3492 | memset(&mbs, 0, sizeof mbs); |
| 3493 | while (len) |
| 3494 | { |
| 3495 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3496 | if (converted == 0) |
| 3497 | /* Reached end of string */ |
| 3498 | break; |
| 3499 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3500 | /* Conversion error or incomplete character */ |
| 3501 | return str - start; |
| 3502 | } |
| 3503 | else { |
| 3504 | str += converted; |
| 3505 | len -= converted; |
| 3506 | } |
| 3507 | } |
| 3508 | /* failed to find the undecodable byte sequence */ |
| 3509 | return 0; |
| 3510 | #endif |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3514 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3515 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3516 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3517 | { |
| 3518 | wchar_t smallbuf[256]; |
| 3519 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3520 | wchar_t *wstr; |
| 3521 | size_t wlen, wlen2; |
| 3522 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3523 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3524 | size_t error_pos; |
| 3525 | char *errmsg; |
| 3526 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3527 | |
| 3528 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3529 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3530 | |
| 3531 | if (str[len] != '\0' || len != strlen(str)) { |
| 3532 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3533 | return NULL; |
| 3534 | } |
| 3535 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3536 | if (surrogateescape) { |
| 3537 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3538 | wstr = _Py_char2wchar(str, &wlen); |
| 3539 | if (wstr == NULL) { |
| 3540 | if (wlen == (size_t)-1) |
| 3541 | PyErr_NoMemory(); |
| 3542 | else |
| 3543 | PyErr_SetFromErrno(PyExc_OSError); |
| 3544 | return NULL; |
| 3545 | } |
| 3546 | |
| 3547 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3548 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3549 | } |
| 3550 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3551 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3552 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3553 | wlen = mbstowcs(NULL, str, 0); |
| 3554 | #else |
| 3555 | wlen = len; |
| 3556 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3557 | if (wlen == (size_t)-1) |
| 3558 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3559 | if (wlen+1 <= smallbuf_len) { |
| 3560 | wstr = smallbuf; |
| 3561 | } |
| 3562 | else { |
| 3563 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3564 | return PyErr_NoMemory(); |
| 3565 | |
| 3566 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3567 | if (!wstr) |
| 3568 | return PyErr_NoMemory(); |
| 3569 | } |
| 3570 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3571 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3572 | if (wlen2 == (size_t)-1) { |
| 3573 | if (wstr != smallbuf) |
| 3574 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3575 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3576 | } |
| 3577 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3578 | assert(wlen2 == wlen); |
| 3579 | #endif |
| 3580 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3581 | if (wstr != smallbuf) |
| 3582 | PyMem_Free(wstr); |
| 3583 | } |
| 3584 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3585 | |
| 3586 | decode_error: |
| 3587 | errmsg = strerror(errno); |
| 3588 | assert(errmsg != NULL); |
| 3589 | |
| 3590 | error_pos = mbstowcs_errorpos(str, len); |
| 3591 | if (errmsg != NULL) { |
| 3592 | size_t errlen; |
| 3593 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3594 | if (wstr != NULL) { |
| 3595 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3596 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3597 | } else |
| 3598 | errmsg = NULL; |
| 3599 | } |
| 3600 | if (errmsg == NULL) |
| 3601 | reason = PyUnicode_FromString( |
| 3602 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3603 | if (reason == NULL) |
| 3604 | return NULL; |
| 3605 | |
| 3606 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3607 | "locale", str, len, |
| 3608 | (Py_ssize_t)error_pos, |
| 3609 | (Py_ssize_t)(error_pos+1), |
| 3610 | reason); |
| 3611 | Py_DECREF(reason); |
| 3612 | if (exc != NULL) { |
| 3613 | PyCodec_StrictErrors(exc); |
| 3614 | Py_XDECREF(exc); |
| 3615 | } |
| 3616 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3620 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3621 | { |
| 3622 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3623 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | |
| 3627 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3628 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3629 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3630 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3631 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3632 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3633 | PyObject* |
| 3634 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3635 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3636 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3637 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3638 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3639 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3640 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3641 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3642 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3643 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3644 | the Python codec requires to encode at least its own filename. Use the C |
| 3645 | version of the locale codec until the codec registry is initialized and |
| 3646 | the Python codec is loaded. |
| 3647 | |
| 3648 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3649 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3650 | subinterpreters. */ |
| 3651 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3652 | return PyUnicode_Decode(s, size, |
| 3653 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3654 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3655 | } |
| 3656 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3657 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3658 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3659 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3662 | |
| 3663 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3664 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3665 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3666 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3667 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3668 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3669 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3670 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3671 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3672 | if (pos == -1) |
| 3673 | return 0; |
| 3674 | else |
| 3675 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3676 | } |
| 3677 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3678 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3679 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3680 | { |
| 3681 | PyObject *output = NULL; |
| 3682 | Py_ssize_t size; |
| 3683 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3684 | if (arg == NULL) { |
| 3685 | Py_DECREF(*(PyObject**)addr); |
| 3686 | return 1; |
| 3687 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3688 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3689 | output = arg; |
| 3690 | Py_INCREF(output); |
| 3691 | } |
| 3692 | else { |
| 3693 | arg = PyUnicode_FromObject(arg); |
| 3694 | if (!arg) |
| 3695 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3696 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3697 | Py_DECREF(arg); |
| 3698 | if (!output) |
| 3699 | return 0; |
| 3700 | if (!PyBytes_Check(output)) { |
| 3701 | Py_DECREF(output); |
| 3702 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3703 | return 0; |
| 3704 | } |
| 3705 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3706 | size = PyBytes_GET_SIZE(output); |
| 3707 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3708 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3709 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3710 | Py_DECREF(output); |
| 3711 | return 0; |
| 3712 | } |
| 3713 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3714 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
| 3717 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3718 | int |
| 3719 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3720 | { |
| 3721 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3722 | if (arg == NULL) { |
| 3723 | Py_DECREF(*(PyObject**)addr); |
| 3724 | return 1; |
| 3725 | } |
| 3726 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3727 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3728 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3729 | output = arg; |
| 3730 | Py_INCREF(output); |
| 3731 | } |
| 3732 | else { |
| 3733 | arg = PyBytes_FromObject(arg); |
| 3734 | if (!arg) |
| 3735 | return 0; |
| 3736 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3737 | PyBytes_GET_SIZE(arg)); |
| 3738 | Py_DECREF(arg); |
| 3739 | if (!output) |
| 3740 | return 0; |
| 3741 | if (!PyUnicode_Check(output)) { |
| 3742 | Py_DECREF(output); |
| 3743 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3744 | return 0; |
| 3745 | } |
| 3746 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3747 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3748 | Py_DECREF(output); |
| 3749 | return 0; |
| 3750 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3751 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3752 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3753 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3754 | Py_DECREF(output); |
| 3755 | return 0; |
| 3756 | } |
| 3757 | *(PyObject**)addr = output; |
| 3758 | return Py_CLEANUP_SUPPORTED; |
| 3759 | } |
| 3760 | |
| 3761 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3762 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3763 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3764 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3765 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3767 | if (!PyUnicode_Check(unicode)) { |
| 3768 | PyErr_BadArgument(); |
| 3769 | return NULL; |
| 3770 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3771 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3772 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3774 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3775 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3776 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3777 | if (bytes == NULL) |
| 3778 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3779 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3780 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3781 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3782 | Py_DECREF(bytes); |
| 3783 | return NULL; |
| 3784 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3785 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3786 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3787 | PyBytes_AS_STRING(bytes), |
| 3788 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3789 | Py_DECREF(bytes); |
| 3790 | } |
| 3791 | |
| 3792 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3793 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3794 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3798 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3799 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3800 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3801 | } |
| 3802 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3803 | Py_UNICODE * |
| 3804 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3805 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3806 | const unsigned char *one_byte; |
| 3807 | #if SIZEOF_WCHAR_T == 4 |
| 3808 | const Py_UCS2 *two_bytes; |
| 3809 | #else |
| 3810 | const Py_UCS4 *four_bytes; |
| 3811 | const Py_UCS4 *ucs4_end; |
| 3812 | Py_ssize_t num_surrogates; |
| 3813 | #endif |
| 3814 | wchar_t *w; |
| 3815 | wchar_t *wchar_end; |
| 3816 | |
| 3817 | if (!PyUnicode_Check(unicode)) { |
| 3818 | PyErr_BadArgument(); |
| 3819 | return NULL; |
| 3820 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3821 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3823 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3824 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3825 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3826 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3827 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3828 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3829 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3830 | num_surrogates = 0; |
| 3831 | |
| 3832 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3833 | if (*four_bytes > 0xFFFF) |
| 3834 | ++num_surrogates; |
| 3835 | } |
| 3836 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3837 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3838 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3839 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3840 | PyErr_NoMemory(); |
| 3841 | return NULL; |
| 3842 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3843 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3845 | w = _PyUnicode_WSTR(unicode); |
| 3846 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3847 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3848 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3849 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3850 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3851 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3852 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3853 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3854 | } |
| 3855 | else |
| 3856 | *w = *four_bytes; |
| 3857 | |
| 3858 | if (w > wchar_end) { |
| 3859 | assert(0 && "Miscalculated string end"); |
| 3860 | } |
| 3861 | } |
| 3862 | *w = 0; |
| 3863 | #else |
| 3864 | /* sizeof(wchar_t) == 4 */ |
| 3865 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3866 | "should share memory already."); |
| 3867 | return NULL; |
| 3868 | #endif |
| 3869 | } |
| 3870 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3871 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3872 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3873 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | PyErr_NoMemory(); |
| 3875 | return NULL; |
| 3876 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3877 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3878 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3879 | w = _PyUnicode_WSTR(unicode); |
| 3880 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3882 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3883 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3884 | for (; w < wchar_end; ++one_byte, ++w) |
| 3885 | *w = *one_byte; |
| 3886 | /* null-terminate the wstr */ |
| 3887 | *w = 0; |
| 3888 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3889 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3891 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3892 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3893 | *w = *two_bytes; |
| 3894 | /* null-terminate the wstr */ |
| 3895 | *w = 0; |
| 3896 | #else |
| 3897 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3898 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3899 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3900 | Py_FatalError("Impossible unicode object state, wstr " |
| 3901 | "and str should share memory already."); |
| 3902 | return NULL; |
| 3903 | #endif |
| 3904 | } |
| 3905 | else { |
| 3906 | assert(0 && "This should never happen."); |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3911 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3912 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3915 | Py_UNICODE * |
| 3916 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3918 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3922 | Py_ssize_t |
| 3923 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3924 | { |
| 3925 | if (!PyUnicode_Check(unicode)) { |
| 3926 | PyErr_BadArgument(); |
| 3927 | goto onError; |
| 3928 | } |
| 3929 | return PyUnicode_GET_SIZE(unicode); |
| 3930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3931 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3932 | return -1; |
| 3933 | } |
| 3934 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3935 | Py_ssize_t |
| 3936 | PyUnicode_GetLength(PyObject *unicode) |
| 3937 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3938 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3939 | PyErr_BadArgument(); |
| 3940 | return -1; |
| 3941 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3942 | if (PyUnicode_READY(unicode) == -1) |
| 3943 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3944 | return PyUnicode_GET_LENGTH(unicode); |
| 3945 | } |
| 3946 | |
| 3947 | Py_UCS4 |
| 3948 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3949 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3950 | void *data; |
| 3951 | int kind; |
| 3952 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3953 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3954 | PyErr_BadArgument(); |
| 3955 | return (Py_UCS4)-1; |
| 3956 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3957 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3958 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 | return (Py_UCS4)-1; |
| 3960 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3961 | data = PyUnicode_DATA(unicode); |
| 3962 | kind = PyUnicode_KIND(unicode); |
| 3963 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | int |
| 3967 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3968 | { |
| 3969 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3970 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3971 | return -1; |
| 3972 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3973 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3974 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3975 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3976 | return -1; |
| 3977 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3978 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3979 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3980 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3981 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3982 | return -1; |
| 3983 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3984 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3985 | index, ch); |
| 3986 | return 0; |
| 3987 | } |
| 3988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3989 | const char * |
| 3990 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3991 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3992 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3995 | /* create or adjust a UnicodeDecodeError */ |
| 3996 | static void |
| 3997 | make_decode_exception(PyObject **exceptionObject, |
| 3998 | const char *encoding, |
| 3999 | const char *input, Py_ssize_t length, |
| 4000 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4001 | const char *reason) |
| 4002 | { |
| 4003 | if (*exceptionObject == NULL) { |
| 4004 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4005 | encoding, input, length, startpos, endpos, reason); |
| 4006 | } |
| 4007 | else { |
| 4008 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4009 | goto onError; |
| 4010 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4011 | goto onError; |
| 4012 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4013 | goto onError; |
| 4014 | } |
| 4015 | return; |
| 4016 | |
| 4017 | onError: |
| 4018 | Py_DECREF(*exceptionObject); |
| 4019 | *exceptionObject = NULL; |
| 4020 | } |
| 4021 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4022 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4023 | /* error handling callback helper: |
| 4024 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4025 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4026 | and adjust various state variables. |
| 4027 | return 0 on success, -1 on error |
| 4028 | */ |
| 4029 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4030 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4031 | unicode_decode_call_errorhandler_wchar( |
| 4032 | const char *errors, PyObject **errorHandler, |
| 4033 | const char *encoding, const char *reason, |
| 4034 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4035 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4036 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4037 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4038 | 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] | 4039 | |
| 4040 | PyObject *restuple = NULL; |
| 4041 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4042 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4043 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4044 | Py_ssize_t requiredsize; |
| 4045 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4046 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4047 | wchar_t *repwstr; |
| 4048 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4049 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4050 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4051 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4052 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4053 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4054 | *errorHandler = PyCodec_LookupError(errors); |
| 4055 | if (*errorHandler == NULL) |
| 4056 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4059 | make_decode_exception(exceptionObject, |
| 4060 | encoding, |
| 4061 | *input, *inend - *input, |
| 4062 | *startinpos, *endinpos, |
| 4063 | reason); |
| 4064 | if (*exceptionObject == NULL) |
| 4065 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4066 | |
| 4067 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4068 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4069 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4070 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4071 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4072 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | } |
| 4074 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4076 | |
| 4077 | /* Copy back the bytes variables, which might have been modified by the |
| 4078 | callback */ |
| 4079 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4080 | if (!inputobj) |
| 4081 | goto onError; |
| 4082 | if (!PyBytes_Check(inputobj)) { |
| 4083 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4084 | } |
| 4085 | *input = PyBytes_AS_STRING(inputobj); |
| 4086 | insize = PyBytes_GET_SIZE(inputobj); |
| 4087 | *inend = *input + insize; |
| 4088 | /* we can DECREF safely, as the exception has another reference, |
| 4089 | so the object won't go away. */ |
| 4090 | Py_DECREF(inputobj); |
| 4091 | |
| 4092 | if (newpos<0) |
| 4093 | newpos = insize+newpos; |
| 4094 | if (newpos<0 || newpos>insize) { |
| 4095 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4096 | goto onError; |
| 4097 | } |
| 4098 | |
| 4099 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4100 | if (repwstr == NULL) |
| 4101 | goto onError; |
| 4102 | /* need more space? (at least enough for what we |
| 4103 | have+the replacement+the rest of the string (starting |
| 4104 | at the new input position), so we won't have to check space |
| 4105 | when there are no errors in the rest of the string) */ |
| 4106 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4107 | if (requiredsize > outsize) { |
| 4108 | if (requiredsize < 2*outsize) |
| 4109 | requiredsize = 2*outsize; |
| 4110 | if (unicode_resize(output, requiredsize) < 0) |
| 4111 | goto onError; |
| 4112 | } |
| 4113 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4114 | *outpos += repwlen; |
| 4115 | |
| 4116 | *endinpos = newpos; |
| 4117 | *inptr = *input + newpos; |
| 4118 | |
| 4119 | /* we made it! */ |
| 4120 | Py_XDECREF(restuple); |
| 4121 | return 0; |
| 4122 | |
| 4123 | onError: |
| 4124 | Py_XDECREF(restuple); |
| 4125 | return -1; |
| 4126 | } |
| 4127 | #endif /* HAVE_MBCS */ |
| 4128 | |
| 4129 | static int |
| 4130 | unicode_decode_call_errorhandler_writer( |
| 4131 | const char *errors, PyObject **errorHandler, |
| 4132 | const char *encoding, const char *reason, |
| 4133 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4134 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4135 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4136 | { |
| 4137 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4138 | |
| 4139 | PyObject *restuple = NULL; |
| 4140 | PyObject *repunicode = NULL; |
| 4141 | Py_ssize_t insize; |
| 4142 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4143 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4144 | PyObject *inputobj = NULL; |
| 4145 | |
| 4146 | if (*errorHandler == NULL) { |
| 4147 | *errorHandler = PyCodec_LookupError(errors); |
| 4148 | if (*errorHandler == NULL) |
| 4149 | goto onError; |
| 4150 | } |
| 4151 | |
| 4152 | make_decode_exception(exceptionObject, |
| 4153 | encoding, |
| 4154 | *input, *inend - *input, |
| 4155 | *startinpos, *endinpos, |
| 4156 | reason); |
| 4157 | if (*exceptionObject == NULL) |
| 4158 | goto onError; |
| 4159 | |
| 4160 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4161 | if (restuple == NULL) |
| 4162 | goto onError; |
| 4163 | if (!PyTuple_Check(restuple)) { |
| 4164 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4165 | goto onError; |
| 4166 | } |
| 4167 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4168 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4169 | |
| 4170 | /* Copy back the bytes variables, which might have been modified by the |
| 4171 | callback */ |
| 4172 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4173 | if (!inputobj) |
| 4174 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4175 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4177 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4178 | *input = PyBytes_AS_STRING(inputobj); |
| 4179 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4180 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4181 | /* we can DECREF safely, as the exception has another reference, |
| 4182 | so the object won't go away. */ |
| 4183 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4184 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4186 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4187 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4188 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4189 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4190 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4192 | if (PyUnicode_READY(repunicode) < 0) |
| 4193 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4194 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 4195 | writer->min_length += replen; |
| 4196 | if (replen > 1) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4197 | writer->overallocate = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4198 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4199 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4200 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4202 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4203 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4204 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4205 | Py_XDECREF(restuple); |
| 4206 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4208 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4209 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4210 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4213 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4214 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4215 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4216 | |
| 4217 | /* Three simple macros defining base-64. */ |
| 4218 | |
| 4219 | /* Is c a base-64 character? */ |
| 4220 | |
| 4221 | #define IS_BASE64(c) \ |
| 4222 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4223 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4224 | ((c) >= '0' && (c) <= '9') || \ |
| 4225 | (c) == '+' || (c) == '/') |
| 4226 | |
| 4227 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4228 | |
| 4229 | #define FROM_BASE64(c) \ |
| 4230 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4231 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4232 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4233 | (c) == '+' ? 62 : 63) |
| 4234 | |
| 4235 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4236 | |
| 4237 | #define TO_BASE64(n) \ |
| 4238 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4239 | |
| 4240 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4241 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4242 | * byte not decoding to itself is the + which begins a base64 |
| 4243 | * string. */ |
| 4244 | |
| 4245 | #define DECODE_DIRECT(c) \ |
| 4246 | ((c) <= 127 && (c) != '+') |
| 4247 | |
| 4248 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4249 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4250 | * the above). See RFC2152. This array identifies these different |
| 4251 | * sets: |
| 4252 | * 0 : "Set D" |
| 4253 | * alphanumeric and '(),-./:? |
| 4254 | * 1 : "Set O" |
| 4255 | * !"#$%&*;<=>@[]^_`{|} |
| 4256 | * 2 : "whitespace" |
| 4257 | * ht nl cr sp |
| 4258 | * 3 : special (must be base64 encoded) |
| 4259 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4260 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4261 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4262 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4263 | char utf7_category[128] = { |
| 4264 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4265 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4266 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4267 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4268 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4269 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4270 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4271 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4272 | /* @ A B C D E F G H I J K L M N O */ |
| 4273 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4274 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4275 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4276 | /* ` a b c d e f g h i j k l m n o */ |
| 4277 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4278 | /* p q r s t u v w x y z { | } ~ del */ |
| 4279 | 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] | 4280 | }; |
| 4281 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4283 | * answer depends on whether we are encoding set O as itself, and also |
| 4284 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4285 | * clear that the answers to these questions vary between |
| 4286 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4287 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4288 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4289 | ((c) < 128 && (c) > 0 && \ |
| 4290 | ((utf7_category[(c)] == 0) || \ |
| 4291 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4292 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4294 | PyObject * |
| 4295 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4296 | Py_ssize_t size, |
| 4297 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4298 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4299 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4300 | } |
| 4301 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4302 | /* The decoder. The only state we preserve is our read position, |
| 4303 | * i.e. how many characters we have consumed. So if we end in the |
| 4304 | * middle of a shift sequence we have to back off the read position |
| 4305 | * and the output to the beginning of the sequence, otherwise we lose |
| 4306 | * all the shift state (seen bits, number of bits seen, high |
| 4307 | * surrogate). */ |
| 4308 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4309 | PyObject * |
| 4310 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4311 | Py_ssize_t size, |
| 4312 | const char *errors, |
| 4313 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4314 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4315 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4316 | Py_ssize_t startinpos; |
| 4317 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4319 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | const char *errmsg = ""; |
| 4321 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4322 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4323 | unsigned int base64bits = 0; |
| 4324 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4325 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4326 | PyObject *errorHandler = NULL; |
| 4327 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4328 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4329 | if (size == 0) { |
| 4330 | if (consumed) |
| 4331 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4332 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4335 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4336 | _PyUnicodeWriter_Init(&writer); |
| 4337 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4338 | |
| 4339 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4340 | e = s + size; |
| 4341 | |
| 4342 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4343 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4344 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4345 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4346 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4347 | if (inShift) { /* in a base-64 section */ |
| 4348 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4349 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4350 | base64bits += 6; |
| 4351 | s++; |
| 4352 | if (base64bits >= 16) { |
| 4353 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4354 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | base64bits -= 16; |
| 4356 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4357 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4358 | if (surrogate) { |
| 4359 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4360 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4361 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4362 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4363 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4365 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | } |
| 4367 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4368 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4369 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4373 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4374 | /* first surrogate */ |
| 4375 | surrogate = outCh; |
| 4376 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4378 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4379 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4384 | inShift = 0; |
| 4385 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4387 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4388 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4389 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4390 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | if (base64bits > 0) { /* left-over bits */ |
| 4392 | if (base64bits >= 6) { |
| 4393 | /* We've seen at least one base-64 character */ |
| 4394 | errmsg = "partial character in shift sequence"; |
| 4395 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4396 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4397 | else { |
| 4398 | /* Some bits remain; they should be zero */ |
| 4399 | if (base64buffer != 0) { |
| 4400 | errmsg = "non-zero padding bits in shift sequence"; |
| 4401 | goto utf7Error; |
| 4402 | } |
| 4403 | } |
| 4404 | } |
| 4405 | if (ch != '-') { |
| 4406 | /* '-' is absorbed; other terminating |
| 4407 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4408 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4409 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | s++; /* consume '+' */ |
| 4416 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4418 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4419 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4420 | } |
| 4421 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4423 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4424 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4425 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4426 | } |
| 4427 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4428 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4430 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4431 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4432 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | else { |
| 4434 | startinpos = s-starts; |
| 4435 | s++; |
| 4436 | errmsg = "unexpected special character"; |
| 4437 | goto utf7Error; |
| 4438 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4439 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4442 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4443 | errors, &errorHandler, |
| 4444 | "utf7", errmsg, |
| 4445 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4446 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4450 | /* end of string */ |
| 4451 | |
| 4452 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4453 | /* if we're in an inconsistent state, that's an error */ |
| 4454 | if (surrogate || |
| 4455 | (base64bits >= 6) || |
| 4456 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4457 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4458 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4459 | errors, &errorHandler, |
| 4460 | "utf7", "unterminated shift sequence", |
| 4461 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4462 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4463 | goto onError; |
| 4464 | if (s < e) |
| 4465 | goto restart; |
| 4466 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4467 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4468 | |
| 4469 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4470 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4471 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4472 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4473 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4474 | } |
| 4475 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4476 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4477 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4478 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4479 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4480 | Py_XDECREF(errorHandler); |
| 4481 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4482 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4484 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | Py_XDECREF(errorHandler); |
| 4486 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4487 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4488 | return NULL; |
| 4489 | } |
| 4490 | |
| 4491 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4492 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4493 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4494 | int base64SetO, |
| 4495 | int base64WhiteSpace, |
| 4496 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4497 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4498 | int kind; |
| 4499 | void *data; |
| 4500 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4501 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4502 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4503 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4504 | unsigned int base64bits = 0; |
| 4505 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4506 | char * out; |
| 4507 | char * start; |
| 4508 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4509 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4510 | return NULL; |
| 4511 | kind = PyUnicode_KIND(str); |
| 4512 | data = PyUnicode_DATA(str); |
| 4513 | len = PyUnicode_GET_LENGTH(str); |
| 4514 | |
| 4515 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4516 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4517 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4518 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4519 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4520 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4521 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4522 | if (v == NULL) |
| 4523 | return NULL; |
| 4524 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4525 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4526 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4527 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4528 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4529 | if (inShift) { |
| 4530 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4531 | /* shifting out */ |
| 4532 | if (base64bits) { /* output remaining bits */ |
| 4533 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4534 | base64buffer = 0; |
| 4535 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4536 | } |
| 4537 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4538 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4539 | so no '-' is required, except if the character is itself a '-' */ |
| 4540 | if (IS_BASE64(ch) || ch == '-') { |
| 4541 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4542 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | *out++ = (char) ch; |
| 4544 | } |
| 4545 | else { |
| 4546 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4547 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4548 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4549 | else { /* not in a shift sequence */ |
| 4550 | if (ch == '+') { |
| 4551 | *out++ = '+'; |
| 4552 | *out++ = '-'; |
| 4553 | } |
| 4554 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4555 | *out++ = (char) ch; |
| 4556 | } |
| 4557 | else { |
| 4558 | *out++ = '+'; |
| 4559 | inShift = 1; |
| 4560 | goto encode_char; |
| 4561 | } |
| 4562 | } |
| 4563 | continue; |
| 4564 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4565 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4566 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4567 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4568 | /* code first surrogate */ |
| 4569 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4570 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4571 | while (base64bits >= 6) { |
| 4572 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4573 | base64bits -= 6; |
| 4574 | } |
| 4575 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4576 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4577 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4578 | base64bits += 16; |
| 4579 | base64buffer = (base64buffer << 16) | ch; |
| 4580 | while (base64bits >= 6) { |
| 4581 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4582 | base64bits -= 6; |
| 4583 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4584 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4585 | if (base64bits) |
| 4586 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4587 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4588 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4589 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4590 | return NULL; |
| 4591 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4592 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4593 | PyObject * |
| 4594 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4595 | Py_ssize_t size, |
| 4596 | int base64SetO, |
| 4597 | int base64WhiteSpace, |
| 4598 | const char *errors) |
| 4599 | { |
| 4600 | PyObject *result; |
| 4601 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4602 | if (tmp == NULL) |
| 4603 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4604 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4605 | base64WhiteSpace, errors); |
| 4606 | Py_DECREF(tmp); |
| 4607 | return result; |
| 4608 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4609 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4610 | #undef IS_BASE64 |
| 4611 | #undef FROM_BASE64 |
| 4612 | #undef TO_BASE64 |
| 4613 | #undef DECODE_DIRECT |
| 4614 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4617 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4618 | PyObject * |
| 4619 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4620 | Py_ssize_t size, |
| 4621 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4623 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4624 | } |
| 4625 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4626 | #include "stringlib/asciilib.h" |
| 4627 | #include "stringlib/codecs.h" |
| 4628 | #include "stringlib/undef.h" |
| 4629 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4630 | #include "stringlib/ucs1lib.h" |
| 4631 | #include "stringlib/codecs.h" |
| 4632 | #include "stringlib/undef.h" |
| 4633 | |
| 4634 | #include "stringlib/ucs2lib.h" |
| 4635 | #include "stringlib/codecs.h" |
| 4636 | #include "stringlib/undef.h" |
| 4637 | |
| 4638 | #include "stringlib/ucs4lib.h" |
| 4639 | #include "stringlib/codecs.h" |
| 4640 | #include "stringlib/undef.h" |
| 4641 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4642 | /* Mask to quickly check whether a C 'long' contains a |
| 4643 | non-ASCII, UTF8-encoded char. */ |
| 4644 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4645 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4646 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4647 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4648 | #else |
| 4649 | # error C 'long' size should be either 4 or 8! |
| 4650 | #endif |
| 4651 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4652 | static Py_ssize_t |
| 4653 | 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] | 4654 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4655 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4656 | 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] | 4657 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4658 | /* |
| 4659 | * Issue #17237: m68k is a bit different from most architectures in |
| 4660 | * that objects do not use "natural alignment" - for example, int and |
| 4661 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4662 | * won't work; also, tests have shown that skipping the "optimised |
| 4663 | * version" will even speed up m68k. |
| 4664 | */ |
| 4665 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4666 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4667 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4668 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4669 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4670 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4671 | /* Help allocation */ |
| 4672 | const char *_p = p; |
| 4673 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4674 | while (_p < aligned_end) { |
| 4675 | unsigned long value = *(const unsigned long *) _p; |
| 4676 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4677 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | *((unsigned long *)q) = value; |
| 4679 | _p += SIZEOF_LONG; |
| 4680 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4681 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | p = _p; |
| 4683 | while (p < end) { |
| 4684 | if ((unsigned char)*p & 0x80) |
| 4685 | break; |
| 4686 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4687 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4689 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4690 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4691 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4692 | while (p < end) { |
| 4693 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4694 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4695 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4696 | /* Help allocation */ |
| 4697 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4698 | while (_p < aligned_end) { |
| 4699 | unsigned long value = *(unsigned long *) _p; |
| 4700 | if (value & ASCII_CHAR_MASK) |
| 4701 | break; |
| 4702 | _p += SIZEOF_LONG; |
| 4703 | } |
| 4704 | p = _p; |
| 4705 | if (_p == end) |
| 4706 | break; |
| 4707 | } |
| 4708 | if ((unsigned char)*p & 0x80) |
| 4709 | break; |
| 4710 | ++p; |
| 4711 | } |
| 4712 | memcpy(dest, start, p - start); |
| 4713 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4714 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4715 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4716 | PyObject * |
| 4717 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4718 | Py_ssize_t size, |
| 4719 | const char *errors, |
| 4720 | Py_ssize_t *consumed) |
| 4721 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4722 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4723 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4724 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4725 | |
| 4726 | Py_ssize_t startinpos; |
| 4727 | Py_ssize_t endinpos; |
| 4728 | const char *errmsg = ""; |
| 4729 | PyObject *errorHandler = NULL; |
| 4730 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4731 | |
| 4732 | if (size == 0) { |
| 4733 | if (consumed) |
| 4734 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4735 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4736 | } |
| 4737 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4738 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4739 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4740 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4741 | *consumed = 1; |
| 4742 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4743 | } |
| 4744 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4745 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4746 | writer.min_length = size; |
| 4747 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4748 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4749 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4750 | writer.pos = ascii_decode(s, end, writer.data); |
| 4751 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4752 | while (s < end) { |
| 4753 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4756 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4757 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4758 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4759 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4760 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4761 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4762 | } else { |
| 4763 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4764 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | switch (ch) { |
| 4768 | case 0: |
| 4769 | if (s == end || consumed) |
| 4770 | goto End; |
| 4771 | errmsg = "unexpected end of data"; |
| 4772 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4773 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4774 | break; |
| 4775 | case 1: |
| 4776 | errmsg = "invalid start byte"; |
| 4777 | startinpos = s - starts; |
| 4778 | endinpos = startinpos + 1; |
| 4779 | break; |
| 4780 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4781 | case 3: |
| 4782 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4783 | errmsg = "invalid continuation byte"; |
| 4784 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4785 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4786 | break; |
| 4787 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4788 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4789 | goto onError; |
| 4790 | continue; |
| 4791 | } |
| 4792 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4793 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4794 | errors, &errorHandler, |
| 4795 | "utf-8", errmsg, |
| 4796 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4797 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4799 | } |
| 4800 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4801 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4802 | if (consumed) |
| 4803 | *consumed = s - starts; |
| 4804 | |
| 4805 | Py_XDECREF(errorHandler); |
| 4806 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4807 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4808 | |
| 4809 | onError: |
| 4810 | Py_XDECREF(errorHandler); |
| 4811 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4812 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4813 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4816 | #ifdef __APPLE__ |
| 4817 | |
| 4818 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4819 | used to decode the command line arguments on Mac OS X. |
| 4820 | |
| 4821 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4822 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4823 | |
| 4824 | wchar_t* |
| 4825 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4826 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4827 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4828 | wchar_t *unicode; |
| 4829 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4830 | |
| 4831 | /* Note: size will always be longer than the resulting Unicode |
| 4832 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4833 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4835 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | if (!unicode) |
| 4837 | return NULL; |
| 4838 | |
| 4839 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4840 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4841 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4842 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4843 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4844 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4845 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4846 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4847 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4848 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4849 | if (ch > 0xFF) { |
| 4850 | #if SIZEOF_WCHAR_T == 4 |
| 4851 | assert(0); |
| 4852 | #else |
| 4853 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4854 | /* compute and append the two surrogates: */ |
| 4855 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4856 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4857 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4858 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4859 | else { |
| 4860 | if (!ch && s == e) |
| 4861 | break; |
| 4862 | /* surrogateescape */ |
| 4863 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4864 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4865 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4866 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4867 | return unicode; |
| 4868 | } |
| 4869 | |
| 4870 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4872 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4873 | |
| 4874 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4875 | and allocate exactly as much space needed at the end. Else allocate the |
| 4876 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4877 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4878 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4879 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4880 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4881 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4882 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4883 | void *data; |
| 4884 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4885 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4886 | if (!PyUnicode_Check(unicode)) { |
| 4887 | PyErr_BadArgument(); |
| 4888 | return NULL; |
| 4889 | } |
| 4890 | |
| 4891 | if (PyUnicode_READY(unicode) == -1) |
| 4892 | return NULL; |
| 4893 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4894 | if (PyUnicode_UTF8(unicode)) |
| 4895 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4896 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4897 | |
| 4898 | kind = PyUnicode_KIND(unicode); |
| 4899 | data = PyUnicode_DATA(unicode); |
| 4900 | size = PyUnicode_GET_LENGTH(unicode); |
| 4901 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4902 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4903 | default: |
| 4904 | assert(0); |
| 4905 | case PyUnicode_1BYTE_KIND: |
| 4906 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4907 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4908 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4909 | case PyUnicode_2BYTE_KIND: |
| 4910 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4911 | case PyUnicode_4BYTE_KIND: |
| 4912 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4913 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4916 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4917 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4918 | Py_ssize_t size, |
| 4919 | const char *errors) |
| 4920 | { |
| 4921 | PyObject *v, *unicode; |
| 4922 | |
| 4923 | unicode = PyUnicode_FromUnicode(s, size); |
| 4924 | if (unicode == NULL) |
| 4925 | return NULL; |
| 4926 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4927 | Py_DECREF(unicode); |
| 4928 | return v; |
| 4929 | } |
| 4930 | |
| 4931 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4932 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4934 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4938 | |
| 4939 | PyObject * |
| 4940 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4941 | Py_ssize_t size, |
| 4942 | const char *errors, |
| 4943 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4944 | { |
| 4945 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4946 | } |
| 4947 | |
| 4948 | PyObject * |
| 4949 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4950 | Py_ssize_t size, |
| 4951 | const char *errors, |
| 4952 | int *byteorder, |
| 4953 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | { |
| 4955 | const char *starts = s; |
| 4956 | Py_ssize_t startinpos; |
| 4957 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4958 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4959 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4960 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4961 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4962 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 | PyObject *errorHandler = NULL; |
| 4964 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4965 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4966 | q = (unsigned char *)s; |
| 4967 | e = q + size; |
| 4968 | |
| 4969 | if (byteorder) |
| 4970 | bo = *byteorder; |
| 4971 | |
| 4972 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4973 | byte order setting accordingly. In native mode, the leading BOM |
| 4974 | mark is skipped, in all other modes, it is copied to the output |
| 4975 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4976 | if (bo == 0 && size >= 4) { |
| 4977 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4978 | if (bom == 0x0000FEFF) { |
| 4979 | bo = -1; |
| 4980 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4981 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4982 | else if (bom == 0xFFFE0000) { |
| 4983 | bo = 1; |
| 4984 | q += 4; |
| 4985 | } |
| 4986 | if (byteorder) |
| 4987 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4990 | if (q == e) { |
| 4991 | if (consumed) |
| 4992 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4993 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4996 | #ifdef WORDS_BIGENDIAN |
| 4997 | le = bo < 0; |
| 4998 | #else |
| 4999 | le = bo <= 0; |
| 5000 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5001 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5002 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5003 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5004 | writer.min_length = (e - q + 3) / 4; |
| 5005 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5006 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5007 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5008 | while (1) { |
| 5009 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5010 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5011 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5012 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5013 | enum PyUnicode_Kind kind = writer.kind; |
| 5014 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5015 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5016 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5017 | if (le) { |
| 5018 | do { |
| 5019 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5020 | if (ch > maxch) |
| 5021 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5022 | if (kind != PyUnicode_1BYTE_KIND && |
| 5023 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5024 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5025 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5026 | q += 4; |
| 5027 | } while (q <= last); |
| 5028 | } |
| 5029 | else { |
| 5030 | do { |
| 5031 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5032 | if (ch > maxch) |
| 5033 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5034 | if (kind != PyUnicode_1BYTE_KIND && |
| 5035 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5036 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5037 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5038 | q += 4; |
| 5039 | } while (q <= last); |
| 5040 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5041 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5042 | } |
| 5043 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5044 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
| 5045 | errmsg = "codepoint in surrogate code point range(0xd800, 0xe000)"; |
| 5046 | startinpos = ((const char *)q) - starts; |
| 5047 | endinpos = startinpos + 4; |
| 5048 | } |
| 5049 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5050 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5051 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5052 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5054 | startinpos = ((const char *)q) - starts; |
| 5055 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5057 | else { |
| 5058 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5059 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5060 | goto onError; |
| 5061 | q += 4; |
| 5062 | continue; |
| 5063 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5064 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5065 | startinpos = ((const char *)q) - starts; |
| 5066 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5067 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5068 | |
| 5069 | /* The remaining input chars are ignored if the callback |
| 5070 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5071 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5072 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5073 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5074 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5075 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5079 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5082 | Py_XDECREF(errorHandler); |
| 5083 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5084 | return _PyUnicodeWriter_Finish(&writer); |
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 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5087 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5088 | Py_XDECREF(errorHandler); |
| 5089 | Py_XDECREF(exc); |
| 5090 | return NULL; |
| 5091 | } |
| 5092 | |
| 5093 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5094 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5095 | const char *errors, |
| 5096 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5098 | int kind; |
| 5099 | void *data; |
| 5100 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5101 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5102 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5103 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5104 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5105 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | int iorder[] = {0, 1, 2, 3}; |
| 5107 | #else |
| 5108 | int iorder[] = {3, 2, 1, 0}; |
| 5109 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5110 | const char *encoding; |
| 5111 | PyObject *errorHandler = NULL; |
| 5112 | PyObject *exc = NULL; |
| 5113 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5114 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5115 | #define STORECHAR(CH) \ |
| 5116 | do { \ |
| 5117 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5118 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5119 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5120 | p[iorder[0]] = (CH) & 0xff; \ |
| 5121 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5122 | } while(0) |
| 5123 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5124 | if (!PyUnicode_Check(str)) { |
| 5125 | PyErr_BadArgument(); |
| 5126 | return NULL; |
| 5127 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5128 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5129 | return NULL; |
| 5130 | kind = PyUnicode_KIND(str); |
| 5131 | data = PyUnicode_DATA(str); |
| 5132 | len = PyUnicode_GET_LENGTH(str); |
| 5133 | |
| 5134 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5135 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5136 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5137 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | if (v == NULL) |
| 5139 | return NULL; |
| 5140 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5141 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5142 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5144 | if (len == 0) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5145 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5146 | |
| 5147 | if (byteorder == -1) { |
| 5148 | /* force LE */ |
| 5149 | iorder[0] = 0; |
| 5150 | iorder[1] = 1; |
| 5151 | iorder[2] = 2; |
| 5152 | iorder[3] = 3; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5153 | encoding = "utf-32-le"; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5154 | } |
| 5155 | else if (byteorder == 1) { |
| 5156 | /* force BE */ |
| 5157 | iorder[0] = 3; |
| 5158 | iorder[1] = 2; |
| 5159 | iorder[2] = 1; |
| 5160 | iorder[3] = 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5161 | encoding = "utf-32-be"; |
| 5162 | } |
| 5163 | else |
| 5164 | encoding = "utf-32"; |
| 5165 | |
| 5166 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5167 | for (i = 0; i < len; i++) |
| 5168 | STORECHAR(PyUnicode_READ(kind, data, i)); |
| 5169 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5172 | for (i = 0; i < len;) { |
| 5173 | Py_ssize_t repsize, moreunits; |
| 5174 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5175 | i++; |
| 5176 | assert(ch <= MAX_UNICODE); |
| 5177 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
| 5178 | STORECHAR(ch); |
| 5179 | continue; |
| 5180 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5181 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5182 | rep = unicode_encode_call_errorhandler( |
| 5183 | errors, &errorHandler, |
| 5184 | encoding, "surrogates not allowed", |
| 5185 | str, &exc, i-1, i, &i); |
| 5186 | |
| 5187 | if (!rep) |
| 5188 | goto error; |
| 5189 | |
| 5190 | if (PyBytes_Check(rep)) { |
| 5191 | repsize = PyBytes_GET_SIZE(rep); |
| 5192 | if (repsize & 3) { |
| 5193 | raise_encode_exception(&exc, encoding, |
| 5194 | str, i - 1, i, |
| 5195 | "surrogates not allowed"); |
| 5196 | goto error; |
| 5197 | } |
| 5198 | moreunits = repsize / 4; |
| 5199 | } |
| 5200 | else { |
| 5201 | assert(PyUnicode_Check(rep)); |
| 5202 | if (PyUnicode_READY(rep) < 0) |
| 5203 | goto error; |
| 5204 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5205 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5206 | raise_encode_exception(&exc, encoding, |
| 5207 | str, i - 1, i, |
| 5208 | "surrogates not allowed"); |
| 5209 | goto error; |
| 5210 | } |
| 5211 | } |
| 5212 | |
| 5213 | /* four bytes are reserved for each surrogate */ |
| 5214 | if (moreunits > 1) { |
| 5215 | Py_ssize_t outpos = p - (unsigned char*) PyBytes_AS_STRING(v); |
| 5216 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5217 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5218 | /* integer overflow */ |
| 5219 | PyErr_NoMemory(); |
| 5220 | goto error; |
| 5221 | } |
| 5222 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5223 | goto error; |
| 5224 | p = (unsigned char*) PyBytes_AS_STRING(v) + outpos; |
| 5225 | } |
| 5226 | |
| 5227 | if (PyBytes_Check(rep)) { |
| 5228 | Py_MEMCPY(p, PyBytes_AS_STRING(rep), repsize); |
| 5229 | p += repsize; |
| 5230 | } else /* rep is unicode */ { |
| 5231 | const Py_UCS1 *repdata; |
| 5232 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5233 | repdata = PyUnicode_1BYTE_DATA(rep); |
| 5234 | while (repsize--) { |
| 5235 | Py_UCS4 ch = *repdata++; |
| 5236 | STORECHAR(ch); |
| 5237 | } |
| 5238 | } |
| 5239 | |
| 5240 | Py_CLEAR(rep); |
| 5241 | } |
| 5242 | |
| 5243 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5244 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5245 | handler is used. */ |
| 5246 | nsize = p - (unsigned char*) PyBytes_AS_STRING(v); |
| 5247 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5248 | _PyBytes_Resize(&v, nsize); |
| 5249 | Py_XDECREF(errorHandler); |
| 5250 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5251 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5252 | error: |
| 5253 | Py_XDECREF(rep); |
| 5254 | Py_XDECREF(errorHandler); |
| 5255 | Py_XDECREF(exc); |
| 5256 | Py_XDECREF(v); |
| 5257 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5258 | #undef STORECHAR |
| 5259 | } |
| 5260 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5261 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5262 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5263 | Py_ssize_t size, |
| 5264 | const char *errors, |
| 5265 | int byteorder) |
| 5266 | { |
| 5267 | PyObject *result; |
| 5268 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5269 | if (tmp == NULL) |
| 5270 | return NULL; |
| 5271 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5272 | Py_DECREF(tmp); |
| 5273 | return result; |
| 5274 | } |
| 5275 | |
| 5276 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5277 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5278 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5279 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5283 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5284 | PyObject * |
| 5285 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5286 | Py_ssize_t size, |
| 5287 | const char *errors, |
| 5288 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5290 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5291 | } |
| 5292 | |
| 5293 | PyObject * |
| 5294 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5295 | Py_ssize_t size, |
| 5296 | const char *errors, |
| 5297 | int *byteorder, |
| 5298 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5299 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5300 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5301 | Py_ssize_t startinpos; |
| 5302 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5303 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5304 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5305 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5306 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5307 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5308 | PyObject *errorHandler = NULL; |
| 5309 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5310 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5311 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5312 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5313 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5314 | |
| 5315 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5316 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5318 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5319 | byte order setting accordingly. In native mode, the leading BOM |
| 5320 | mark is skipped, in all other modes, it is copied to the output |
| 5321 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5322 | if (bo == 0 && size >= 2) { |
| 5323 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5324 | if (bom == 0xFEFF) { |
| 5325 | q += 2; |
| 5326 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5327 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5328 | else if (bom == 0xFFFE) { |
| 5329 | q += 2; |
| 5330 | bo = 1; |
| 5331 | } |
| 5332 | if (byteorder) |
| 5333 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5336 | if (q == e) { |
| 5337 | if (consumed) |
| 5338 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5339 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5340 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5341 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5342 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5343 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5344 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5345 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5346 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5347 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5348 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5349 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5350 | /* Note: size will always be longer than the resulting Unicode |
| 5351 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5352 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5353 | writer.min_length = (e - q + 1) / 2; |
| 5354 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5355 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5356 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5357 | while (1) { |
| 5358 | Py_UCS4 ch = 0; |
| 5359 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5360 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5361 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5362 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5363 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5364 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5365 | native_ordering); |
| 5366 | else |
| 5367 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5368 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5369 | native_ordering); |
| 5370 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5371 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5372 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5373 | native_ordering); |
| 5374 | } else { |
| 5375 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5376 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5377 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5378 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5379 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5380 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5381 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5382 | switch (ch) |
| 5383 | { |
| 5384 | case 0: |
| 5385 | /* remaining byte at the end? (size should be even) */ |
| 5386 | if (q == e || consumed) |
| 5387 | goto End; |
| 5388 | errmsg = "truncated data"; |
| 5389 | startinpos = ((const char *)q) - starts; |
| 5390 | endinpos = ((const char *)e) - starts; |
| 5391 | break; |
| 5392 | /* The remaining input chars are ignored if the callback |
| 5393 | chooses to skip the input */ |
| 5394 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5395 | q -= 2; |
| 5396 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5397 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5398 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5399 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5400 | endinpos = ((const char *)e) - starts; |
| 5401 | break; |
| 5402 | case 2: |
| 5403 | errmsg = "illegal encoding"; |
| 5404 | startinpos = ((const char *)q) - 2 - starts; |
| 5405 | endinpos = startinpos + 2; |
| 5406 | break; |
| 5407 | case 3: |
| 5408 | errmsg = "illegal UTF-16 surrogate"; |
| 5409 | startinpos = ((const char *)q) - 4 - starts; |
| 5410 | endinpos = startinpos + 2; |
| 5411 | break; |
| 5412 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5413 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5414 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5415 | continue; |
| 5416 | } |
| 5417 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5418 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5419 | errors, |
| 5420 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5421 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5422 | &starts, |
| 5423 | (const char **)&e, |
| 5424 | &startinpos, |
| 5425 | &endinpos, |
| 5426 | &exc, |
| 5427 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5428 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5432 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5433 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5434 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5435 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5436 | Py_XDECREF(errorHandler); |
| 5437 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5438 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5440 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5441 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | Py_XDECREF(errorHandler); |
| 5443 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | return NULL; |
| 5445 | } |
| 5446 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5447 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5448 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5449 | const char *errors, |
| 5450 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5452 | enum PyUnicode_Kind kind; |
| 5453 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5454 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5455 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5456 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5457 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5458 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5459 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5460 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5461 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5462 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5463 | const char *encoding; |
| 5464 | Py_ssize_t nsize, pos; |
| 5465 | PyObject *errorHandler = NULL; |
| 5466 | PyObject *exc = NULL; |
| 5467 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5468 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5469 | if (!PyUnicode_Check(str)) { |
| 5470 | PyErr_BadArgument(); |
| 5471 | return NULL; |
| 5472 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5473 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5474 | return NULL; |
| 5475 | kind = PyUnicode_KIND(str); |
| 5476 | data = PyUnicode_DATA(str); |
| 5477 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5478 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5479 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5480 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5481 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5482 | const Py_UCS4 *end = in + len; |
| 5483 | while (in < end) |
| 5484 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5485 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5486 | } |
| 5487 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5488 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5489 | nsize = len + pairs + (byteorder == 0); |
| 5490 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | if (v == NULL) |
| 5492 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5494 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5495 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5496 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5498 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5499 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5500 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5501 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5502 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5503 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5504 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5505 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5506 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5507 | if (byteorder < 0) |
| 5508 | encoding = "utf-16-le"; |
| 5509 | else if (byteorder > 0) |
| 5510 | encoding = "utf-16-be"; |
| 5511 | else |
| 5512 | encoding = "utf-16"; |
| 5513 | |
| 5514 | pos = 0; |
| 5515 | while (pos < len) { |
| 5516 | Py_ssize_t repsize, moreunits; |
| 5517 | |
| 5518 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5519 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5520 | &out, native_ordering); |
| 5521 | } |
| 5522 | else { |
| 5523 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5524 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5525 | &out, native_ordering); |
| 5526 | } |
| 5527 | if (pos == len) |
| 5528 | break; |
| 5529 | |
| 5530 | rep = unicode_encode_call_errorhandler( |
| 5531 | errors, &errorHandler, |
| 5532 | encoding, "surrogates not allowed", |
| 5533 | str, &exc, pos, pos + 1, &pos); |
| 5534 | if (!rep) |
| 5535 | goto error; |
| 5536 | |
| 5537 | if (PyBytes_Check(rep)) { |
| 5538 | repsize = PyBytes_GET_SIZE(rep); |
| 5539 | if (repsize & 1) { |
| 5540 | raise_encode_exception(&exc, encoding, |
| 5541 | str, pos - 1, pos, |
| 5542 | "surrogates not allowed"); |
| 5543 | goto error; |
| 5544 | } |
| 5545 | moreunits = repsize / 2; |
| 5546 | } |
| 5547 | else { |
| 5548 | assert(PyUnicode_Check(rep)); |
| 5549 | if (PyUnicode_READY(rep) < 0) |
| 5550 | goto error; |
| 5551 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5552 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5553 | raise_encode_exception(&exc, encoding, |
| 5554 | str, pos - 1, pos, |
| 5555 | "surrogates not allowed"); |
| 5556 | goto error; |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | /* two bytes are reserved for each surrogate */ |
| 5561 | if (moreunits > 1) { |
| 5562 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5563 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5564 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5565 | /* integer overflow */ |
| 5566 | PyErr_NoMemory(); |
| 5567 | goto error; |
| 5568 | } |
| 5569 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5570 | goto error; |
| 5571 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5572 | } |
| 5573 | |
| 5574 | if (PyBytes_Check(rep)) { |
| 5575 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5576 | out += moreunits; |
| 5577 | } else /* rep is unicode */ { |
| 5578 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5579 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5580 | &out, native_ordering); |
| 5581 | } |
| 5582 | |
| 5583 | Py_CLEAR(rep); |
| 5584 | } |
| 5585 | |
| 5586 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5587 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5588 | is used. */ |
| 5589 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5590 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5591 | _PyBytes_Resize(&v, nsize); |
| 5592 | Py_XDECREF(errorHandler); |
| 5593 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5594 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5595 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5596 | error: |
| 5597 | Py_XDECREF(rep); |
| 5598 | Py_XDECREF(errorHandler); |
| 5599 | Py_XDECREF(exc); |
| 5600 | Py_XDECREF(v); |
| 5601 | return NULL; |
| 5602 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5605 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5606 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5607 | Py_ssize_t size, |
| 5608 | const char *errors, |
| 5609 | int byteorder) |
| 5610 | { |
| 5611 | PyObject *result; |
| 5612 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5613 | if (tmp == NULL) |
| 5614 | return NULL; |
| 5615 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5616 | Py_DECREF(tmp); |
| 5617 | return result; |
| 5618 | } |
| 5619 | |
| 5620 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5621 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5623 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5628 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5629 | if all the escapes in the string make it still a valid ASCII string. |
| 5630 | Returns -1 if any escapes were found which cause the string to |
| 5631 | pop out of ASCII range. Otherwise returns the length of the |
| 5632 | required buffer to hold the string. |
| 5633 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5634 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5635 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5636 | { |
| 5637 | const unsigned char *p = (const unsigned char *)s; |
| 5638 | const unsigned char *end = p + size; |
| 5639 | Py_ssize_t length = 0; |
| 5640 | |
| 5641 | if (size < 0) |
| 5642 | return -1; |
| 5643 | |
| 5644 | for (; p < end; ++p) { |
| 5645 | if (*p > 127) { |
| 5646 | /* Non-ASCII */ |
| 5647 | return -1; |
| 5648 | } |
| 5649 | else if (*p != '\\') { |
| 5650 | /* Normal character */ |
| 5651 | ++length; |
| 5652 | } |
| 5653 | else { |
| 5654 | /* Backslash-escape, check next char */ |
| 5655 | ++p; |
| 5656 | /* Escape sequence reaches till end of string or |
| 5657 | non-ASCII follow-up. */ |
| 5658 | if (p >= end || *p > 127) |
| 5659 | return -1; |
| 5660 | switch (*p) { |
| 5661 | case '\n': |
| 5662 | /* backslash + \n result in zero characters */ |
| 5663 | break; |
| 5664 | case '\\': case '\'': case '\"': |
| 5665 | case 'b': case 'f': case 't': |
| 5666 | case 'n': case 'r': case 'v': case 'a': |
| 5667 | ++length; |
| 5668 | break; |
| 5669 | case '0': case '1': case '2': case '3': |
| 5670 | case '4': case '5': case '6': case '7': |
| 5671 | case 'x': case 'u': case 'U': case 'N': |
| 5672 | /* these do not guarantee ASCII characters */ |
| 5673 | return -1; |
| 5674 | default: |
| 5675 | /* count the backslash + the other character */ |
| 5676 | length += 2; |
| 5677 | } |
| 5678 | } |
| 5679 | } |
| 5680 | return length; |
| 5681 | } |
| 5682 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5683 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5684 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5685 | PyObject * |
| 5686 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5687 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5688 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5690 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5691 | Py_ssize_t startinpos; |
| 5692 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5693 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5695 | char* message; |
| 5696 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5697 | PyObject *errorHandler = NULL; |
| 5698 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5699 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5700 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5701 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5702 | if (len == 0) |
| 5703 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | |
| 5705 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5706 | either the string is pure ASCII with named escapes like \n, etc. |
| 5707 | and we determined it's exact size (common case) |
| 5708 | or it contains \x, \u, ... escape sequences. then we create a |
| 5709 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5710 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5711 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5712 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5713 | } |
| 5714 | else { |
| 5715 | /* Escaped strings will always be longer than the resulting |
| 5716 | Unicode string, so we start with size here and then reduce the |
| 5717 | length after conversion to the true value. |
| 5718 | (but if the error callback returns a long replacement string |
| 5719 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5720 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5721 | } |
| 5722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5724 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5726 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | while (s < end) { |
| 5728 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5729 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5730 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | |
| 5732 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5733 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5734 | x = (unsigned char)*s; |
| 5735 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5736 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5737 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | continue; |
| 5739 | } |
| 5740 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | /* \ - Escapes */ |
| 5743 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5744 | c = *s++; |
| 5745 | if (s > end) |
| 5746 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5747 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5748 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5749 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5751 | #define WRITECHAR(ch) \ |
| 5752 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5753 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5754 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5755 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5758 | case '\\': WRITECHAR('\\'); break; |
| 5759 | case '\'': WRITECHAR('\''); break; |
| 5760 | case '\"': WRITECHAR('\"'); break; |
| 5761 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5762 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5763 | case 'f': WRITECHAR('\014'); break; |
| 5764 | case 't': WRITECHAR('\t'); break; |
| 5765 | case 'n': WRITECHAR('\n'); break; |
| 5766 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5768 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5769 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5770 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | case '0': case '1': case '2': case '3': |
| 5774 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5775 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5776 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5777 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5778 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5779 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5781 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | break; |
| 5783 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | /* hex escapes */ |
| 5785 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5787 | digits = 2; |
| 5788 | message = "truncated \\xXX escape"; |
| 5789 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5791 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5793 | digits = 4; |
| 5794 | message = "truncated \\uXXXX escape"; |
| 5795 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5797 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5798 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5799 | digits = 8; |
| 5800 | message = "truncated \\UXXXXXXXX escape"; |
| 5801 | hexescape: |
| 5802 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5803 | if (end - s < digits) { |
| 5804 | /* count only hex digits */ |
| 5805 | for (; s < end; ++s) { |
| 5806 | c = (unsigned char)*s; |
| 5807 | if (!Py_ISXDIGIT(c)) |
| 5808 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5809 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5810 | goto error; |
| 5811 | } |
| 5812 | for (; digits--; ++s) { |
| 5813 | c = (unsigned char)*s; |
| 5814 | if (!Py_ISXDIGIT(c)) |
| 5815 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5816 | chr = (chr<<4) & ~0xF; |
| 5817 | if (c >= '0' && c <= '9') |
| 5818 | chr += c - '0'; |
| 5819 | else if (c >= 'a' && c <= 'f') |
| 5820 | chr += 10 + c - 'a'; |
| 5821 | else |
| 5822 | chr += 10 + c - 'A'; |
| 5823 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5824 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5825 | /* _decoding_error will have already written into the |
| 5826 | target buffer. */ |
| 5827 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5828 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5829 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5830 | message = "illegal Unicode character"; |
| 5831 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5832 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5833 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5834 | break; |
| 5835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5837 | case 'N': |
| 5838 | message = "malformed \\N character escape"; |
| 5839 | if (ucnhash_CAPI == NULL) { |
| 5840 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5841 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5842 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5843 | if (ucnhash_CAPI == NULL) |
| 5844 | goto ucnhashError; |
| 5845 | } |
| 5846 | if (*s == '{') { |
| 5847 | const char *start = s+1; |
| 5848 | /* look for the closing brace */ |
| 5849 | while (*s != '}' && s < end) |
| 5850 | s++; |
| 5851 | if (s > start && s < end && *s == '}') { |
| 5852 | /* found a name. look it up in the unicode database */ |
| 5853 | message = "unknown Unicode character name"; |
| 5854 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5855 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5856 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5857 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5858 | goto store; |
| 5859 | } |
| 5860 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5861 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5862 | |
| 5863 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5864 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5865 | message = "\\ at end of string"; |
| 5866 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5867 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5868 | } |
| 5869 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5870 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5871 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5872 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5873 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5875 | continue; |
| 5876 | |
| 5877 | error: |
| 5878 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5879 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5880 | errors, &errorHandler, |
| 5881 | "unicodeescape", message, |
| 5882 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5883 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5884 | goto onError; |
| 5885 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5887 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5888 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5889 | Py_XDECREF(errorHandler); |
| 5890 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5891 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5894 | PyErr_SetString( |
| 5895 | PyExc_UnicodeError, |
| 5896 | "\\N escapes not supported (can't load unicodedata module)" |
| 5897 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5898 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5899 | Py_XDECREF(errorHandler); |
| 5900 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5901 | return NULL; |
| 5902 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5904 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5905 | Py_XDECREF(errorHandler); |
| 5906 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | return NULL; |
| 5908 | } |
| 5909 | |
| 5910 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5911 | |
| 5912 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5913 | appropriate. |
| 5914 | |
| 5915 | */ |
| 5916 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5917 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5918 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5920 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5921 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5923 | int kind; |
| 5924 | void *data; |
| 5925 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5927 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5928 | escape. |
| 5929 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5930 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5931 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5932 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5933 | */ |
| 5934 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5935 | if (!PyUnicode_Check(unicode)) { |
| 5936 | PyErr_BadArgument(); |
| 5937 | return NULL; |
| 5938 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5939 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5940 | return NULL; |
| 5941 | len = PyUnicode_GET_LENGTH(unicode); |
| 5942 | kind = PyUnicode_KIND(unicode); |
| 5943 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5944 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5945 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5946 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5947 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5948 | } |
| 5949 | |
| 5950 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5951 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5952 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5953 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5954 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5955 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5956 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5957 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5958 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5959 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | if (repr == NULL) |
| 5961 | return NULL; |
| 5962 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5963 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5965 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5966 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5967 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5968 | /* Escape backslashes */ |
| 5969 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | *p++ = '\\'; |
| 5971 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5972 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5973 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5974 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5975 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5976 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5977 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5978 | *p++ = '\\'; |
| 5979 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5980 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5981 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5982 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5983 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5984 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5985 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5986 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5987 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5989 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5990 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5992 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | *p++ = '\\'; |
| 5994 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5995 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5996 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5997 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5998 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6000 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6001 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6002 | else if (ch == '\t') { |
| 6003 | *p++ = '\\'; |
| 6004 | *p++ = 't'; |
| 6005 | } |
| 6006 | else if (ch == '\n') { |
| 6007 | *p++ = '\\'; |
| 6008 | *p++ = 'n'; |
| 6009 | } |
| 6010 | else if (ch == '\r') { |
| 6011 | *p++ = '\\'; |
| 6012 | *p++ = 'r'; |
| 6013 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6014 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6015 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6016 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6018 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6019 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6020 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6021 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | /* Copy everything else as-is */ |
| 6024 | else |
| 6025 | *p++ = (char) ch; |
| 6026 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6028 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6029 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6030 | return NULL; |
| 6031 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6034 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6035 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6036 | Py_ssize_t size) |
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 | PyObject *result; |
| 6039 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6040 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6042 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6043 | Py_DECREF(tmp); |
| 6044 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6048 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6049 | PyObject * |
| 6050 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6051 | Py_ssize_t size, |
| 6052 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6054 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6055 | Py_ssize_t startinpos; |
| 6056 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6057 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | const char *end; |
| 6059 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6060 | PyObject *errorHandler = NULL; |
| 6061 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6062 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6063 | if (size == 0) |
| 6064 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | /* Escaped strings will always be longer than the resulting |
| 6067 | 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] | 6068 | length after conversion to the true value. (But decoding error |
| 6069 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6070 | _PyUnicodeWriter_Init(&writer); |
| 6071 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6072 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | end = s + size; |
| 6074 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | unsigned char c; |
| 6076 | Py_UCS4 x; |
| 6077 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6078 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6080 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6081 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6082 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6083 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6084 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6086 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6087 | startinpos = s-starts; |
| 6088 | |
| 6089 | /* \u-escapes are only interpreted iff the number of leading |
| 6090 | backslashes if odd */ |
| 6091 | bs = s; |
| 6092 | for (;s < end;) { |
| 6093 | if (*s != '\\') |
| 6094 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6095 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6096 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6097 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6098 | } |
| 6099 | if (((s - bs) & 1) == 0 || |
| 6100 | s >= end || |
| 6101 | (*s != 'u' && *s != 'U')) { |
| 6102 | continue; |
| 6103 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6104 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6105 | count = *s=='u' ? 4 : 8; |
| 6106 | s++; |
| 6107 | |
| 6108 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6110 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6111 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6113 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6114 | errors, &errorHandler, |
| 6115 | "rawunicodeescape", "truncated \\uXXXX", |
| 6116 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6117 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | goto onError; |
| 6119 | goto nextByte; |
| 6120 | } |
| 6121 | x = (x<<4) & ~0xF; |
| 6122 | if (c >= '0' && c <= '9') |
| 6123 | x += c - '0'; |
| 6124 | else if (c >= 'a' && c <= 'f') |
| 6125 | x += 10 + c - 'a'; |
| 6126 | else |
| 6127 | x += 10 + c - 'A'; |
| 6128 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6129 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6130 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6131 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6132 | } |
| 6133 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6134 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6135 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6136 | errors, &errorHandler, |
| 6137 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6139 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6141 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | nextByte: |
| 6143 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | Py_XDECREF(errorHandler); |
| 6146 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6147 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6150 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6151 | Py_XDECREF(errorHandler); |
| 6152 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | return NULL; |
| 6154 | } |
| 6155 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6156 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6157 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6158 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6160 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | char *p; |
| 6162 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6163 | Py_ssize_t expandsize, pos; |
| 6164 | int kind; |
| 6165 | void *data; |
| 6166 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6168 | if (!PyUnicode_Check(unicode)) { |
| 6169 | PyErr_BadArgument(); |
| 6170 | return NULL; |
| 6171 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6172 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6173 | return NULL; |
| 6174 | kind = PyUnicode_KIND(unicode); |
| 6175 | data = PyUnicode_DATA(unicode); |
| 6176 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6177 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6178 | bytes, and 1 byte characters 4. */ |
| 6179 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6180 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6181 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6182 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6183 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6184 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | if (repr == NULL) |
| 6186 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6187 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6188 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6190 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6191 | for (pos = 0; pos < len; pos++) { |
| 6192 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6193 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6194 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6195 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6196 | *p++ = '\\'; |
| 6197 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6198 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6199 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6200 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6201 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6202 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6203 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6204 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6205 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6206 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6207 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6208 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | *p++ = '\\'; |
| 6210 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6211 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6212 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6213 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6214 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 | /* Copy everything else as-is */ |
| 6217 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | *p++ = (char) ch; |
| 6219 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6220 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6221 | assert(p > q); |
| 6222 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6223 | return NULL; |
| 6224 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6227 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6228 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6229 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6230 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6231 | PyObject *result; |
| 6232 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6233 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6234 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6235 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6236 | Py_DECREF(tmp); |
| 6237 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6240 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6241 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6242 | PyObject * |
| 6243 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6244 | Py_ssize_t size, |
| 6245 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6246 | { |
| 6247 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6248 | Py_ssize_t startinpos; |
| 6249 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6250 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6251 | const char *end; |
| 6252 | const char *reason; |
| 6253 | PyObject *errorHandler = NULL; |
| 6254 | PyObject *exc = NULL; |
| 6255 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6256 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6257 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6258 | 1)) |
| 6259 | return NULL; |
| 6260 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6261 | if (size == 0) |
| 6262 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6263 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6264 | _PyUnicodeWriter_Init(&writer); |
| 6265 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6266 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6268 | } |
| 6269 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6270 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6271 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6272 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6273 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6274 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6275 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6276 | endinpos = end-starts; |
| 6277 | reason = "truncated input"; |
| 6278 | goto error; |
| 6279 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6280 | /* We copy the raw representation one byte at a time because the |
| 6281 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6282 | ((char *) &uch)[0] = s[0]; |
| 6283 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6284 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6285 | ((char *) &uch)[2] = s[2]; |
| 6286 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6287 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6288 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6289 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6290 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6291 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6292 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6293 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6294 | reason = "illegal code point (> 0x10FFFF)"; |
| 6295 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6296 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6297 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6298 | s += Py_UNICODE_SIZE; |
| 6299 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6300 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6301 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6302 | Py_UNICODE uch2; |
| 6303 | ((char *) &uch2)[0] = s[0]; |
| 6304 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6305 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6306 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6307 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6308 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6309 | } |
| 6310 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6311 | #endif |
| 6312 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6313 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6314 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6315 | continue; |
| 6316 | |
| 6317 | error: |
| 6318 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6319 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6320 | errors, &errorHandler, |
| 6321 | "unicode_internal", reason, |
| 6322 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6323 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6324 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6327 | Py_XDECREF(errorHandler); |
| 6328 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6329 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6330 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6331 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6332 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6333 | Py_XDECREF(errorHandler); |
| 6334 | Py_XDECREF(exc); |
| 6335 | return NULL; |
| 6336 | } |
| 6337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6339 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6340 | PyObject * |
| 6341 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6342 | Py_ssize_t size, |
| 6343 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6346 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6349 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6350 | static void |
| 6351 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6352 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6353 | PyObject *unicode, |
| 6354 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6355 | const char *reason) |
| 6356 | { |
| 6357 | if (*exceptionObject == NULL) { |
| 6358 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6359 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6360 | encoding, unicode, startpos, endpos, reason); |
| 6361 | } |
| 6362 | else { |
| 6363 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6364 | goto onError; |
| 6365 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6366 | goto onError; |
| 6367 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6368 | goto onError; |
| 6369 | return; |
| 6370 | onError: |
| 6371 | Py_DECREF(*exceptionObject); |
| 6372 | *exceptionObject = NULL; |
| 6373 | } |
| 6374 | } |
| 6375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6376 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6377 | static void |
| 6378 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6379 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6380 | PyObject *unicode, |
| 6381 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6382 | const char *reason) |
| 6383 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6384 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6385 | encoding, unicode, startpos, endpos, reason); |
| 6386 | if (*exceptionObject != NULL) |
| 6387 | PyCodec_StrictErrors(*exceptionObject); |
| 6388 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6389 | |
| 6390 | /* error handling callback helper: |
| 6391 | build arguments, call the callback and check the arguments, |
| 6392 | put the result into newpos and return the replacement string, which |
| 6393 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6394 | static PyObject * |
| 6395 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6396 | PyObject **errorHandler, |
| 6397 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6398 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6399 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6400 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6401 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6402 | 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] | 6403 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6404 | PyObject *restuple; |
| 6405 | PyObject *resunicode; |
| 6406 | |
| 6407 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6408 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6409 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6413 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6414 | return NULL; |
| 6415 | len = PyUnicode_GET_LENGTH(unicode); |
| 6416 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6417 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6418 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6421 | |
| 6422 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6426 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6427 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | Py_DECREF(restuple); |
| 6429 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6431 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6432 | &resunicode, newpos)) { |
| 6433 | Py_DECREF(restuple); |
| 6434 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6435 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6436 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6437 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6438 | Py_DECREF(restuple); |
| 6439 | return NULL; |
| 6440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6442 | *newpos = len + *newpos; |
| 6443 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6444 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6445 | Py_DECREF(restuple); |
| 6446 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6447 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6448 | Py_INCREF(resunicode); |
| 6449 | Py_DECREF(restuple); |
| 6450 | return resunicode; |
| 6451 | } |
| 6452 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6453 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6454 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6455 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6456 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6458 | /* input state */ |
| 6459 | Py_ssize_t pos=0, size; |
| 6460 | int kind; |
| 6461 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | /* output object */ |
| 6463 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6464 | /* pointer into the output */ |
| 6465 | char *str; |
| 6466 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6467 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6468 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6469 | 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] | 6470 | PyObject *errorHandler = NULL; |
| 6471 | PyObject *exc = NULL; |
| 6472 | /* the following variable is used for caching string comparisons |
| 6473 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6474 | int known_errorHandler = -1; |
| 6475 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6476 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6477 | return NULL; |
| 6478 | size = PyUnicode_GET_LENGTH(unicode); |
| 6479 | kind = PyUnicode_KIND(unicode); |
| 6480 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6481 | /* allocate enough for a simple encoding without |
| 6482 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6483 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6484 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6485 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6486 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6487 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6488 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6489 | ressize = size; |
| 6490 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6491 | while (pos < size) { |
| 6492 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | /* can we encode this? */ |
| 6495 | if (c<limit) { |
| 6496 | /* no overflow check, because we know that the space is enough */ |
| 6497 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6498 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6499 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6500 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6501 | Py_ssize_t requiredsize; |
| 6502 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6503 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6504 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6505 | Py_ssize_t collstart = pos; |
| 6506 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6507 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6508 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | ++collend; |
| 6510 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6511 | if (known_errorHandler==-1) { |
| 6512 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6513 | known_errorHandler = 1; |
| 6514 | else if (!strcmp(errors, "replace")) |
| 6515 | known_errorHandler = 2; |
| 6516 | else if (!strcmp(errors, "ignore")) |
| 6517 | known_errorHandler = 3; |
| 6518 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6519 | known_errorHandler = 4; |
| 6520 | else |
| 6521 | known_errorHandler = 0; |
| 6522 | } |
| 6523 | switch (known_errorHandler) { |
| 6524 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6525 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6526 | goto onError; |
| 6527 | case 2: /* replace */ |
| 6528 | while (collstart++<collend) |
| 6529 | *str++ = '?'; /* fall through */ |
| 6530 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6531 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | break; |
| 6533 | case 4: /* xmlcharrefreplace */ |
| 6534 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6535 | /* determine replacement size */ |
| 6536 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6537 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6538 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6539 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6540 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6542 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6544 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6545 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6546 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6548 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6550 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6551 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6553 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6555 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | if (requiredsize > ressize) { |
| 6557 | if (requiredsize<2*ressize) |
| 6558 | requiredsize = 2*ressize; |
| 6559 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6560 | goto onError; |
| 6561 | str = PyBytes_AS_STRING(res) + respos; |
| 6562 | ressize = requiredsize; |
| 6563 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6564 | /* generate replacement */ |
| 6565 | for (i = collstart; i < collend; ++i) { |
| 6566 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6567 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6568 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | break; |
| 6570 | default: |
| 6571 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6572 | encoding, reason, unicode, &exc, |
| 6573 | collstart, collend, &newpos); |
| 6574 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6575 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6576 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6577 | if (PyBytes_Check(repunicode)) { |
| 6578 | /* Directly copy bytes result to output. */ |
| 6579 | repsize = PyBytes_Size(repunicode); |
| 6580 | if (repsize > 1) { |
| 6581 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6582 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6583 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6584 | Py_DECREF(repunicode); |
| 6585 | goto onError; |
| 6586 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6587 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6588 | ressize += repsize-1; |
| 6589 | } |
| 6590 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6591 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6592 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6593 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6594 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6595 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | /* need more space? (at least enough for what we |
| 6597 | have+the replacement+the rest of the string, so |
| 6598 | we won't have to check space for encodable characters) */ |
| 6599 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6600 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6601 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | if (requiredsize > ressize) { |
| 6603 | if (requiredsize<2*ressize) |
| 6604 | requiredsize = 2*ressize; |
| 6605 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6606 | Py_DECREF(repunicode); |
| 6607 | goto onError; |
| 6608 | } |
| 6609 | str = PyBytes_AS_STRING(res) + respos; |
| 6610 | ressize = requiredsize; |
| 6611 | } |
| 6612 | /* check if there is anything unencodable in the replacement |
| 6613 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6614 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6615 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6617 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6618 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | Py_DECREF(repunicode); |
| 6620 | goto onError; |
| 6621 | } |
| 6622 | *str = (char)c; |
| 6623 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6624 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6625 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6626 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6627 | } |
| 6628 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6629 | /* Resize if we allocated to much */ |
| 6630 | size = str - PyBytes_AS_STRING(res); |
| 6631 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6632 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6633 | if (_PyBytes_Resize(&res, size) < 0) |
| 6634 | goto onError; |
| 6635 | } |
| 6636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6637 | Py_XDECREF(errorHandler); |
| 6638 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6639 | return res; |
| 6640 | |
| 6641 | onError: |
| 6642 | Py_XDECREF(res); |
| 6643 | Py_XDECREF(errorHandler); |
| 6644 | Py_XDECREF(exc); |
| 6645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6648 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6649 | PyObject * |
| 6650 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6651 | Py_ssize_t size, |
| 6652 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6654 | PyObject *result; |
| 6655 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6656 | if (unicode == NULL) |
| 6657 | return NULL; |
| 6658 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6659 | Py_DECREF(unicode); |
| 6660 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6663 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6664 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | { |
| 6666 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | PyErr_BadArgument(); |
| 6668 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6669 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6670 | if (PyUnicode_READY(unicode) == -1) |
| 6671 | return NULL; |
| 6672 | /* Fast path: if it is a one-byte string, construct |
| 6673 | bytes object directly. */ |
| 6674 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6675 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6676 | PyUnicode_GET_LENGTH(unicode)); |
| 6677 | /* Non-Latin-1 characters present. Defer to above function to |
| 6678 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6679 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6680 | } |
| 6681 | |
| 6682 | PyObject* |
| 6683 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6684 | { |
| 6685 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6689 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6690 | PyObject * |
| 6691 | PyUnicode_DecodeASCII(const char *s, |
| 6692 | Py_ssize_t size, |
| 6693 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6695 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6696 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6697 | int kind; |
| 6698 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6699 | Py_ssize_t startinpos; |
| 6700 | Py_ssize_t endinpos; |
| 6701 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6702 | const char *e; |
| 6703 | PyObject *errorHandler = NULL; |
| 6704 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6706 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6707 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6708 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6709 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6710 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6711 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6712 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6713 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6714 | writer.min_length = size; |
| 6715 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6716 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6717 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6718 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6719 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6720 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6721 | writer.pos = outpos; |
| 6722 | if (writer.pos == size) |
| 6723 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6724 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6725 | s += writer.pos; |
| 6726 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6727 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6728 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6729 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6730 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6731 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6732 | ++s; |
| 6733 | } |
| 6734 | else { |
| 6735 | startinpos = s-starts; |
| 6736 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6737 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6738 | errors, &errorHandler, |
| 6739 | "ascii", "ordinal not in range(128)", |
| 6740 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6741 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6743 | kind = writer.kind; |
| 6744 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6747 | Py_XDECREF(errorHandler); |
| 6748 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6749 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6750 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6752 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6753 | Py_XDECREF(errorHandler); |
| 6754 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | return NULL; |
| 6756 | } |
| 6757 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6758 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6759 | PyObject * |
| 6760 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6761 | Py_ssize_t size, |
| 6762 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6764 | PyObject *result; |
| 6765 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6766 | if (unicode == NULL) |
| 6767 | return NULL; |
| 6768 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6769 | Py_DECREF(unicode); |
| 6770 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6773 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6774 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | { |
| 6776 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | PyErr_BadArgument(); |
| 6778 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6780 | if (PyUnicode_READY(unicode) == -1) |
| 6781 | return NULL; |
| 6782 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6783 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6784 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6785 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6786 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6787 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6788 | } |
| 6789 | |
| 6790 | PyObject * |
| 6791 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6792 | { |
| 6793 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6796 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6797 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6798 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6799 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6800 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6801 | #define NEED_RETRY |
| 6802 | #endif |
| 6803 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6804 | #ifndef WC_ERR_INVALID_CHARS |
| 6805 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6806 | #endif |
| 6807 | |
| 6808 | static char* |
| 6809 | code_page_name(UINT code_page, PyObject **obj) |
| 6810 | { |
| 6811 | *obj = NULL; |
| 6812 | if (code_page == CP_ACP) |
| 6813 | return "mbcs"; |
| 6814 | if (code_page == CP_UTF7) |
| 6815 | return "CP_UTF7"; |
| 6816 | if (code_page == CP_UTF8) |
| 6817 | return "CP_UTF8"; |
| 6818 | |
| 6819 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6820 | if (*obj == NULL) |
| 6821 | return NULL; |
| 6822 | return PyBytes_AS_STRING(*obj); |
| 6823 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6825 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6826 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6827 | { |
| 6828 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6829 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6830 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6831 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6832 | return 0; |
| 6833 | |
| 6834 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6835 | if (prev == curr) |
| 6836 | return 1; |
| 6837 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6838 | as it assumes an incomplete character consists of a single |
| 6839 | byte. */ |
| 6840 | if (curr - prev == 2) |
| 6841 | return 1; |
| 6842 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6843 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6844 | return 0; |
| 6845 | } |
| 6846 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6847 | static DWORD |
| 6848 | decode_code_page_flags(UINT code_page) |
| 6849 | { |
| 6850 | if (code_page == CP_UTF7) { |
| 6851 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6852 | return 0; |
| 6853 | } |
| 6854 | else |
| 6855 | return MB_ERR_INVALID_CHARS; |
| 6856 | } |
| 6857 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6858 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6859 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6860 | * mode. |
| 6861 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6862 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6863 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6864 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6865 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6866 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6867 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6868 | const char *in, |
| 6869 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6870 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6871 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6872 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6873 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6874 | |
| 6875 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6876 | assert(insize > 0); |
| 6877 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6878 | if (outsize <= 0) |
| 6879 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 | |
| 6881 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6882 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6883 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6884 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6885 | if (*v == NULL) |
| 6886 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6887 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6888 | } |
| 6889 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6890 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6891 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6892 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6893 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6894 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
| 6897 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6898 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6899 | if (outsize <= 0) |
| 6900 | goto error; |
| 6901 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6903 | error: |
| 6904 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6905 | return -2; |
| 6906 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6907 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | /* |
| 6911 | * Decode a byte string from a code page into unicode object with an error |
| 6912 | * handler. |
| 6913 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6914 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6915 | * UnicodeDecodeError exception and returns -1 on error. |
| 6916 | */ |
| 6917 | static int |
| 6918 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6919 | PyObject **v, |
| 6920 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6921 | const char *errors) |
| 6922 | { |
| 6923 | const char *startin = in; |
| 6924 | const char *endin = in + size; |
| 6925 | const DWORD flags = decode_code_page_flags(code_page); |
| 6926 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6927 | 2000 English version of the message. */ |
| 6928 | const char *reason = "No mapping for the Unicode character exists " |
| 6929 | "in the target code page."; |
| 6930 | /* each step cannot decode more than 1 character, but a character can be |
| 6931 | represented as a surrogate pair */ |
| 6932 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6933 | int insize; |
| 6934 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6935 | PyObject *errorHandler = NULL; |
| 6936 | PyObject *exc = NULL; |
| 6937 | PyObject *encoding_obj = NULL; |
| 6938 | char *encoding; |
| 6939 | DWORD err; |
| 6940 | int ret = -1; |
| 6941 | |
| 6942 | assert(size > 0); |
| 6943 | |
| 6944 | encoding = code_page_name(code_page, &encoding_obj); |
| 6945 | if (encoding == NULL) |
| 6946 | return -1; |
| 6947 | |
| 6948 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6949 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6950 | UnicodeDecodeError. */ |
| 6951 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6952 | if (exc != NULL) { |
| 6953 | PyCodec_StrictErrors(exc); |
| 6954 | Py_CLEAR(exc); |
| 6955 | } |
| 6956 | goto error; |
| 6957 | } |
| 6958 | |
| 6959 | if (*v == NULL) { |
| 6960 | /* Create unicode object */ |
| 6961 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6962 | PyErr_NoMemory(); |
| 6963 | goto error; |
| 6964 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6965 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6966 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6967 | if (*v == NULL) |
| 6968 | goto error; |
| 6969 | startout = PyUnicode_AS_UNICODE(*v); |
| 6970 | } |
| 6971 | else { |
| 6972 | /* Extend unicode object */ |
| 6973 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6974 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6975 | PyErr_NoMemory(); |
| 6976 | goto error; |
| 6977 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6978 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | goto error; |
| 6980 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6981 | } |
| 6982 | |
| 6983 | /* Decode the byte string character per character */ |
| 6984 | out = startout; |
| 6985 | while (in < endin) |
| 6986 | { |
| 6987 | /* Decode a character */ |
| 6988 | insize = 1; |
| 6989 | do |
| 6990 | { |
| 6991 | outsize = MultiByteToWideChar(code_page, flags, |
| 6992 | in, insize, |
| 6993 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6994 | if (outsize > 0) |
| 6995 | break; |
| 6996 | err = GetLastError(); |
| 6997 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6998 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6999 | { |
| 7000 | PyErr_SetFromWindowsErr(0); |
| 7001 | goto error; |
| 7002 | } |
| 7003 | insize++; |
| 7004 | } |
| 7005 | /* 4=maximum length of a UTF-8 sequence */ |
| 7006 | while (insize <= 4 && (in + insize) <= endin); |
| 7007 | |
| 7008 | if (outsize <= 0) { |
| 7009 | Py_ssize_t startinpos, endinpos, outpos; |
| 7010 | |
| 7011 | startinpos = in - startin; |
| 7012 | endinpos = startinpos + 1; |
| 7013 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7014 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7015 | errors, &errorHandler, |
| 7016 | encoding, reason, |
| 7017 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7018 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7019 | { |
| 7020 | goto error; |
| 7021 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7022 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7023 | } |
| 7024 | else { |
| 7025 | in += insize; |
| 7026 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7027 | out += outsize; |
| 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | /* write a NUL character at the end */ |
| 7032 | *out = 0; |
| 7033 | |
| 7034 | /* Extend unicode object */ |
| 7035 | outsize = out - startout; |
| 7036 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7037 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7038 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7039 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7040 | |
| 7041 | error: |
| 7042 | Py_XDECREF(encoding_obj); |
| 7043 | Py_XDECREF(errorHandler); |
| 7044 | Py_XDECREF(exc); |
| 7045 | return ret; |
| 7046 | } |
| 7047 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | static PyObject * |
| 7049 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7050 | const char *s, Py_ssize_t size, |
| 7051 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7052 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7053 | PyObject *v = NULL; |
| 7054 | int chunk_size, final, converted, done; |
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 | if (code_page < 0) { |
| 7057 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7058 | return NULL; |
| 7059 | } |
| 7060 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7062 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7063 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7064 | do |
| 7065 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7066 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7067 | if (size > INT_MAX) { |
| 7068 | chunk_size = INT_MAX; |
| 7069 | final = 0; |
| 7070 | done = 0; |
| 7071 | } |
| 7072 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7073 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7074 | { |
| 7075 | chunk_size = (int)size; |
| 7076 | final = (consumed == NULL); |
| 7077 | done = 1; |
| 7078 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7080 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7081 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7082 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7083 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7084 | if (chunk_size == 0 && done) { |
| 7085 | if (v != NULL) |
| 7086 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7087 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7088 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7089 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7090 | |
| 7091 | converted = decode_code_page_strict(code_page, &v, |
| 7092 | s, chunk_size); |
| 7093 | if (converted == -2) |
| 7094 | converted = decode_code_page_errors(code_page, &v, |
| 7095 | s, chunk_size, |
| 7096 | errors); |
| 7097 | assert(converted != 0); |
| 7098 | |
| 7099 | if (converted < 0) { |
| 7100 | Py_XDECREF(v); |
| 7101 | return NULL; |
| 7102 | } |
| 7103 | |
| 7104 | if (consumed) |
| 7105 | *consumed += converted; |
| 7106 | |
| 7107 | s += converted; |
| 7108 | size -= converted; |
| 7109 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7111 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7114 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7115 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7116 | const char *s, |
| 7117 | Py_ssize_t size, |
| 7118 | const char *errors, |
| 7119 | Py_ssize_t *consumed) |
| 7120 | { |
| 7121 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7122 | } |
| 7123 | |
| 7124 | PyObject * |
| 7125 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7126 | Py_ssize_t size, |
| 7127 | const char *errors, |
| 7128 | Py_ssize_t *consumed) |
| 7129 | { |
| 7130 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7131 | } |
| 7132 | |
| 7133 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7134 | PyUnicode_DecodeMBCS(const char *s, |
| 7135 | Py_ssize_t size, |
| 7136 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7137 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7138 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7139 | } |
| 7140 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7141 | static DWORD |
| 7142 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7143 | { |
| 7144 | if (code_page == CP_UTF8) { |
| 7145 | if (winver.dwMajorVersion >= 6) |
| 7146 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7147 | and later */ |
| 7148 | return WC_ERR_INVALID_CHARS; |
| 7149 | else |
| 7150 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7151 | return 0; |
| 7152 | } |
| 7153 | else if (code_page == CP_UTF7) { |
| 7154 | /* CP_UTF7 only supports flags=0 */ |
| 7155 | return 0; |
| 7156 | } |
| 7157 | else { |
| 7158 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7159 | return 0; |
| 7160 | else |
| 7161 | return WC_NO_BEST_FIT_CHARS; |
| 7162 | } |
| 7163 | } |
| 7164 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7165 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7166 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7167 | * mode. |
| 7168 | * |
| 7169 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7170 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7171 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7172 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7173 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7174 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7175 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7176 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7177 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7179 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7180 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7181 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7182 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7184 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7185 | /* Create a substring so that we can get the UTF-16 representation |
| 7186 | of just the slice under consideration. */ |
| 7187 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7188 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7189 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7190 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7191 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7192 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7193 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7194 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7195 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7196 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7197 | if (substring == NULL) |
| 7198 | return -1; |
| 7199 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7200 | if (p == NULL) { |
| 7201 | Py_DECREF(substring); |
| 7202 | return -1; |
| 7203 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7204 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7205 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7206 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7208 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7209 | NULL, 0, |
| 7210 | NULL, pusedDefaultChar); |
| 7211 | if (outsize <= 0) |
| 7212 | goto error; |
| 7213 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7214 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7215 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7217 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7218 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7219 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7220 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7221 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7222 | if (*outbytes == NULL) { |
| 7223 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7225 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7226 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7227 | } |
| 7228 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7229 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7230 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7231 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7232 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7233 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7235 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7236 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7237 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7238 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7239 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7240 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7244 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7245 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7246 | out, outsize, |
| 7247 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7248 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7249 | if (outsize <= 0) |
| 7250 | goto error; |
| 7251 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7252 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7253 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7254 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7256 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7257 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7258 | return -2; |
| 7259 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7260 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7261 | } |
| 7262 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7263 | /* |
| 7264 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7265 | * error handler. |
| 7266 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7267 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | * -1 on other error. |
| 7269 | */ |
| 7270 | static int |
| 7271 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7272 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7273 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7274 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7275 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7276 | Py_ssize_t pos = unicode_offset; |
| 7277 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7279 | 2000 English version of the message. */ |
| 7280 | const char *reason = "invalid character"; |
| 7281 | /* 4=maximum length of a UTF-8 sequence */ |
| 7282 | char buffer[4]; |
| 7283 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7284 | Py_ssize_t outsize; |
| 7285 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | PyObject *errorHandler = NULL; |
| 7287 | PyObject *exc = NULL; |
| 7288 | PyObject *encoding_obj = NULL; |
| 7289 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7290 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7291 | PyObject *rep; |
| 7292 | int ret = -1; |
| 7293 | |
| 7294 | assert(insize > 0); |
| 7295 | |
| 7296 | encoding = code_page_name(code_page, &encoding_obj); |
| 7297 | if (encoding == NULL) |
| 7298 | return -1; |
| 7299 | |
| 7300 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7301 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7302 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7303 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7304 | if (exc != NULL) { |
| 7305 | PyCodec_StrictErrors(exc); |
| 7306 | Py_DECREF(exc); |
| 7307 | } |
| 7308 | Py_XDECREF(encoding_obj); |
| 7309 | return -1; |
| 7310 | } |
| 7311 | |
| 7312 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7313 | pusedDefaultChar = &usedDefaultChar; |
| 7314 | else |
| 7315 | pusedDefaultChar = NULL; |
| 7316 | |
| 7317 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7318 | PyErr_NoMemory(); |
| 7319 | goto error; |
| 7320 | } |
| 7321 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7322 | |
| 7323 | if (*outbytes == NULL) { |
| 7324 | /* Create string object */ |
| 7325 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7326 | if (*outbytes == NULL) |
| 7327 | goto error; |
| 7328 | out = PyBytes_AS_STRING(*outbytes); |
| 7329 | } |
| 7330 | else { |
| 7331 | /* Extend string object */ |
| 7332 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7333 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7334 | PyErr_NoMemory(); |
| 7335 | goto error; |
| 7336 | } |
| 7337 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7338 | goto error; |
| 7339 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7340 | } |
| 7341 | |
| 7342 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7343 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7344 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7345 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7346 | wchar_t chars[2]; |
| 7347 | int charsize; |
| 7348 | if (ch < 0x10000) { |
| 7349 | chars[0] = (wchar_t)ch; |
| 7350 | charsize = 1; |
| 7351 | } |
| 7352 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7353 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7354 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7355 | charsize = 2; |
| 7356 | } |
| 7357 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7358 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7359 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7360 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7361 | NULL, pusedDefaultChar); |
| 7362 | if (outsize > 0) { |
| 7363 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7364 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7365 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7366 | memcpy(out, buffer, outsize); |
| 7367 | out += outsize; |
| 7368 | continue; |
| 7369 | } |
| 7370 | } |
| 7371 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7372 | PyErr_SetFromWindowsErr(0); |
| 7373 | goto error; |
| 7374 | } |
| 7375 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7376 | rep = unicode_encode_call_errorhandler( |
| 7377 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7378 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7379 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | if (rep == NULL) |
| 7381 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7382 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | |
| 7384 | if (PyBytes_Check(rep)) { |
| 7385 | outsize = PyBytes_GET_SIZE(rep); |
| 7386 | if (outsize != 1) { |
| 7387 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7388 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7389 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7390 | Py_DECREF(rep); |
| 7391 | goto error; |
| 7392 | } |
| 7393 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7394 | } |
| 7395 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7396 | out += outsize; |
| 7397 | } |
| 7398 | else { |
| 7399 | Py_ssize_t i; |
| 7400 | enum PyUnicode_Kind kind; |
| 7401 | void *data; |
| 7402 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7403 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7404 | Py_DECREF(rep); |
| 7405 | goto error; |
| 7406 | } |
| 7407 | |
| 7408 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7409 | if (outsize != 1) { |
| 7410 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7411 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7412 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7413 | Py_DECREF(rep); |
| 7414 | goto error; |
| 7415 | } |
| 7416 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7417 | } |
| 7418 | kind = PyUnicode_KIND(rep); |
| 7419 | data = PyUnicode_DATA(rep); |
| 7420 | for (i=0; i < outsize; i++) { |
| 7421 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7422 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7423 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7424 | encoding, unicode, |
| 7425 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7426 | "unable to encode error handler result to ASCII"); |
| 7427 | Py_DECREF(rep); |
| 7428 | goto error; |
| 7429 | } |
| 7430 | *out = (unsigned char)ch; |
| 7431 | out++; |
| 7432 | } |
| 7433 | } |
| 7434 | Py_DECREF(rep); |
| 7435 | } |
| 7436 | /* write a NUL byte */ |
| 7437 | *out = 0; |
| 7438 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7439 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7440 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7441 | goto error; |
| 7442 | ret = 0; |
| 7443 | |
| 7444 | error: |
| 7445 | Py_XDECREF(encoding_obj); |
| 7446 | Py_XDECREF(errorHandler); |
| 7447 | Py_XDECREF(exc); |
| 7448 | return ret; |
| 7449 | } |
| 7450 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7451 | static PyObject * |
| 7452 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7453 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7454 | const char *errors) |
| 7455 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7456 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7457 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7458 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7459 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7460 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7461 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7462 | return NULL; |
| 7463 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7464 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7465 | if (code_page < 0) { |
| 7466 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7467 | return NULL; |
| 7468 | } |
| 7469 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7470 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7471 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7472 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7473 | offset = 0; |
| 7474 | do |
| 7475 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7476 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7477 | /* 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] | 7478 | chunks. */ |
| 7479 | if (len > INT_MAX/2) { |
| 7480 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7481 | done = 0; |
| 7482 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7483 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7484 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7485 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7486 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7487 | done = 1; |
| 7488 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7489 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7490 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7491 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7492 | errors); |
| 7493 | if (ret == -2) |
| 7494 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7495 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7496 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7497 | if (ret < 0) { |
| 7498 | Py_XDECREF(outbytes); |
| 7499 | return NULL; |
| 7500 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7501 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7502 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7503 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7504 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7505 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7506 | return outbytes; |
| 7507 | } |
| 7508 | |
| 7509 | PyObject * |
| 7510 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7511 | Py_ssize_t size, |
| 7512 | const char *errors) |
| 7513 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7514 | PyObject *unicode, *res; |
| 7515 | unicode = PyUnicode_FromUnicode(p, size); |
| 7516 | if (unicode == NULL) |
| 7517 | return NULL; |
| 7518 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7519 | Py_DECREF(unicode); |
| 7520 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7521 | } |
| 7522 | |
| 7523 | PyObject * |
| 7524 | PyUnicode_EncodeCodePage(int code_page, |
| 7525 | PyObject *unicode, |
| 7526 | const char *errors) |
| 7527 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7528 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7529 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7530 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7531 | PyObject * |
| 7532 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7533 | { |
| 7534 | if (!PyUnicode_Check(unicode)) { |
| 7535 | PyErr_BadArgument(); |
| 7536 | return NULL; |
| 7537 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7538 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7539 | } |
| 7540 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7541 | #undef NEED_RETRY |
| 7542 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7543 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7545 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7546 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7547 | static int |
| 7548 | charmap_decode_string(const char *s, |
| 7549 | Py_ssize_t size, |
| 7550 | PyObject *mapping, |
| 7551 | const char *errors, |
| 7552 | _PyUnicodeWriter *writer) |
| 7553 | { |
| 7554 | const char *starts = s; |
| 7555 | const char *e; |
| 7556 | Py_ssize_t startinpos, endinpos; |
| 7557 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7558 | Py_ssize_t maplen; |
| 7559 | enum PyUnicode_Kind mapkind; |
| 7560 | void *mapdata; |
| 7561 | Py_UCS4 x; |
| 7562 | unsigned char ch; |
| 7563 | |
| 7564 | if (PyUnicode_READY(mapping) == -1) |
| 7565 | return -1; |
| 7566 | |
| 7567 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7568 | mapdata = PyUnicode_DATA(mapping); |
| 7569 | mapkind = PyUnicode_KIND(mapping); |
| 7570 | |
| 7571 | e = s + size; |
| 7572 | |
| 7573 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7574 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7575 | * is disabled in encoding aliases, latin1 is preferred because |
| 7576 | * its implementation is faster. */ |
| 7577 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7578 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7579 | Py_UCS4 maxchar = writer->maxchar; |
| 7580 | |
| 7581 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7582 | while (s < e) { |
| 7583 | ch = *s; |
| 7584 | x = mapdata_ucs1[ch]; |
| 7585 | if (x > maxchar) { |
| 7586 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7587 | goto onError; |
| 7588 | maxchar = writer->maxchar; |
| 7589 | outdata = (Py_UCS1 *)writer->data; |
| 7590 | } |
| 7591 | outdata[writer->pos] = x; |
| 7592 | writer->pos++; |
| 7593 | ++s; |
| 7594 | } |
| 7595 | return 0; |
| 7596 | } |
| 7597 | |
| 7598 | while (s < e) { |
| 7599 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7600 | enum PyUnicode_Kind outkind = writer->kind; |
| 7601 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7602 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7603 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7604 | Py_UCS4 maxchar = writer->maxchar; |
| 7605 | while (s < e) { |
| 7606 | ch = *s; |
| 7607 | x = mapdata_ucs2[ch]; |
| 7608 | if (x > maxchar) |
| 7609 | goto Error; |
| 7610 | outdata[writer->pos] = x; |
| 7611 | writer->pos++; |
| 7612 | ++s; |
| 7613 | } |
| 7614 | break; |
| 7615 | } |
| 7616 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7617 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7618 | while (s < e) { |
| 7619 | ch = *s; |
| 7620 | x = mapdata_ucs2[ch]; |
| 7621 | if (x == 0xFFFE) |
| 7622 | goto Error; |
| 7623 | outdata[writer->pos] = x; |
| 7624 | writer->pos++; |
| 7625 | ++s; |
| 7626 | } |
| 7627 | break; |
| 7628 | } |
| 7629 | } |
| 7630 | ch = *s; |
| 7631 | |
| 7632 | if (ch < maplen) |
| 7633 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7634 | else |
| 7635 | x = 0xfffe; /* invalid value */ |
| 7636 | Error: |
| 7637 | if (x == 0xfffe) |
| 7638 | { |
| 7639 | /* undefined mapping */ |
| 7640 | startinpos = s-starts; |
| 7641 | endinpos = startinpos+1; |
| 7642 | if (unicode_decode_call_errorhandler_writer( |
| 7643 | errors, &errorHandler, |
| 7644 | "charmap", "character maps to <undefined>", |
| 7645 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7646 | writer)) { |
| 7647 | goto onError; |
| 7648 | } |
| 7649 | continue; |
| 7650 | } |
| 7651 | |
| 7652 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7653 | goto onError; |
| 7654 | ++s; |
| 7655 | } |
| 7656 | Py_XDECREF(errorHandler); |
| 7657 | Py_XDECREF(exc); |
| 7658 | return 0; |
| 7659 | |
| 7660 | onError: |
| 7661 | Py_XDECREF(errorHandler); |
| 7662 | Py_XDECREF(exc); |
| 7663 | return -1; |
| 7664 | } |
| 7665 | |
| 7666 | static int |
| 7667 | charmap_decode_mapping(const char *s, |
| 7668 | Py_ssize_t size, |
| 7669 | PyObject *mapping, |
| 7670 | const char *errors, |
| 7671 | _PyUnicodeWriter *writer) |
| 7672 | { |
| 7673 | const char *starts = s; |
| 7674 | const char *e; |
| 7675 | Py_ssize_t startinpos, endinpos; |
| 7676 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7677 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7678 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7679 | |
| 7680 | e = s + size; |
| 7681 | |
| 7682 | while (s < e) { |
| 7683 | ch = *s; |
| 7684 | |
| 7685 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7686 | key = PyLong_FromLong((long)ch); |
| 7687 | if (key == NULL) |
| 7688 | goto onError; |
| 7689 | |
| 7690 | item = PyObject_GetItem(mapping, key); |
| 7691 | Py_DECREF(key); |
| 7692 | if (item == NULL) { |
| 7693 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7694 | /* No mapping found means: mapping is undefined. */ |
| 7695 | PyErr_Clear(); |
| 7696 | goto Undefined; |
| 7697 | } else |
| 7698 | goto onError; |
| 7699 | } |
| 7700 | |
| 7701 | /* Apply mapping */ |
| 7702 | if (item == Py_None) |
| 7703 | goto Undefined; |
| 7704 | if (PyLong_Check(item)) { |
| 7705 | long value = PyLong_AS_LONG(item); |
| 7706 | if (value == 0xFFFE) |
| 7707 | goto Undefined; |
| 7708 | if (value < 0 || value > MAX_UNICODE) { |
| 7709 | PyErr_Format(PyExc_TypeError, |
| 7710 | "character mapping must be in range(0x%lx)", |
| 7711 | (unsigned long)MAX_UNICODE + 1); |
| 7712 | goto onError; |
| 7713 | } |
| 7714 | |
| 7715 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7716 | goto onError; |
| 7717 | } |
| 7718 | else if (PyUnicode_Check(item)) { |
| 7719 | if (PyUnicode_READY(item) == -1) |
| 7720 | goto onError; |
| 7721 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7722 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7723 | if (value == 0xFFFE) |
| 7724 | goto Undefined; |
| 7725 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7726 | goto onError; |
| 7727 | } |
| 7728 | else { |
| 7729 | writer->overallocate = 1; |
| 7730 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7731 | goto onError; |
| 7732 | } |
| 7733 | } |
| 7734 | else { |
| 7735 | /* wrong return value */ |
| 7736 | PyErr_SetString(PyExc_TypeError, |
| 7737 | "character mapping must return integer, None or str"); |
| 7738 | goto onError; |
| 7739 | } |
| 7740 | Py_CLEAR(item); |
| 7741 | ++s; |
| 7742 | continue; |
| 7743 | |
| 7744 | Undefined: |
| 7745 | /* undefined mapping */ |
| 7746 | Py_CLEAR(item); |
| 7747 | startinpos = s-starts; |
| 7748 | endinpos = startinpos+1; |
| 7749 | if (unicode_decode_call_errorhandler_writer( |
| 7750 | errors, &errorHandler, |
| 7751 | "charmap", "character maps to <undefined>", |
| 7752 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7753 | writer)) { |
| 7754 | goto onError; |
| 7755 | } |
| 7756 | } |
| 7757 | Py_XDECREF(errorHandler); |
| 7758 | Py_XDECREF(exc); |
| 7759 | return 0; |
| 7760 | |
| 7761 | onError: |
| 7762 | Py_XDECREF(item); |
| 7763 | Py_XDECREF(errorHandler); |
| 7764 | Py_XDECREF(exc); |
| 7765 | return -1; |
| 7766 | } |
| 7767 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7768 | PyObject * |
| 7769 | PyUnicode_DecodeCharmap(const char *s, |
| 7770 | Py_ssize_t size, |
| 7771 | PyObject *mapping, |
| 7772 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7774 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | /* Default to Latin-1 */ |
| 7777 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7778 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7781 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7782 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7783 | writer.min_length = size; |
| 7784 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7786 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7787 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7788 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7789 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7790 | } |
| 7791 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7792 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7793 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7795 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7798 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | return NULL; |
| 7800 | } |
| 7801 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7802 | /* Charmap encoding: the lookup table */ |
| 7803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7804 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7805 | PyObject_HEAD |
| 7806 | unsigned char level1[32]; |
| 7807 | int count2, count3; |
| 7808 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7809 | }; |
| 7810 | |
| 7811 | static PyObject* |
| 7812 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7813 | { |
| 7814 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7815 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7816 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
| 7819 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7820 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7822 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7823 | }; |
| 7824 | |
| 7825 | static void |
| 7826 | encoding_map_dealloc(PyObject* o) |
| 7827 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7828 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7829 | } |
| 7830 | |
| 7831 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7832 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | "EncodingMap", /*tp_name*/ |
| 7834 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7835 | 0, /*tp_itemsize*/ |
| 7836 | /* methods */ |
| 7837 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7838 | 0, /*tp_print*/ |
| 7839 | 0, /*tp_getattr*/ |
| 7840 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7841 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7842 | 0, /*tp_repr*/ |
| 7843 | 0, /*tp_as_number*/ |
| 7844 | 0, /*tp_as_sequence*/ |
| 7845 | 0, /*tp_as_mapping*/ |
| 7846 | 0, /*tp_hash*/ |
| 7847 | 0, /*tp_call*/ |
| 7848 | 0, /*tp_str*/ |
| 7849 | 0, /*tp_getattro*/ |
| 7850 | 0, /*tp_setattro*/ |
| 7851 | 0, /*tp_as_buffer*/ |
| 7852 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7853 | 0, /*tp_doc*/ |
| 7854 | 0, /*tp_traverse*/ |
| 7855 | 0, /*tp_clear*/ |
| 7856 | 0, /*tp_richcompare*/ |
| 7857 | 0, /*tp_weaklistoffset*/ |
| 7858 | 0, /*tp_iter*/ |
| 7859 | 0, /*tp_iternext*/ |
| 7860 | encoding_map_methods, /*tp_methods*/ |
| 7861 | 0, /*tp_members*/ |
| 7862 | 0, /*tp_getset*/ |
| 7863 | 0, /*tp_base*/ |
| 7864 | 0, /*tp_dict*/ |
| 7865 | 0, /*tp_descr_get*/ |
| 7866 | 0, /*tp_descr_set*/ |
| 7867 | 0, /*tp_dictoffset*/ |
| 7868 | 0, /*tp_init*/ |
| 7869 | 0, /*tp_alloc*/ |
| 7870 | 0, /*tp_new*/ |
| 7871 | 0, /*tp_free*/ |
| 7872 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7873 | }; |
| 7874 | |
| 7875 | PyObject* |
| 7876 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7877 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7878 | PyObject *result; |
| 7879 | struct encoding_map *mresult; |
| 7880 | int i; |
| 7881 | int need_dict = 0; |
| 7882 | unsigned char level1[32]; |
| 7883 | unsigned char level2[512]; |
| 7884 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7885 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7886 | int kind; |
| 7887 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7888 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7889 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7890 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7891 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7892 | PyErr_BadArgument(); |
| 7893 | return NULL; |
| 7894 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | kind = PyUnicode_KIND(string); |
| 7896 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7897 | length = PyUnicode_GET_LENGTH(string); |
| 7898 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 | memset(level1, 0xFF, sizeof level1); |
| 7900 | memset(level2, 0xFF, sizeof level2); |
| 7901 | |
| 7902 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7903 | or if there are non-BMP characters, we need to use |
| 7904 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7905 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7906 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7907 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7908 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7909 | ch = PyUnicode_READ(kind, data, i); |
| 7910 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7911 | need_dict = 1; |
| 7912 | break; |
| 7913 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7914 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7915 | /* unmapped character */ |
| 7916 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | l1 = ch >> 11; |
| 7918 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7919 | if (level1[l1] == 0xFF) |
| 7920 | level1[l1] = count2++; |
| 7921 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7922 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7923 | } |
| 7924 | |
| 7925 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7926 | need_dict = 1; |
| 7927 | |
| 7928 | if (need_dict) { |
| 7929 | PyObject *result = PyDict_New(); |
| 7930 | PyObject *key, *value; |
| 7931 | if (!result) |
| 7932 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7933 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7934 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7935 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7936 | if (!key || !value) |
| 7937 | goto failed1; |
| 7938 | if (PyDict_SetItem(result, key, value) == -1) |
| 7939 | goto failed1; |
| 7940 | Py_DECREF(key); |
| 7941 | Py_DECREF(value); |
| 7942 | } |
| 7943 | return result; |
| 7944 | failed1: |
| 7945 | Py_XDECREF(key); |
| 7946 | Py_XDECREF(value); |
| 7947 | Py_DECREF(result); |
| 7948 | return NULL; |
| 7949 | } |
| 7950 | |
| 7951 | /* Create a three-level trie */ |
| 7952 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7953 | 16*count2 + 128*count3 - 1); |
| 7954 | if (!result) |
| 7955 | return PyErr_NoMemory(); |
| 7956 | PyObject_Init(result, &EncodingMapType); |
| 7957 | mresult = (struct encoding_map*)result; |
| 7958 | mresult->count2 = count2; |
| 7959 | mresult->count3 = count3; |
| 7960 | mlevel1 = mresult->level1; |
| 7961 | mlevel2 = mresult->level23; |
| 7962 | mlevel3 = mresult->level23 + 16*count2; |
| 7963 | memcpy(mlevel1, level1, 32); |
| 7964 | memset(mlevel2, 0xFF, 16*count2); |
| 7965 | memset(mlevel3, 0, 128*count3); |
| 7966 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7967 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7968 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7969 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7970 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7971 | /* unmapped character */ |
| 7972 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7973 | o1 = ch>>11; |
| 7974 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7975 | i2 = 16*mlevel1[o1] + o2; |
| 7976 | if (mlevel2[i2] == 0xFF) |
| 7977 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7978 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7979 | i3 = 128*mlevel2[i2] + o3; |
| 7980 | mlevel3[i3] = i; |
| 7981 | } |
| 7982 | return result; |
| 7983 | } |
| 7984 | |
| 7985 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7986 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7987 | { |
| 7988 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7989 | int l1 = c>>11; |
| 7990 | int l2 = (c>>7) & 0xF; |
| 7991 | int l3 = c & 0x7F; |
| 7992 | int i; |
| 7993 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7994 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7996 | if (c == 0) |
| 7997 | return 0; |
| 7998 | /* level 1*/ |
| 7999 | i = map->level1[l1]; |
| 8000 | if (i == 0xFF) { |
| 8001 | return -1; |
| 8002 | } |
| 8003 | /* level 2*/ |
| 8004 | i = map->level23[16*i+l2]; |
| 8005 | if (i == 0xFF) { |
| 8006 | return -1; |
| 8007 | } |
| 8008 | /* level 3 */ |
| 8009 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8010 | if (i == 0) { |
| 8011 | return -1; |
| 8012 | } |
| 8013 | return i; |
| 8014 | } |
| 8015 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8016 | /* Lookup the character ch in the mapping. If the character |
| 8017 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8018 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8019 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8020 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8022 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8023 | PyObject *x; |
| 8024 | |
| 8025 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8027 | x = PyObject_GetItem(mapping, w); |
| 8028 | Py_DECREF(w); |
| 8029 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8031 | /* No mapping found means: mapping is undefined. */ |
| 8032 | PyErr_Clear(); |
| 8033 | x = Py_None; |
| 8034 | Py_INCREF(x); |
| 8035 | return x; |
| 8036 | } else |
| 8037 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8038 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8039 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8041 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | long value = PyLong_AS_LONG(x); |
| 8043 | if (value < 0 || value > 255) { |
| 8044 | PyErr_SetString(PyExc_TypeError, |
| 8045 | "character mapping must be in range(256)"); |
| 8046 | Py_DECREF(x); |
| 8047 | return NULL; |
| 8048 | } |
| 8049 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8051 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8054 | /* wrong return value */ |
| 8055 | PyErr_Format(PyExc_TypeError, |
| 8056 | "character mapping must return integer, bytes or None, not %.400s", |
| 8057 | x->ob_type->tp_name); |
| 8058 | Py_DECREF(x); |
| 8059 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | } |
| 8061 | } |
| 8062 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8063 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8064 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8065 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8066 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8067 | /* exponentially overallocate to minimize reallocations */ |
| 8068 | if (requiredsize < 2*outsize) |
| 8069 | requiredsize = 2*outsize; |
| 8070 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8071 | return -1; |
| 8072 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8075 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8077 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8078 | /* 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] | 8079 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8080 | space is available. Return a new reference to the object that |
| 8081 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8082 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8083 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8084 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8085 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8086 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8087 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8088 | PyObject *rep; |
| 8089 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8090 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8091 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8092 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8093 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8094 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8095 | if (res == -1) |
| 8096 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8097 | if (outsize<requiredsize) |
| 8098 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8099 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8100 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8101 | outstart[(*outpos)++] = (char)res; |
| 8102 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8103 | } |
| 8104 | |
| 8105 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8106 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8108 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | Py_DECREF(rep); |
| 8110 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8111 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | if (PyLong_Check(rep)) { |
| 8113 | Py_ssize_t requiredsize = *outpos+1; |
| 8114 | if (outsize<requiredsize) |
| 8115 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8116 | Py_DECREF(rep); |
| 8117 | return enc_EXCEPTION; |
| 8118 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8119 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8121 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | else { |
| 8123 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8124 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8125 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8126 | if (outsize<requiredsize) |
| 8127 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8128 | Py_DECREF(rep); |
| 8129 | return enc_EXCEPTION; |
| 8130 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8131 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | memcpy(outstart + *outpos, repchars, repsize); |
| 8133 | *outpos += repsize; |
| 8134 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8135 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8136 | Py_DECREF(rep); |
| 8137 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8138 | } |
| 8139 | |
| 8140 | /* handle an error in PyUnicode_EncodeCharmap |
| 8141 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8142 | static int |
| 8143 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8144 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8145 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8146 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8147 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8148 | { |
| 8149 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8150 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8151 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8152 | enum PyUnicode_Kind kind; |
| 8153 | void *data; |
| 8154 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8155 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8156 | Py_ssize_t collstartpos = *inpos; |
| 8157 | Py_ssize_t collendpos = *inpos+1; |
| 8158 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8159 | char *encoding = "charmap"; |
| 8160 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8161 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8162 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8163 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +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 -1; |
| 8167 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8168 | /* find all unencodable characters */ |
| 8169 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8170 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8171 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8172 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8173 | val = encoding_map_lookup(ch, mapping); |
| 8174 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | break; |
| 8176 | ++collendpos; |
| 8177 | continue; |
| 8178 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8179 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8180 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8181 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8182 | if (rep==NULL) |
| 8183 | return -1; |
| 8184 | else if (rep!=Py_None) { |
| 8185 | Py_DECREF(rep); |
| 8186 | break; |
| 8187 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8188 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8190 | } |
| 8191 | /* cache callback name lookup |
| 8192 | * (if not done yet, i.e. it's the first error) */ |
| 8193 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8194 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8195 | *known_errorHandler = 1; |
| 8196 | else if (!strcmp(errors, "replace")) |
| 8197 | *known_errorHandler = 2; |
| 8198 | else if (!strcmp(errors, "ignore")) |
| 8199 | *known_errorHandler = 3; |
| 8200 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8201 | *known_errorHandler = 4; |
| 8202 | else |
| 8203 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8204 | } |
| 8205 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8206 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8207 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8208 | return -1; |
| 8209 | case 2: /* replace */ |
| 8210 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8211 | x = charmapencode_output('?', mapping, res, respos); |
| 8212 | if (x==enc_EXCEPTION) { |
| 8213 | return -1; |
| 8214 | } |
| 8215 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8216 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | return -1; |
| 8218 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8219 | } |
| 8220 | /* fall through */ |
| 8221 | case 3: /* ignore */ |
| 8222 | *inpos = collendpos; |
| 8223 | break; |
| 8224 | case 4: /* xmlcharrefreplace */ |
| 8225 | /* generate replacement (temporarily (mis)uses p) */ |
| 8226 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | char buffer[2+29+1+1]; |
| 8228 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8229 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | for (cp = buffer; *cp; ++cp) { |
| 8231 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8232 | if (x==enc_EXCEPTION) |
| 8233 | return -1; |
| 8234 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8235 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8236 | return -1; |
| 8237 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8238 | } |
| 8239 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8240 | *inpos = collendpos; |
| 8241 | break; |
| 8242 | default: |
| 8243 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8244 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8246 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8247 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8248 | if (PyBytes_Check(repunicode)) { |
| 8249 | /* Directly copy bytes result to output. */ |
| 8250 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8251 | Py_ssize_t requiredsize; |
| 8252 | repsize = PyBytes_Size(repunicode); |
| 8253 | requiredsize = *respos + repsize; |
| 8254 | if (requiredsize > outsize) |
| 8255 | /* Make room for all additional bytes. */ |
| 8256 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8257 | Py_DECREF(repunicode); |
| 8258 | return -1; |
| 8259 | } |
| 8260 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8261 | PyBytes_AsString(repunicode), repsize); |
| 8262 | *respos += repsize; |
| 8263 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8264 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8265 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8266 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8267 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8268 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8269 | Py_DECREF(repunicode); |
| 8270 | return -1; |
| 8271 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8272 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8273 | data = PyUnicode_DATA(repunicode); |
| 8274 | kind = PyUnicode_KIND(repunicode); |
| 8275 | for (index = 0; index < repsize; index++) { |
| 8276 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8277 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8279 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8280 | return -1; |
| 8281 | } |
| 8282 | else if (x==enc_FAILED) { |
| 8283 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8284 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | return -1; |
| 8286 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8287 | } |
| 8288 | *inpos = newpos; |
| 8289 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8290 | } |
| 8291 | return 0; |
| 8292 | } |
| 8293 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8294 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8295 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8296 | PyObject *mapping, |
| 8297 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8298 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8299 | /* output object */ |
| 8300 | PyObject *res = NULL; |
| 8301 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8302 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8303 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8304 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8305 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | PyObject *errorHandler = NULL; |
| 8307 | PyObject *exc = NULL; |
| 8308 | /* the following variable is used for caching string comparisons |
| 8309 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8310 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8311 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8312 | void *data; |
| 8313 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8315 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8316 | return NULL; |
| 8317 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8318 | data = PyUnicode_DATA(unicode); |
| 8319 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | /* Default to Latin-1 */ |
| 8322 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8323 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8324 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8325 | /* allocate enough for a simple encoding without |
| 8326 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8327 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8328 | if (res == NULL) |
| 8329 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8330 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8333 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8334 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8336 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8337 | if (x==enc_EXCEPTION) /* error */ |
| 8338 | goto onError; |
| 8339 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8340 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8341 | &exc, |
| 8342 | &known_errorHandler, &errorHandler, errors, |
| 8343 | &res, &respos)) { |
| 8344 | goto onError; |
| 8345 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8346 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | else |
| 8348 | /* done with this character => adjust input position */ |
| 8349 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8350 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8353 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8354 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8355 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8356 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 | Py_XDECREF(exc); |
| 8358 | Py_XDECREF(errorHandler); |
| 8359 | return res; |
| 8360 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | Py_XDECREF(res); |
| 8363 | Py_XDECREF(exc); |
| 8364 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8365 | return NULL; |
| 8366 | } |
| 8367 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8368 | /* Deprecated */ |
| 8369 | PyObject * |
| 8370 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8371 | Py_ssize_t size, |
| 8372 | PyObject *mapping, |
| 8373 | const char *errors) |
| 8374 | { |
| 8375 | PyObject *result; |
| 8376 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8377 | if (unicode == NULL) |
| 8378 | return NULL; |
| 8379 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8380 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8381 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8382 | } |
| 8383 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8384 | PyObject * |
| 8385 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8386 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | { |
| 8388 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8389 | PyErr_BadArgument(); |
| 8390 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8391 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8392 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8393 | } |
| 8394 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8395 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8396 | static void |
| 8397 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8398 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8399 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8400 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8402 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8403 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8404 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | } |
| 8406 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8408 | goto onError; |
| 8409 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8410 | goto onError; |
| 8411 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8412 | goto onError; |
| 8413 | return; |
| 8414 | onError: |
| 8415 | Py_DECREF(*exceptionObject); |
| 8416 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8417 | } |
| 8418 | } |
| 8419 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | /* error handling callback helper: |
| 8421 | build arguments, call the callback and check the arguments, |
| 8422 | put the result into newpos and return the replacement string, which |
| 8423 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8424 | static PyObject * |
| 8425 | unicode_translate_call_errorhandler(const char *errors, |
| 8426 | PyObject **errorHandler, |
| 8427 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8428 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8429 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8430 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8432 | 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] | 8433 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8434 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8435 | PyObject *restuple; |
| 8436 | PyObject *resunicode; |
| 8437 | |
| 8438 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8440 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8442 | } |
| 8443 | |
| 8444 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8447 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8448 | |
| 8449 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8451 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8453 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8454 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8455 | Py_DECREF(restuple); |
| 8456 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | } |
| 8458 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8459 | &resunicode, &i_newpos)) { |
| 8460 | Py_DECREF(restuple); |
| 8461 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8462 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8463 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8465 | else |
| 8466 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8467 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8468 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8469 | Py_DECREF(restuple); |
| 8470 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8471 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | Py_INCREF(resunicode); |
| 8473 | Py_DECREF(restuple); |
| 8474 | return resunicode; |
| 8475 | } |
| 8476 | |
| 8477 | /* Lookup the character ch in the mapping and put the result in result, |
| 8478 | which must be decrefed by the caller. |
| 8479 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8480 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8482 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8483 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8484 | PyObject *x; |
| 8485 | |
| 8486 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | x = PyObject_GetItem(mapping, w); |
| 8489 | Py_DECREF(w); |
| 8490 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8492 | /* No mapping found means: use 1:1 mapping. */ |
| 8493 | PyErr_Clear(); |
| 8494 | *result = NULL; |
| 8495 | return 0; |
| 8496 | } else |
| 8497 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8498 | } |
| 8499 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | *result = x; |
| 8501 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8502 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8503 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | long value = PyLong_AS_LONG(x); |
| 8505 | long max = PyUnicode_GetMax(); |
| 8506 | if (value < 0 || value > max) { |
| 8507 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8508 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | Py_DECREF(x); |
| 8510 | return -1; |
| 8511 | } |
| 8512 | *result = x; |
| 8513 | return 0; |
| 8514 | } |
| 8515 | else if (PyUnicode_Check(x)) { |
| 8516 | *result = x; |
| 8517 | return 0; |
| 8518 | } |
| 8519 | else { |
| 8520 | /* wrong return value */ |
| 8521 | PyErr_SetString(PyExc_TypeError, |
| 8522 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8523 | Py_DECREF(x); |
| 8524 | return -1; |
| 8525 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8526 | } |
| 8527 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | if not reallocate and adjust various state variables. |
| 8529 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8530 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8533 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8534 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8535 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8536 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | /* exponentially overallocate to minimize reallocations */ |
| 8538 | if (requiredsize < 2 * oldsize) |
| 8539 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8540 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8541 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8542 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8543 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8544 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8545 | } |
| 8546 | return 0; |
| 8547 | } |
| 8548 | /* lookup the character, put the result in the output string and adjust |
| 8549 | various state variables. Return a new reference to the object that |
| 8550 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8551 | undefined (in which case no character was written). |
| 8552 | The called must decref result. |
| 8553 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8554 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8555 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8556 | PyObject *mapping, Py_UCS4 **output, |
| 8557 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8558 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8559 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8560 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8561 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8563 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8565 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8566 | } |
| 8567 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8569 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8570 | /* 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] | 8571 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8572 | } |
| 8573 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8574 | Py_ssize_t repsize; |
| 8575 | if (PyUnicode_READY(*res) == -1) |
| 8576 | return -1; |
| 8577 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8578 | if (repsize==1) { |
| 8579 | /* 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] | 8580 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8581 | } |
| 8582 | else if (repsize!=0) { |
| 8583 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8584 | Py_ssize_t requiredsize = *opos + |
| 8585 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8586 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8587 | Py_ssize_t i; |
| 8588 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8589 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | for(i = 0; i < repsize; i++) |
| 8591 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8592 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8593 | } |
| 8594 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8596 | return 0; |
| 8597 | } |
| 8598 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8599 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8600 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8601 | PyObject *mapping, |
| 8602 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | /* input object */ |
| 8605 | char *idata; |
| 8606 | Py_ssize_t size, i; |
| 8607 | int kind; |
| 8608 | /* output buffer */ |
| 8609 | Py_UCS4 *output = NULL; |
| 8610 | Py_ssize_t osize; |
| 8611 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8612 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8614 | char *reason = "character maps to <undefined>"; |
| 8615 | PyObject *errorHandler = NULL; |
| 8616 | PyObject *exc = NULL; |
| 8617 | /* the following variable is used for caching string comparisons |
| 8618 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8619 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8620 | int known_errorHandler = -1; |
| 8621 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8623 | PyErr_BadArgument(); |
| 8624 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8625 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8626 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | if (PyUnicode_READY(input) == -1) |
| 8628 | return NULL; |
| 8629 | idata = (char*)PyUnicode_DATA(input); |
| 8630 | kind = PyUnicode_KIND(input); |
| 8631 | size = PyUnicode_GET_LENGTH(input); |
| 8632 | i = 0; |
| 8633 | |
| 8634 | if (size == 0) { |
| 8635 | Py_INCREF(input); |
| 8636 | return input; |
| 8637 | } |
| 8638 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8639 | /* allocate enough for a simple 1:1 translation without |
| 8640 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | osize = size; |
| 8642 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8643 | opos = 0; |
| 8644 | if (output == NULL) { |
| 8645 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8646 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | /* try to encode it */ |
| 8651 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8652 | if (charmaptranslate_output(input, i, mapping, |
| 8653 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8654 | Py_XDECREF(x); |
| 8655 | goto onError; |
| 8656 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8657 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8658 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8660 | else { /* untranslatable character */ |
| 8661 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8662 | Py_ssize_t repsize; |
| 8663 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8665 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8666 | Py_ssize_t collstart = i; |
| 8667 | Py_ssize_t collend = i+1; |
| 8668 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8670 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | while (collend < size) { |
| 8672 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8673 | goto onError; |
| 8674 | Py_XDECREF(x); |
| 8675 | if (x!=Py_None) |
| 8676 | break; |
| 8677 | ++collend; |
| 8678 | } |
| 8679 | /* cache callback name lookup |
| 8680 | * (if not done yet, i.e. it's the first error) */ |
| 8681 | if (known_errorHandler==-1) { |
| 8682 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8683 | known_errorHandler = 1; |
| 8684 | else if (!strcmp(errors, "replace")) |
| 8685 | known_errorHandler = 2; |
| 8686 | else if (!strcmp(errors, "ignore")) |
| 8687 | known_errorHandler = 3; |
| 8688 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8689 | known_errorHandler = 4; |
| 8690 | else |
| 8691 | known_errorHandler = 0; |
| 8692 | } |
| 8693 | switch (known_errorHandler) { |
| 8694 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8695 | make_translate_exception(&exc, |
| 8696 | input, collstart, collend, reason); |
| 8697 | if (exc != NULL) |
| 8698 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8699 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8700 | case 2: /* replace */ |
| 8701 | /* 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] | 8702 | for (coll = collstart; coll<collend; coll++) |
| 8703 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8704 | /* fall through */ |
| 8705 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8706 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8707 | break; |
| 8708 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8709 | /* generate replacement (temporarily (mis)uses i) */ |
| 8710 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8711 | char buffer[2+29+1+1]; |
| 8712 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8714 | if (charmaptranslate_makespace(&output, &osize, |
| 8715 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8716 | goto onError; |
| 8717 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8720 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | break; |
| 8722 | default: |
| 8723 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | reason, input, &exc, |
| 8725 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8726 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8728 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8729 | Py_DECREF(repunicode); |
| 8730 | goto onError; |
| 8731 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8732 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8733 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8734 | if (charmaptranslate_makespace(&output, &osize, |
| 8735 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8736 | Py_DECREF(repunicode); |
| 8737 | goto onError; |
| 8738 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8740 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8741 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8742 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8743 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8744 | } |
| 8745 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8747 | if (!res) |
| 8748 | goto onError; |
| 8749 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8750 | Py_XDECREF(exc); |
| 8751 | Py_XDECREF(errorHandler); |
| 8752 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8754 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8755 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8756 | Py_XDECREF(exc); |
| 8757 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | return NULL; |
| 8759 | } |
| 8760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8761 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8762 | PyObject * |
| 8763 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8764 | Py_ssize_t size, |
| 8765 | PyObject *mapping, |
| 8766 | const char *errors) |
| 8767 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8768 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8769 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8770 | if (!unicode) |
| 8771 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8772 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8773 | Py_DECREF(unicode); |
| 8774 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8775 | } |
| 8776 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8777 | PyObject * |
| 8778 | PyUnicode_Translate(PyObject *str, |
| 8779 | PyObject *mapping, |
| 8780 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8781 | { |
| 8782 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | str = PyUnicode_FromObject(str); |
| 8785 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8786 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | Py_DECREF(str); |
| 8789 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8790 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8793 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | { |
| 8795 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8796 | called as a callback from fixup() which does it already. */ |
| 8797 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8798 | const int kind = PyUnicode_KIND(self); |
| 8799 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8800 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8801 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | Py_ssize_t i; |
| 8803 | |
| 8804 | for (i = 0; i < len; ++i) { |
| 8805 | ch = PyUnicode_READ(kind, data, i); |
| 8806 | fixed = 0; |
| 8807 | if (ch > 127) { |
| 8808 | if (Py_UNICODE_ISSPACE(ch)) |
| 8809 | fixed = ' '; |
| 8810 | else { |
| 8811 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8812 | if (decimal >= 0) |
| 8813 | fixed = '0' + decimal; |
| 8814 | } |
| 8815 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8816 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8817 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8819 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8820 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8821 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8823 | } |
| 8824 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8825 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8826 | } |
| 8827 | |
| 8828 | PyObject * |
| 8829 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8830 | { |
| 8831 | if (!PyUnicode_Check(unicode)) { |
| 8832 | PyErr_BadInternalCall(); |
| 8833 | return NULL; |
| 8834 | } |
| 8835 | if (PyUnicode_READY(unicode) == -1) |
| 8836 | return NULL; |
| 8837 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8838 | /* If the string is already ASCII, just return the same string */ |
| 8839 | Py_INCREF(unicode); |
| 8840 | return unicode; |
| 8841 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8842 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8843 | } |
| 8844 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8845 | PyObject * |
| 8846 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8847 | Py_ssize_t length) |
| 8848 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8849 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8850 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8851 | Py_UCS4 maxchar; |
| 8852 | enum PyUnicode_Kind kind; |
| 8853 | void *data; |
| 8854 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8855 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8856 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8857 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8858 | if (ch > 127) { |
| 8859 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8860 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8861 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8862 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8863 | } |
| 8864 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8865 | |
| 8866 | /* Copy to a new string */ |
| 8867 | decimal = PyUnicode_New(length, maxchar); |
| 8868 | if (decimal == NULL) |
| 8869 | return decimal; |
| 8870 | kind = PyUnicode_KIND(decimal); |
| 8871 | data = PyUnicode_DATA(decimal); |
| 8872 | /* Iterate over code points */ |
| 8873 | for (i = 0; i < length; i++) { |
| 8874 | Py_UNICODE ch = s[i]; |
| 8875 | if (ch > 127) { |
| 8876 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8877 | if (decimal >= 0) |
| 8878 | ch = '0' + decimal; |
| 8879 | } |
| 8880 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8882 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8883 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8884 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8886 | int |
| 8887 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8888 | Py_ssize_t length, |
| 8889 | char *output, |
| 8890 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8891 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8892 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8893 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8894 | enum PyUnicode_Kind kind; |
| 8895 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8896 | |
| 8897 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | PyErr_BadArgument(); |
| 8899 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8900 | } |
| 8901 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8902 | unicode = PyUnicode_FromUnicode(s, length); |
| 8903 | if (unicode == NULL) |
| 8904 | return -1; |
| 8905 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8906 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8907 | Py_DECREF(unicode); |
| 8908 | return -1; |
| 8909 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8910 | kind = PyUnicode_KIND(unicode); |
| 8911 | data = PyUnicode_DATA(unicode); |
| 8912 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8913 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8914 | PyObject *exc; |
| 8915 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8917 | Py_ssize_t startpos; |
| 8918 | |
| 8919 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8921 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8922 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8923 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8924 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8925 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8926 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8927 | if (decimal >= 0) { |
| 8928 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8929 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8930 | continue; |
| 8931 | } |
| 8932 | if (0 < ch && ch < 256) { |
| 8933 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8934 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | continue; |
| 8936 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8937 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8938 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8939 | exc = NULL; |
| 8940 | raise_encode_exception(&exc, "decimal", unicode, |
| 8941 | startpos, startpos+1, |
| 8942 | "invalid decimal Unicode string"); |
| 8943 | Py_XDECREF(exc); |
| 8944 | Py_DECREF(unicode); |
| 8945 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8946 | } |
| 8947 | /* 0-terminate the output string */ |
| 8948 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8949 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8950 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8953 | /* --- Helpers ------------------------------------------------------------ */ |
| 8954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8955 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8956 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8957 | Py_ssize_t start, |
| 8958 | Py_ssize_t end) |
| 8959 | { |
| 8960 | int kind1, kind2, kind; |
| 8961 | void *buf1, *buf2; |
| 8962 | Py_ssize_t len1, len2, result; |
| 8963 | |
| 8964 | kind1 = PyUnicode_KIND(s1); |
| 8965 | kind2 = PyUnicode_KIND(s2); |
| 8966 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8967 | buf1 = PyUnicode_DATA(s1); |
| 8968 | buf2 = PyUnicode_DATA(s2); |
| 8969 | if (kind1 != kind) |
| 8970 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8971 | if (!buf1) |
| 8972 | return -2; |
| 8973 | if (kind2 != kind) |
| 8974 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8975 | if (!buf2) { |
| 8976 | if (kind1 != kind) PyMem_Free(buf1); |
| 8977 | return -2; |
| 8978 | } |
| 8979 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8980 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8981 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8982 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8983 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8984 | case PyUnicode_1BYTE_KIND: |
| 8985 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8986 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8987 | else |
| 8988 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8989 | break; |
| 8990 | case PyUnicode_2BYTE_KIND: |
| 8991 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8992 | break; |
| 8993 | case PyUnicode_4BYTE_KIND: |
| 8994 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8995 | break; |
| 8996 | default: |
| 8997 | assert(0); result = -2; |
| 8998 | } |
| 8999 | } |
| 9000 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9001 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9002 | case PyUnicode_1BYTE_KIND: |
| 9003 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9004 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9005 | else |
| 9006 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9007 | break; |
| 9008 | case PyUnicode_2BYTE_KIND: |
| 9009 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9010 | break; |
| 9011 | case PyUnicode_4BYTE_KIND: |
| 9012 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9013 | break; |
| 9014 | default: |
| 9015 | assert(0); result = -2; |
| 9016 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9017 | } |
| 9018 | |
| 9019 | if (kind1 != kind) |
| 9020 | PyMem_Free(buf1); |
| 9021 | if (kind2 != kind) |
| 9022 | PyMem_Free(buf2); |
| 9023 | |
| 9024 | return result; |
| 9025 | } |
| 9026 | |
| 9027 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9028 | _PyUnicode_InsertThousandsGrouping( |
| 9029 | PyObject *unicode, Py_ssize_t index, |
| 9030 | Py_ssize_t n_buffer, |
| 9031 | void *digits, Py_ssize_t n_digits, |
| 9032 | Py_ssize_t min_width, |
| 9033 | const char *grouping, PyObject *thousands_sep, |
| 9034 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9035 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9036 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9037 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9038 | Py_ssize_t thousands_sep_len; |
| 9039 | Py_ssize_t len; |
| 9040 | |
| 9041 | if (unicode != NULL) { |
| 9042 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9043 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9044 | } |
| 9045 | else { |
| 9046 | kind = PyUnicode_1BYTE_KIND; |
| 9047 | data = NULL; |
| 9048 | } |
| 9049 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9050 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9051 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9052 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9053 | if (thousands_sep_kind < kind) { |
| 9054 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9055 | if (!thousands_sep_data) |
| 9056 | return -1; |
| 9057 | } |
| 9058 | else { |
| 9059 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9060 | if (!data) |
| 9061 | return -1; |
| 9062 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9063 | } |
| 9064 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9065 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9066 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9067 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9068 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9069 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9070 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9071 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9072 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9073 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9074 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9075 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9076 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9077 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9078 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9079 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9080 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9081 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9082 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9083 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9084 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9085 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9086 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9087 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9088 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9089 | break; |
| 9090 | default: |
| 9091 | assert(0); |
| 9092 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9094 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9095 | if (thousands_sep_kind < kind) |
| 9096 | PyMem_Free(thousands_sep_data); |
| 9097 | else |
| 9098 | PyMem_Free(data); |
| 9099 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9100 | if (unicode == NULL) { |
| 9101 | *maxchar = 127; |
| 9102 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9103 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9104 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9105 | } |
| 9106 | } |
| 9107 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9108 | } |
| 9109 | |
| 9110 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9111 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9112 | #define ADJUST_INDICES(start, end, len) \ |
| 9113 | if (end > len) \ |
| 9114 | end = len; \ |
| 9115 | else if (end < 0) { \ |
| 9116 | end += len; \ |
| 9117 | if (end < 0) \ |
| 9118 | end = 0; \ |
| 9119 | } \ |
| 9120 | if (start < 0) { \ |
| 9121 | start += len; \ |
| 9122 | if (start < 0) \ |
| 9123 | start = 0; \ |
| 9124 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9125 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9126 | Py_ssize_t |
| 9127 | PyUnicode_Count(PyObject *str, |
| 9128 | PyObject *substr, |
| 9129 | Py_ssize_t start, |
| 9130 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9131 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9132 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9133 | PyObject* str_obj; |
| 9134 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9135 | int kind1, kind2, kind; |
| 9136 | void *buf1 = NULL, *buf2 = NULL; |
| 9137 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9138 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9139 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9140 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9141 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9142 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9143 | if (!sub_obj) { |
| 9144 | Py_DECREF(str_obj); |
| 9145 | return -1; |
| 9146 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9147 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9148 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9149 | Py_DECREF(str_obj); |
| 9150 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9153 | kind1 = PyUnicode_KIND(str_obj); |
| 9154 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9155 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9156 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9157 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9158 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9159 | if (kind2 > kind) { |
| 9160 | Py_DECREF(sub_obj); |
| 9161 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9162 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9163 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9164 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9166 | if (!buf2) |
| 9167 | goto onError; |
| 9168 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9169 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9170 | |
| 9171 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9172 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9173 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9174 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9175 | result = asciilib_count( |
| 9176 | ((Py_UCS1*)buf1) + start, end - start, |
| 9177 | buf2, len2, PY_SSIZE_T_MAX |
| 9178 | ); |
| 9179 | else |
| 9180 | result = ucs1lib_count( |
| 9181 | ((Py_UCS1*)buf1) + start, end - start, |
| 9182 | buf2, len2, PY_SSIZE_T_MAX |
| 9183 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9184 | break; |
| 9185 | case PyUnicode_2BYTE_KIND: |
| 9186 | result = ucs2lib_count( |
| 9187 | ((Py_UCS2*)buf1) + start, end - start, |
| 9188 | buf2, len2, PY_SSIZE_T_MAX |
| 9189 | ); |
| 9190 | break; |
| 9191 | case PyUnicode_4BYTE_KIND: |
| 9192 | result = ucs4lib_count( |
| 9193 | ((Py_UCS4*)buf1) + start, end - start, |
| 9194 | buf2, len2, PY_SSIZE_T_MAX |
| 9195 | ); |
| 9196 | break; |
| 9197 | default: |
| 9198 | assert(0); result = 0; |
| 9199 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9200 | |
| 9201 | Py_DECREF(sub_obj); |
| 9202 | Py_DECREF(str_obj); |
| 9203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9204 | if (kind2 != kind) |
| 9205 | PyMem_Free(buf2); |
| 9206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9208 | onError: |
| 9209 | Py_DECREF(sub_obj); |
| 9210 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9211 | if (kind2 != kind && buf2) |
| 9212 | PyMem_Free(buf2); |
| 9213 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9214 | } |
| 9215 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9216 | Py_ssize_t |
| 9217 | PyUnicode_Find(PyObject *str, |
| 9218 | PyObject *sub, |
| 9219 | Py_ssize_t start, |
| 9220 | Py_ssize_t end, |
| 9221 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9222 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9223 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9226 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9227 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9228 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9229 | if (!sub) { |
| 9230 | Py_DECREF(str); |
| 9231 | return -2; |
| 9232 | } |
| 9233 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9234 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9235 | Py_DECREF(str); |
| 9236 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9237 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9238 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9239 | result = any_find_slice(direction, |
| 9240 | str, sub, start, end |
| 9241 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9243 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9244 | Py_DECREF(sub); |
| 9245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9246 | return result; |
| 9247 | } |
| 9248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9249 | Py_ssize_t |
| 9250 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9251 | Py_ssize_t start, Py_ssize_t end, |
| 9252 | int direction) |
| 9253 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9254 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9255 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | if (PyUnicode_READY(str) == -1) |
| 9257 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9258 | if (start < 0 || end < 0) { |
| 9259 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9260 | return -2; |
| 9261 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9262 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9263 | end = PyUnicode_GET_LENGTH(str); |
| 9264 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9265 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9266 | kind, end-start, ch, direction); |
| 9267 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9268 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9269 | else |
| 9270 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9271 | } |
| 9272 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9273 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9274 | tailmatch(PyObject *self, |
| 9275 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9276 | Py_ssize_t start, |
| 9277 | Py_ssize_t end, |
| 9278 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9279 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9280 | int kind_self; |
| 9281 | int kind_sub; |
| 9282 | void *data_self; |
| 9283 | void *data_sub; |
| 9284 | Py_ssize_t offset; |
| 9285 | Py_ssize_t i; |
| 9286 | Py_ssize_t end_sub; |
| 9287 | |
| 9288 | if (PyUnicode_READY(self) == -1 || |
| 9289 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9290 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9291 | |
| 9292 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | return 1; |
| 9294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9296 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9298 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | kind_self = PyUnicode_KIND(self); |
| 9301 | data_self = PyUnicode_DATA(self); |
| 9302 | kind_sub = PyUnicode_KIND(substring); |
| 9303 | data_sub = PyUnicode_DATA(substring); |
| 9304 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9305 | |
| 9306 | if (direction > 0) |
| 9307 | offset = end; |
| 9308 | else |
| 9309 | offset = start; |
| 9310 | |
| 9311 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9312 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9313 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9314 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9315 | /* If both are of the same kind, memcmp is sufficient */ |
| 9316 | if (kind_self == kind_sub) { |
| 9317 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9318 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | data_sub, |
| 9320 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9321 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9322 | } |
| 9323 | /* otherwise we have to compare each character by first accesing it */ |
| 9324 | else { |
| 9325 | /* We do not need to compare 0 and len(substring)-1 because |
| 9326 | the if statement above ensured already that they are equal |
| 9327 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | for (i = 1; i < end_sub; ++i) { |
| 9329 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9330 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9331 | return 0; |
| 9332 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9333 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9335 | } |
| 9336 | |
| 9337 | return 0; |
| 9338 | } |
| 9339 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9340 | Py_ssize_t |
| 9341 | PyUnicode_Tailmatch(PyObject *str, |
| 9342 | PyObject *substr, |
| 9343 | Py_ssize_t start, |
| 9344 | Py_ssize_t end, |
| 9345 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9346 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9347 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9349 | str = PyUnicode_FromObject(str); |
| 9350 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9351 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9352 | substr = PyUnicode_FromObject(substr); |
| 9353 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9354 | Py_DECREF(str); |
| 9355 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9356 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9357 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9358 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9359 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | Py_DECREF(str); |
| 9361 | Py_DECREF(substr); |
| 9362 | return result; |
| 9363 | } |
| 9364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | /* Apply fixfct filter to the Unicode object self and return a |
| 9366 | reference to the modified object */ |
| 9367 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9368 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9369 | fixup(PyObject *self, |
| 9370 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9371 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9372 | PyObject *u; |
| 9373 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9374 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9376 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9378 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9379 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9381 | /* fix functions return the new maximum character in a string, |
| 9382 | if the kind of the resulting unicode object does not change, |
| 9383 | everything is fine. Otherwise we need to change the string kind |
| 9384 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9385 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9386 | |
| 9387 | if (maxchar_new == 0) { |
| 9388 | /* no changes */; |
| 9389 | if (PyUnicode_CheckExact(self)) { |
| 9390 | Py_DECREF(u); |
| 9391 | Py_INCREF(self); |
| 9392 | return self; |
| 9393 | } |
| 9394 | else |
| 9395 | return u; |
| 9396 | } |
| 9397 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9398 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9399 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9400 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9402 | |
| 9403 | /* In case the maximum character changed, we need to |
| 9404 | convert the string to the new category. */ |
| 9405 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9406 | if (v == NULL) { |
| 9407 | Py_DECREF(u); |
| 9408 | return NULL; |
| 9409 | } |
| 9410 | if (maxchar_new > maxchar_old) { |
| 9411 | /* If the maxchar increased so that the kind changed, not all |
| 9412 | characters are representable anymore and we need to fix the |
| 9413 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9414 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9415 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9416 | maxchar_old = fixfct(v); |
| 9417 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | } |
| 9419 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9420 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9421 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9422 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9423 | Py_DECREF(u); |
| 9424 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9425 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9426 | } |
| 9427 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9428 | static PyObject * |
| 9429 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9430 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9431 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9432 | char *resdata, *data = PyUnicode_DATA(self); |
| 9433 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9434 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9435 | res = PyUnicode_New(len, 127); |
| 9436 | if (res == NULL) |
| 9437 | return NULL; |
| 9438 | resdata = PyUnicode_DATA(res); |
| 9439 | if (lower) |
| 9440 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9441 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9442 | _Py_bytes_upper(resdata, data, len); |
| 9443 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9444 | } |
| 9445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9446 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9447 | 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] | 9448 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9449 | Py_ssize_t j; |
| 9450 | int final_sigma; |
| 9451 | Py_UCS4 c; |
| 9452 | /* 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] | 9453 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9454 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9455 | |
| 9456 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9457 | */ |
| 9458 | for (j = i - 1; j >= 0; j--) { |
| 9459 | c = PyUnicode_READ(kind, data, j); |
| 9460 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9461 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9462 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9463 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9464 | if (final_sigma) { |
| 9465 | for (j = i + 1; j < length; j++) { |
| 9466 | c = PyUnicode_READ(kind, data, j); |
| 9467 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9468 | break; |
| 9469 | } |
| 9470 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9471 | } |
| 9472 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9475 | static int |
| 9476 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9477 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9478 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9479 | /* Obscure special case. */ |
| 9480 | if (c == 0x3A3) { |
| 9481 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9482 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9484 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | } |
| 9486 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9487 | static Py_ssize_t |
| 9488 | 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] | 9489 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9490 | Py_ssize_t i, k = 0; |
| 9491 | int n_res, j; |
| 9492 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9493 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9494 | c = PyUnicode_READ(kind, data, 0); |
| 9495 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9496 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9497 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9498 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9500 | for (i = 1; i < length; i++) { |
| 9501 | c = PyUnicode_READ(kind, data, i); |
| 9502 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9503 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9504 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9505 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9506 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9507 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9508 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | } |
| 9510 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9511 | static Py_ssize_t |
| 9512 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9513 | Py_ssize_t i, k = 0; |
| 9514 | |
| 9515 | for (i = 0; i < length; i++) { |
| 9516 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9517 | int n_res, j; |
| 9518 | if (Py_UNICODE_ISUPPER(c)) { |
| 9519 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9520 | } |
| 9521 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9522 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9523 | } |
| 9524 | else { |
| 9525 | n_res = 1; |
| 9526 | mapped[0] = c; |
| 9527 | } |
| 9528 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9529 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9530 | res[k++] = mapped[j]; |
| 9531 | } |
| 9532 | } |
| 9533 | return k; |
| 9534 | } |
| 9535 | |
| 9536 | static Py_ssize_t |
| 9537 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9538 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9540 | Py_ssize_t i, k = 0; |
| 9541 | |
| 9542 | for (i = 0; i < length; i++) { |
| 9543 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9544 | int n_res, j; |
| 9545 | if (lower) |
| 9546 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9547 | else |
| 9548 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9549 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9550 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9551 | res[k++] = mapped[j]; |
| 9552 | } |
| 9553 | } |
| 9554 | return k; |
| 9555 | } |
| 9556 | |
| 9557 | static Py_ssize_t |
| 9558 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9559 | { |
| 9560 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9561 | } |
| 9562 | |
| 9563 | static Py_ssize_t |
| 9564 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9565 | { |
| 9566 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9567 | } |
| 9568 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9569 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9570 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9571 | { |
| 9572 | Py_ssize_t i, k = 0; |
| 9573 | |
| 9574 | for (i = 0; i < length; i++) { |
| 9575 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9576 | Py_UCS4 mapped[3]; |
| 9577 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9578 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9579 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9580 | res[k++] = mapped[j]; |
| 9581 | } |
| 9582 | } |
| 9583 | return k; |
| 9584 | } |
| 9585 | |
| 9586 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9587 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9588 | { |
| 9589 | Py_ssize_t i, k = 0; |
| 9590 | int previous_is_cased; |
| 9591 | |
| 9592 | previous_is_cased = 0; |
| 9593 | for (i = 0; i < length; i++) { |
| 9594 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9595 | Py_UCS4 mapped[3]; |
| 9596 | int n_res, j; |
| 9597 | |
| 9598 | if (previous_is_cased) |
| 9599 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9600 | else |
| 9601 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9602 | |
| 9603 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9604 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9605 | res[k++] = mapped[j]; |
| 9606 | } |
| 9607 | |
| 9608 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9609 | } |
| 9610 | return k; |
| 9611 | } |
| 9612 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9613 | static PyObject * |
| 9614 | case_operation(PyObject *self, |
| 9615 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9616 | { |
| 9617 | PyObject *res = NULL; |
| 9618 | Py_ssize_t length, newlength = 0; |
| 9619 | int kind, outkind; |
| 9620 | void *data, *outdata; |
| 9621 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9622 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9623 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9624 | |
| 9625 | kind = PyUnicode_KIND(self); |
| 9626 | data = PyUnicode_DATA(self); |
| 9627 | length = PyUnicode_GET_LENGTH(self); |
| 9628 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9629 | if (tmp == NULL) |
| 9630 | return PyErr_NoMemory(); |
| 9631 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9632 | res = PyUnicode_New(newlength, maxchar); |
| 9633 | if (res == NULL) |
| 9634 | goto leave; |
| 9635 | tmpend = tmp + newlength; |
| 9636 | outdata = PyUnicode_DATA(res); |
| 9637 | outkind = PyUnicode_KIND(res); |
| 9638 | switch (outkind) { |
| 9639 | case PyUnicode_1BYTE_KIND: |
| 9640 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9641 | break; |
| 9642 | case PyUnicode_2BYTE_KIND: |
| 9643 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9644 | break; |
| 9645 | case PyUnicode_4BYTE_KIND: |
| 9646 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9647 | break; |
| 9648 | default: |
| 9649 | assert(0); |
| 9650 | break; |
| 9651 | } |
| 9652 | leave: |
| 9653 | PyMem_FREE(tmp); |
| 9654 | return res; |
| 9655 | } |
| 9656 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9657 | PyObject * |
| 9658 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9660 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9661 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9662 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9663 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9664 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9665 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9666 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9667 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9668 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9669 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9670 | int use_memcpy; |
| 9671 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9672 | PyObject *last_obj; |
| 9673 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9674 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9675 | fseq = PySequence_Fast(seq, ""); |
| 9676 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9677 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9678 | } |
| 9679 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9680 | /* NOTE: the following code can't call back into Python code, |
| 9681 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9682 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9683 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9684 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9685 | /* If empty sequence, return u"". */ |
| 9686 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9687 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9688 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9689 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9690 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9691 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9692 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9693 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9694 | if (seqlen == 1) { |
| 9695 | if (PyUnicode_CheckExact(items[0])) { |
| 9696 | res = items[0]; |
| 9697 | Py_INCREF(res); |
| 9698 | Py_DECREF(fseq); |
| 9699 | return res; |
| 9700 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9701 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9702 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9703 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9704 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9705 | /* Set up sep and seplen */ |
| 9706 | if (separator == NULL) { |
| 9707 | /* fall back to a blank space separator */ |
| 9708 | sep = PyUnicode_FromOrdinal(' '); |
| 9709 | if (!sep) |
| 9710 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9711 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9712 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9713 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9714 | else { |
| 9715 | if (!PyUnicode_Check(separator)) { |
| 9716 | PyErr_Format(PyExc_TypeError, |
| 9717 | "separator: expected str instance," |
| 9718 | " %.80s found", |
| 9719 | Py_TYPE(separator)->tp_name); |
| 9720 | goto onError; |
| 9721 | } |
| 9722 | if (PyUnicode_READY(separator)) |
| 9723 | goto onError; |
| 9724 | sep = separator; |
| 9725 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9726 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9727 | /* inc refcount to keep this code path symmetric with the |
| 9728 | above case of a blank separator */ |
| 9729 | Py_INCREF(sep); |
| 9730 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9731 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9732 | } |
| 9733 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9734 | /* There are at least two things to join, or else we have a subclass |
| 9735 | * of str in the sequence. |
| 9736 | * Do a pre-pass to figure out the total amount of space we'll |
| 9737 | * need (sz), and see whether all argument are strings. |
| 9738 | */ |
| 9739 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9740 | #ifdef Py_DEBUG |
| 9741 | use_memcpy = 0; |
| 9742 | #else |
| 9743 | use_memcpy = 1; |
| 9744 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9745 | for (i = 0; i < seqlen; i++) { |
| 9746 | const Py_ssize_t old_sz = sz; |
| 9747 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9748 | if (!PyUnicode_Check(item)) { |
| 9749 | PyErr_Format(PyExc_TypeError, |
| 9750 | "sequence item %zd: expected str instance," |
| 9751 | " %.80s found", |
| 9752 | i, Py_TYPE(item)->tp_name); |
| 9753 | goto onError; |
| 9754 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9755 | if (PyUnicode_READY(item) == -1) |
| 9756 | goto onError; |
| 9757 | sz += PyUnicode_GET_LENGTH(item); |
| 9758 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9759 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9760 | if (i != 0) |
| 9761 | sz += seplen; |
| 9762 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9763 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9764 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9765 | goto onError; |
| 9766 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9767 | if (use_memcpy && last_obj != NULL) { |
| 9768 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9769 | use_memcpy = 0; |
| 9770 | } |
| 9771 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9772 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9773 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9774 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9775 | if (res == NULL) |
| 9776 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9777 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9778 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9779 | #ifdef Py_DEBUG |
| 9780 | use_memcpy = 0; |
| 9781 | #else |
| 9782 | if (use_memcpy) { |
| 9783 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9784 | kind = PyUnicode_KIND(res); |
| 9785 | if (seplen != 0) |
| 9786 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9787 | } |
| 9788 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9789 | if (use_memcpy) { |
| 9790 | for (i = 0; i < seqlen; ++i) { |
| 9791 | Py_ssize_t itemlen; |
| 9792 | item = items[i]; |
| 9793 | |
| 9794 | /* Copy item, and maybe the separator. */ |
| 9795 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9796 | Py_MEMCPY(res_data, |
| 9797 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9798 | kind * seplen); |
| 9799 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9800 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9801 | |
| 9802 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9803 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9804 | Py_MEMCPY(res_data, |
| 9805 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9806 | kind * itemlen); |
| 9807 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9808 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9809 | } |
| 9810 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9811 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9812 | } |
| 9813 | else { |
| 9814 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9815 | Py_ssize_t itemlen; |
| 9816 | item = items[i]; |
| 9817 | |
| 9818 | /* Copy item, and maybe the separator. */ |
| 9819 | if (i && seplen != 0) { |
| 9820 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9821 | res_offset += seplen; |
| 9822 | } |
| 9823 | |
| 9824 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9825 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9826 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9827 | res_offset += itemlen; |
| 9828 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9829 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9830 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9831 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9832 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9833 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9834 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9835 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9836 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9838 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9839 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9840 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9841 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9842 | return NULL; |
| 9843 | } |
| 9844 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9845 | #define FILL(kind, data, value, start, length) \ |
| 9846 | do { \ |
| 9847 | Py_ssize_t i_ = 0; \ |
| 9848 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9849 | switch ((kind)) { \ |
| 9850 | case PyUnicode_1BYTE_KIND: { \ |
| 9851 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9852 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9853 | break; \ |
| 9854 | } \ |
| 9855 | case PyUnicode_2BYTE_KIND: { \ |
| 9856 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9857 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9858 | break; \ |
| 9859 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9860 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9861 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9862 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9863 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9864 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | } \ |
| 9866 | } \ |
| 9867 | } while (0) |
| 9868 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9869 | void |
| 9870 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9871 | Py_UCS4 fill_char) |
| 9872 | { |
| 9873 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9874 | const void *data = PyUnicode_DATA(unicode); |
| 9875 | assert(PyUnicode_IS_READY(unicode)); |
| 9876 | assert(unicode_modifiable(unicode)); |
| 9877 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9878 | assert(start >= 0); |
| 9879 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9880 | FILL(kind, data, fill_char, start, length); |
| 9881 | } |
| 9882 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9883 | Py_ssize_t |
| 9884 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9885 | Py_UCS4 fill_char) |
| 9886 | { |
| 9887 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9888 | |
| 9889 | if (!PyUnicode_Check(unicode)) { |
| 9890 | PyErr_BadInternalCall(); |
| 9891 | return -1; |
| 9892 | } |
| 9893 | if (PyUnicode_READY(unicode) == -1) |
| 9894 | return -1; |
| 9895 | if (unicode_check_modifiable(unicode)) |
| 9896 | return -1; |
| 9897 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9898 | if (start < 0) { |
| 9899 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9900 | return -1; |
| 9901 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9902 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9903 | PyErr_SetString(PyExc_ValueError, |
| 9904 | "fill character is bigger than " |
| 9905 | "the string maximum character"); |
| 9906 | return -1; |
| 9907 | } |
| 9908 | |
| 9909 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9910 | length = Py_MIN(maxlen, length); |
| 9911 | if (length <= 0) |
| 9912 | return 0; |
| 9913 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9914 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9915 | return length; |
| 9916 | } |
| 9917 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9918 | static PyObject * |
| 9919 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9920 | Py_ssize_t left, |
| 9921 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | PyObject *u; |
| 9925 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9926 | int kind; |
| 9927 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9928 | |
| 9929 | if (left < 0) |
| 9930 | left = 0; |
| 9931 | if (right < 0) |
| 9932 | right = 0; |
| 9933 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9934 | if (left == 0 && right == 0) |
| 9935 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9937 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9938 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9939 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9940 | return NULL; |
| 9941 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9943 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9944 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9945 | if (!u) |
| 9946 | return NULL; |
| 9947 | |
| 9948 | kind = PyUnicode_KIND(u); |
| 9949 | data = PyUnicode_DATA(u); |
| 9950 | if (left) |
| 9951 | FILL(kind, data, fill, 0, left); |
| 9952 | if (right) |
| 9953 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9954 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9955 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9956 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9957 | } |
| 9958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9959 | PyObject * |
| 9960 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9962 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9963 | |
| 9964 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9965 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9966 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9967 | if (PyUnicode_READY(string) == -1) { |
| 9968 | Py_DECREF(string); |
| 9969 | return NULL; |
| 9970 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9972 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9974 | if (PyUnicode_IS_ASCII(string)) |
| 9975 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9976 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9977 | PyUnicode_GET_LENGTH(string), keepends); |
| 9978 | else |
| 9979 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9980 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9981 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9982 | break; |
| 9983 | case PyUnicode_2BYTE_KIND: |
| 9984 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9985 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | PyUnicode_GET_LENGTH(string), keepends); |
| 9987 | break; |
| 9988 | case PyUnicode_4BYTE_KIND: |
| 9989 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9990 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | PyUnicode_GET_LENGTH(string), keepends); |
| 9992 | break; |
| 9993 | default: |
| 9994 | assert(0); |
| 9995 | list = 0; |
| 9996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | Py_DECREF(string); |
| 9998 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9999 | } |
| 10000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10001 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10002 | split(PyObject *self, |
| 10003 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10004 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10005 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10006 | int kind1, kind2, kind; |
| 10007 | void *buf1, *buf2; |
| 10008 | Py_ssize_t len1, len2; |
| 10009 | PyObject* out; |
| 10010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10011 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10012 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | if (PyUnicode_READY(self) == -1) |
| 10015 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10018 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10020 | if (PyUnicode_IS_ASCII(self)) |
| 10021 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10022 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10023 | PyUnicode_GET_LENGTH(self), maxcount |
| 10024 | ); |
| 10025 | else |
| 10026 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10027 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10028 | PyUnicode_GET_LENGTH(self), maxcount |
| 10029 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | case PyUnicode_2BYTE_KIND: |
| 10031 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10032 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10033 | PyUnicode_GET_LENGTH(self), maxcount |
| 10034 | ); |
| 10035 | case PyUnicode_4BYTE_KIND: |
| 10036 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10037 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | PyUnicode_GET_LENGTH(self), maxcount |
| 10039 | ); |
| 10040 | default: |
| 10041 | assert(0); |
| 10042 | return NULL; |
| 10043 | } |
| 10044 | |
| 10045 | if (PyUnicode_READY(substring) == -1) |
| 10046 | return NULL; |
| 10047 | |
| 10048 | kind1 = PyUnicode_KIND(self); |
| 10049 | kind2 = PyUnicode_KIND(substring); |
| 10050 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10051 | buf1 = PyUnicode_DATA(self); |
| 10052 | buf2 = PyUnicode_DATA(substring); |
| 10053 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10054 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10055 | if (!buf1) |
| 10056 | return NULL; |
| 10057 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10058 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | if (!buf2) { |
| 10060 | if (kind1 != kind) PyMem_Free(buf1); |
| 10061 | return NULL; |
| 10062 | } |
| 10063 | len1 = PyUnicode_GET_LENGTH(self); |
| 10064 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10065 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10066 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10068 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10069 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10070 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10071 | else |
| 10072 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10073 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | break; |
| 10075 | case PyUnicode_2BYTE_KIND: |
| 10076 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10077 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | break; |
| 10079 | case PyUnicode_4BYTE_KIND: |
| 10080 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10081 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | break; |
| 10083 | default: |
| 10084 | out = NULL; |
| 10085 | } |
| 10086 | if (kind1 != kind) |
| 10087 | PyMem_Free(buf1); |
| 10088 | if (kind2 != kind) |
| 10089 | PyMem_Free(buf2); |
| 10090 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10091 | } |
| 10092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10093 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10094 | rsplit(PyObject *self, |
| 10095 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10096 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10097 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10098 | int kind1, kind2, kind; |
| 10099 | void *buf1, *buf2; |
| 10100 | Py_ssize_t len1, len2; |
| 10101 | PyObject* out; |
| 10102 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10103 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10104 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10105 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | if (PyUnicode_READY(self) == -1) |
| 10107 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10110 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10112 | if (PyUnicode_IS_ASCII(self)) |
| 10113 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10114 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10115 | PyUnicode_GET_LENGTH(self), maxcount |
| 10116 | ); |
| 10117 | else |
| 10118 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10119 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10120 | PyUnicode_GET_LENGTH(self), maxcount |
| 10121 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10122 | case PyUnicode_2BYTE_KIND: |
| 10123 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10124 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10125 | PyUnicode_GET_LENGTH(self), maxcount |
| 10126 | ); |
| 10127 | case PyUnicode_4BYTE_KIND: |
| 10128 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10129 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | PyUnicode_GET_LENGTH(self), maxcount |
| 10131 | ); |
| 10132 | default: |
| 10133 | assert(0); |
| 10134 | return NULL; |
| 10135 | } |
| 10136 | |
| 10137 | if (PyUnicode_READY(substring) == -1) |
| 10138 | return NULL; |
| 10139 | |
| 10140 | kind1 = PyUnicode_KIND(self); |
| 10141 | kind2 = PyUnicode_KIND(substring); |
| 10142 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10143 | buf1 = PyUnicode_DATA(self); |
| 10144 | buf2 = PyUnicode_DATA(substring); |
| 10145 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10146 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10147 | if (!buf1) |
| 10148 | return NULL; |
| 10149 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10150 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | if (!buf2) { |
| 10152 | if (kind1 != kind) PyMem_Free(buf1); |
| 10153 | return NULL; |
| 10154 | } |
| 10155 | len1 = PyUnicode_GET_LENGTH(self); |
| 10156 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10157 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10158 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10160 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10161 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10162 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10163 | else |
| 10164 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10165 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | break; |
| 10167 | case PyUnicode_2BYTE_KIND: |
| 10168 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10169 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | break; |
| 10171 | case PyUnicode_4BYTE_KIND: |
| 10172 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10173 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | break; |
| 10175 | default: |
| 10176 | out = NULL; |
| 10177 | } |
| 10178 | if (kind1 != kind) |
| 10179 | PyMem_Free(buf1); |
| 10180 | if (kind2 != kind) |
| 10181 | PyMem_Free(buf2); |
| 10182 | return out; |
| 10183 | } |
| 10184 | |
| 10185 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10186 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10187 | 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] | 10188 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10189 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10191 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10192 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10193 | else |
| 10194 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10195 | case PyUnicode_2BYTE_KIND: |
| 10196 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10197 | case PyUnicode_4BYTE_KIND: |
| 10198 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10199 | } |
| 10200 | assert(0); |
| 10201 | return -1; |
| 10202 | } |
| 10203 | |
| 10204 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10205 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10206 | 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] | 10207 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10208 | switch (kind) { |
| 10209 | case PyUnicode_1BYTE_KIND: |
| 10210 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10211 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10212 | else |
| 10213 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10214 | case PyUnicode_2BYTE_KIND: |
| 10215 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10216 | case PyUnicode_4BYTE_KIND: |
| 10217 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10218 | } |
| 10219 | assert(0); |
| 10220 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10221 | } |
| 10222 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10223 | static void |
| 10224 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10225 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10226 | { |
| 10227 | int kind = PyUnicode_KIND(u); |
| 10228 | void *data = PyUnicode_DATA(u); |
| 10229 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10230 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10231 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10232 | (Py_UCS1 *)data + len, |
| 10233 | u1, u2, maxcount); |
| 10234 | } |
| 10235 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10236 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10237 | (Py_UCS2 *)data + len, |
| 10238 | u1, u2, maxcount); |
| 10239 | } |
| 10240 | else { |
| 10241 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10242 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10243 | (Py_UCS4 *)data + len, |
| 10244 | u1, u2, maxcount); |
| 10245 | } |
| 10246 | } |
| 10247 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10248 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10249 | replace(PyObject *self, PyObject *str1, |
| 10250 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10251 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10252 | PyObject *u; |
| 10253 | char *sbuf = PyUnicode_DATA(self); |
| 10254 | char *buf1 = PyUnicode_DATA(str1); |
| 10255 | char *buf2 = PyUnicode_DATA(str2); |
| 10256 | int srelease = 0, release1 = 0, release2 = 0; |
| 10257 | int skind = PyUnicode_KIND(self); |
| 10258 | int kind1 = PyUnicode_KIND(str1); |
| 10259 | int kind2 = PyUnicode_KIND(str2); |
| 10260 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10261 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10262 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10263 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10264 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | |
| 10266 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10267 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10268 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10269 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10270 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10271 | if (str1 == str2) |
| 10272 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10273 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10274 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10275 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10276 | if (maxchar < maxchar_str1) |
| 10277 | /* substring too wide to be present */ |
| 10278 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10279 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10280 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10281 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10282 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10283 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10286 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10288 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10290 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10291 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10292 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10293 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10294 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10295 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10296 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10297 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10298 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10299 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10300 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10302 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10303 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10304 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10305 | } |
| 10306 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | int rkind = skind; |
| 10308 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10309 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | if (kind1 < rkind) { |
| 10312 | /* widen substring */ |
| 10313 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10314 | if (!buf1) goto error; |
| 10315 | release1 = 1; |
| 10316 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10317 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10318 | if (i < 0) |
| 10319 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10320 | if (rkind > kind2) { |
| 10321 | /* widen replacement */ |
| 10322 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10323 | if (!buf2) goto error; |
| 10324 | release2 = 1; |
| 10325 | } |
| 10326 | else if (rkind < kind2) { |
| 10327 | /* widen self and buf1 */ |
| 10328 | rkind = kind2; |
| 10329 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10330 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10332 | if (!sbuf) goto error; |
| 10333 | srelease = 1; |
| 10334 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10335 | if (!buf1) goto error; |
| 10336 | release1 = 1; |
| 10337 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10338 | u = PyUnicode_New(slen, maxchar); |
| 10339 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10340 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10341 | assert(PyUnicode_KIND(u) == rkind); |
| 10342 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10343 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10344 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10345 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10346 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10348 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10349 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10350 | |
| 10351 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10352 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10353 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10354 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10355 | if (i == -1) |
| 10356 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10357 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10358 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10359 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10361 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10362 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10363 | } |
| 10364 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10365 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10366 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | int rkind = skind; |
| 10368 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10370 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10371 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10372 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10373 | if (!buf1) goto error; |
| 10374 | release1 = 1; |
| 10375 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10376 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10377 | if (n == 0) |
| 10378 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10380 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10381 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10382 | if (!buf2) goto error; |
| 10383 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10385 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10386 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10387 | rkind = kind2; |
| 10388 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10389 | if (!sbuf) goto error; |
| 10390 | srelease = 1; |
| 10391 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10392 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10393 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10394 | if (!buf1) goto error; |
| 10395 | release1 = 1; |
| 10396 | } |
| 10397 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10398 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10399 | 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] | 10400 | PyErr_SetString(PyExc_OverflowError, |
| 10401 | "replace string is too long"); |
| 10402 | goto error; |
| 10403 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10404 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10405 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10406 | _Py_INCREF_UNICODE_EMPTY(); |
| 10407 | if (!unicode_empty) |
| 10408 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10409 | u = unicode_empty; |
| 10410 | goto done; |
| 10411 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10412 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10413 | PyErr_SetString(PyExc_OverflowError, |
| 10414 | "replace string is too long"); |
| 10415 | goto error; |
| 10416 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10417 | u = PyUnicode_New(new_size, maxchar); |
| 10418 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10419 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10420 | assert(PyUnicode_KIND(u) == rkind); |
| 10421 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 | ires = i = 0; |
| 10423 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10424 | while (n-- > 0) { |
| 10425 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10426 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10427 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10428 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10429 | if (j == -1) |
| 10430 | break; |
| 10431 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10432 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10433 | memcpy(res + rkind * ires, |
| 10434 | sbuf + rkind * i, |
| 10435 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10437 | } |
| 10438 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10440 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10441 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10442 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10446 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10448 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10449 | memcpy(res + rkind * ires, |
| 10450 | sbuf + rkind * i, |
| 10451 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10452 | } |
| 10453 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10454 | /* interleave */ |
| 10455 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10456 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10458 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10460 | if (--n <= 0) |
| 10461 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10462 | memcpy(res + rkind * ires, |
| 10463 | sbuf + rkind * i, |
| 10464 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | ires++; |
| 10466 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10467 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10468 | memcpy(res + rkind * ires, |
| 10469 | sbuf + rkind * i, |
| 10470 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10471 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10472 | } |
| 10473 | |
| 10474 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10475 | unicode_adjust_maxchar(&u); |
| 10476 | if (u == NULL) |
| 10477 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10479 | |
| 10480 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10481 | if (srelease) |
| 10482 | PyMem_FREE(sbuf); |
| 10483 | if (release1) |
| 10484 | PyMem_FREE(buf1); |
| 10485 | if (release2) |
| 10486 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10487 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10490 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10491 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10492 | if (srelease) |
| 10493 | PyMem_FREE(sbuf); |
| 10494 | if (release1) |
| 10495 | PyMem_FREE(buf1); |
| 10496 | if (release2) |
| 10497 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10498 | return unicode_result_unchanged(self); |
| 10499 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10500 | error: |
| 10501 | if (srelease && sbuf) |
| 10502 | PyMem_FREE(sbuf); |
| 10503 | if (release1 && buf1) |
| 10504 | PyMem_FREE(buf1); |
| 10505 | if (release2 && buf2) |
| 10506 | PyMem_FREE(buf2); |
| 10507 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10508 | } |
| 10509 | |
| 10510 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10512 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10513 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 | \n\ |
| 10515 | 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] | 10516 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | |
| 10518 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10519 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10520 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10521 | if (PyUnicode_READY(self) == -1) |
| 10522 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10523 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10524 | } |
| 10525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10526 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10527 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | \n\ |
| 10529 | 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] | 10530 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10531 | |
| 10532 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10533 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10534 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10535 | if (PyUnicode_READY(self) == -1) |
| 10536 | return NULL; |
| 10537 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10538 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10539 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10540 | } |
| 10541 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10542 | PyDoc_STRVAR(casefold__doc__, |
| 10543 | "S.casefold() -> str\n\ |
| 10544 | \n\ |
| 10545 | Return a version of S suitable for caseless comparisons."); |
| 10546 | |
| 10547 | static PyObject * |
| 10548 | unicode_casefold(PyObject *self) |
| 10549 | { |
| 10550 | if (PyUnicode_READY(self) == -1) |
| 10551 | return NULL; |
| 10552 | if (PyUnicode_IS_ASCII(self)) |
| 10553 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10554 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10555 | } |
| 10556 | |
| 10557 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10558 | /* Argument converter. Coerces to a single unicode character */ |
| 10559 | |
| 10560 | static int |
| 10561 | convert_uc(PyObject *obj, void *addr) |
| 10562 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10563 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10564 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10565 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10566 | uniobj = PyUnicode_FromObject(obj); |
| 10567 | if (uniobj == NULL) { |
| 10568 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10569 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10570 | return 0; |
| 10571 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10573 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10574 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10575 | Py_DECREF(uniobj); |
| 10576 | return 0; |
| 10577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10579 | Py_DECREF(uniobj); |
| 10580 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10581 | } |
| 10582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10583 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10584 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10586 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10587 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | |
| 10589 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10590 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10592 | Py_ssize_t marg, left; |
| 10593 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10594 | Py_UCS4 fillchar = ' '; |
| 10595 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10596 | 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] | 10597 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10599 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10600 | return NULL; |
| 10601 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10602 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10603 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10605 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | left = marg / 2 + (marg & width & 1); |
| 10607 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10608 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10612 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10613 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10614 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10615 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10616 | #define COMPARE(TYPE1, TYPE2) \ |
| 10617 | do { \ |
| 10618 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10619 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10620 | TYPE1* end = p1 + len; \ |
| 10621 | Py_UCS4 c1, c2; \ |
| 10622 | for (; p1 != end; p1++, p2++) { \ |
| 10623 | c1 = *p1; \ |
| 10624 | c2 = *p2; \ |
| 10625 | if (c1 != c2) \ |
| 10626 | return (c1 < c2) ? -1 : 1; \ |
| 10627 | } \ |
| 10628 | } \ |
| 10629 | while (0) |
| 10630 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10631 | int kind1, kind2; |
| 10632 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10633 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | kind1 = PyUnicode_KIND(str1); |
| 10636 | kind2 = PyUnicode_KIND(str2); |
| 10637 | data1 = PyUnicode_DATA(str1); |
| 10638 | data2 = PyUnicode_DATA(str2); |
| 10639 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10640 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10641 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10642 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10643 | switch(kind1) { |
| 10644 | case PyUnicode_1BYTE_KIND: |
| 10645 | { |
| 10646 | switch(kind2) { |
| 10647 | case PyUnicode_1BYTE_KIND: |
| 10648 | { |
| 10649 | int cmp = memcmp(data1, data2, len); |
| 10650 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10651 | if (cmp < 0) |
| 10652 | return -1; |
| 10653 | if (cmp > 0) |
| 10654 | return 1; |
| 10655 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10656 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10657 | case PyUnicode_2BYTE_KIND: |
| 10658 | COMPARE(Py_UCS1, Py_UCS2); |
| 10659 | break; |
| 10660 | case PyUnicode_4BYTE_KIND: |
| 10661 | COMPARE(Py_UCS1, Py_UCS4); |
| 10662 | break; |
| 10663 | default: |
| 10664 | assert(0); |
| 10665 | } |
| 10666 | break; |
| 10667 | } |
| 10668 | case PyUnicode_2BYTE_KIND: |
| 10669 | { |
| 10670 | switch(kind2) { |
| 10671 | case PyUnicode_1BYTE_KIND: |
| 10672 | COMPARE(Py_UCS2, Py_UCS1); |
| 10673 | break; |
| 10674 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10675 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10676 | COMPARE(Py_UCS2, Py_UCS2); |
| 10677 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10678 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10679 | case PyUnicode_4BYTE_KIND: |
| 10680 | COMPARE(Py_UCS2, Py_UCS4); |
| 10681 | break; |
| 10682 | default: |
| 10683 | assert(0); |
| 10684 | } |
| 10685 | break; |
| 10686 | } |
| 10687 | case PyUnicode_4BYTE_KIND: |
| 10688 | { |
| 10689 | switch(kind2) { |
| 10690 | case PyUnicode_1BYTE_KIND: |
| 10691 | COMPARE(Py_UCS4, Py_UCS1); |
| 10692 | break; |
| 10693 | case PyUnicode_2BYTE_KIND: |
| 10694 | COMPARE(Py_UCS4, Py_UCS2); |
| 10695 | break; |
| 10696 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10697 | { |
| 10698 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10699 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10700 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10701 | if (cmp < 0) |
| 10702 | return -1; |
| 10703 | if (cmp > 0) |
| 10704 | return 1; |
| 10705 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10706 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10707 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10708 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10709 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10710 | default: |
| 10711 | assert(0); |
| 10712 | } |
| 10713 | break; |
| 10714 | } |
| 10715 | default: |
| 10716 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10717 | } |
| 10718 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10719 | if (len1 == len2) |
| 10720 | return 0; |
| 10721 | if (len1 < len2) |
| 10722 | return -1; |
| 10723 | else |
| 10724 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10725 | |
| 10726 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10727 | } |
| 10728 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10729 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10730 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10731 | { |
| 10732 | int kind; |
| 10733 | void *data1, *data2; |
| 10734 | Py_ssize_t len; |
| 10735 | int cmp; |
| 10736 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10737 | len = PyUnicode_GET_LENGTH(str1); |
| 10738 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10739 | return 0; |
| 10740 | kind = PyUnicode_KIND(str1); |
| 10741 | if (PyUnicode_KIND(str2) != kind) |
| 10742 | return 0; |
| 10743 | data1 = PyUnicode_DATA(str1); |
| 10744 | data2 = PyUnicode_DATA(str2); |
| 10745 | |
| 10746 | cmp = memcmp(data1, data2, len * kind); |
| 10747 | return (cmp == 0); |
| 10748 | } |
| 10749 | |
| 10750 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10751 | int |
| 10752 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10753 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10754 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10755 | if (PyUnicode_READY(left) == -1 || |
| 10756 | PyUnicode_READY(right) == -1) |
| 10757 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10758 | |
| 10759 | /* a string is equal to itself */ |
| 10760 | if (left == right) |
| 10761 | return 0; |
| 10762 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10763 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10764 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10765 | PyErr_Format(PyExc_TypeError, |
| 10766 | "Can't compare %.100s and %.100s", |
| 10767 | left->ob_type->tp_name, |
| 10768 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10769 | return -1; |
| 10770 | } |
| 10771 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10772 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10773 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10774 | { |
| 10775 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10776 | if (right_str == NULL) |
| 10777 | return -1; |
| 10778 | return PyUnicode_Compare(left, right_str); |
| 10779 | } |
| 10780 | |
| 10781 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10782 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10783 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10784 | Py_ssize_t i; |
| 10785 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 | Py_UCS4 chr; |
| 10787 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10788 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10789 | if (PyUnicode_READY(uni) == -1) |
| 10790 | return -1; |
| 10791 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10792 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10793 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10794 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10795 | size_t len, len2 = strlen(str); |
| 10796 | int cmp; |
| 10797 | |
| 10798 | len = Py_MIN(len1, len2); |
| 10799 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10800 | if (cmp != 0) { |
| 10801 | if (cmp < 0) |
| 10802 | return -1; |
| 10803 | else |
| 10804 | return 1; |
| 10805 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10806 | if (len1 > len2) |
| 10807 | return 1; /* uni is longer */ |
| 10808 | if (len2 > len1) |
| 10809 | return -1; /* str is longer */ |
| 10810 | return 0; |
| 10811 | } |
| 10812 | else { |
| 10813 | void *data = PyUnicode_DATA(uni); |
| 10814 | /* Compare Unicode string and source character set string */ |
| 10815 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10816 | if (chr != str[i]) |
| 10817 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10818 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10819 | to C strings identical up to that point. */ |
| 10820 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10821 | return 1; /* uni is longer */ |
| 10822 | if (str[i]) |
| 10823 | return -1; /* str is longer */ |
| 10824 | return 0; |
| 10825 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10826 | } |
| 10827 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10828 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10829 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10830 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10831 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10832 | PyObject * |
| 10833 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10834 | { |
| 10835 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10836 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10837 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10838 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10839 | Py_RETURN_NOTIMPLEMENTED; |
| 10840 | |
| 10841 | if (PyUnicode_READY(left) == -1 || |
| 10842 | PyUnicode_READY(right) == -1) |
| 10843 | return NULL; |
| 10844 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10845 | if (left == right) { |
| 10846 | switch (op) { |
| 10847 | case Py_EQ: |
| 10848 | case Py_LE: |
| 10849 | case Py_GE: |
| 10850 | /* a string is equal to itself */ |
| 10851 | v = Py_True; |
| 10852 | break; |
| 10853 | case Py_NE: |
| 10854 | case Py_LT: |
| 10855 | case Py_GT: |
| 10856 | v = Py_False; |
| 10857 | break; |
| 10858 | default: |
| 10859 | PyErr_BadArgument(); |
| 10860 | return NULL; |
| 10861 | } |
| 10862 | } |
| 10863 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10864 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10865 | result ^= (op == Py_NE); |
| 10866 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10867 | } |
| 10868 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10869 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10870 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10871 | /* Convert the return value to a Boolean */ |
| 10872 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10873 | case Py_LE: |
| 10874 | v = TEST_COND(result <= 0); |
| 10875 | break; |
| 10876 | case Py_GE: |
| 10877 | v = TEST_COND(result >= 0); |
| 10878 | break; |
| 10879 | case Py_LT: |
| 10880 | v = TEST_COND(result == -1); |
| 10881 | break; |
| 10882 | case Py_GT: |
| 10883 | v = TEST_COND(result == 1); |
| 10884 | break; |
| 10885 | default: |
| 10886 | PyErr_BadArgument(); |
| 10887 | return NULL; |
| 10888 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10889 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10890 | Py_INCREF(v); |
| 10891 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10892 | } |
| 10893 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10894 | int |
| 10895 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10896 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10897 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10898 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10899 | void *buf1, *buf2; |
| 10900 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10901 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10902 | |
| 10903 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10904 | sub = PyUnicode_FromObject(element); |
| 10905 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10906 | PyErr_Format(PyExc_TypeError, |
| 10907 | "'in <string>' requires string as left operand, not %s", |
| 10908 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10909 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10910 | } |
| 10911 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10912 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10913 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10914 | Py_DECREF(sub); |
| 10915 | return -1; |
| 10916 | } |
| 10917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10918 | kind1 = PyUnicode_KIND(str); |
| 10919 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10920 | buf1 = PyUnicode_DATA(str); |
| 10921 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10922 | if (kind2 != kind1) { |
| 10923 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10924 | Py_DECREF(sub); |
| 10925 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10926 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10927 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10928 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10930 | if (!buf2) { |
| 10931 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10932 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10933 | return -1; |
| 10934 | } |
| 10935 | len1 = PyUnicode_GET_LENGTH(str); |
| 10936 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10937 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10938 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10939 | case PyUnicode_1BYTE_KIND: |
| 10940 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10941 | break; |
| 10942 | case PyUnicode_2BYTE_KIND: |
| 10943 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10944 | break; |
| 10945 | case PyUnicode_4BYTE_KIND: |
| 10946 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10947 | break; |
| 10948 | default: |
| 10949 | result = -1; |
| 10950 | assert(0); |
| 10951 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10952 | |
| 10953 | Py_DECREF(str); |
| 10954 | Py_DECREF(sub); |
| 10955 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10956 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | PyMem_Free(buf2); |
| 10958 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10959 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10960 | } |
| 10961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10963 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10964 | PyObject * |
| 10965 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10967 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10968 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10969 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | |
| 10971 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10972 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10975 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10976 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10977 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | |
| 10979 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10980 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10981 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10984 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10985 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10986 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10987 | } |
| 10988 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10989 | u_len = PyUnicode_GET_LENGTH(u); |
| 10990 | v_len = PyUnicode_GET_LENGTH(v); |
| 10991 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10992 | PyErr_SetString(PyExc_OverflowError, |
| 10993 | "strings are too large to concat"); |
| 10994 | goto onError; |
| 10995 | } |
| 10996 | new_len = u_len + v_len; |
| 10997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10998 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10999 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11000 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11003 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11005 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11006 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11007 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | Py_DECREF(u); |
| 11009 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11010 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11011 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11013 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | Py_XDECREF(u); |
| 11015 | Py_XDECREF(v); |
| 11016 | return NULL; |
| 11017 | } |
| 11018 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11019 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11020 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11021 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11022 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11023 | Py_UCS4 maxchar, maxchar2; |
| 11024 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11025 | |
| 11026 | if (p_left == NULL) { |
| 11027 | if (!PyErr_Occurred()) |
| 11028 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11029 | return; |
| 11030 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11031 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11032 | if (right == NULL || left == NULL |
| 11033 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11034 | if (!PyErr_Occurred()) |
| 11035 | PyErr_BadInternalCall(); |
| 11036 | goto error; |
| 11037 | } |
| 11038 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11039 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11040 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11041 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11042 | goto error; |
| 11043 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11044 | /* Shortcuts */ |
| 11045 | if (left == unicode_empty) { |
| 11046 | Py_DECREF(left); |
| 11047 | Py_INCREF(right); |
| 11048 | *p_left = right; |
| 11049 | return; |
| 11050 | } |
| 11051 | if (right == unicode_empty) |
| 11052 | return; |
| 11053 | |
| 11054 | left_len = PyUnicode_GET_LENGTH(left); |
| 11055 | right_len = PyUnicode_GET_LENGTH(right); |
| 11056 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11057 | PyErr_SetString(PyExc_OverflowError, |
| 11058 | "strings are too large to concat"); |
| 11059 | goto error; |
| 11060 | } |
| 11061 | new_len = left_len + right_len; |
| 11062 | |
| 11063 | if (unicode_modifiable(left) |
| 11064 | && PyUnicode_CheckExact(right) |
| 11065 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11066 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11067 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11068 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11069 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11070 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11071 | { |
| 11072 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11073 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11074 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11075 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11076 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11077 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11078 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11079 | else { |
| 11080 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11081 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11082 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11083 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11084 | /* Concat the two Unicode strings */ |
| 11085 | res = PyUnicode_New(new_len, maxchar); |
| 11086 | if (res == NULL) |
| 11087 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11088 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11089 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11090 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11091 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11092 | } |
| 11093 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11094 | return; |
| 11095 | |
| 11096 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11097 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11098 | } |
| 11099 | |
| 11100 | void |
| 11101 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11102 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11103 | PyUnicode_Append(pleft, right); |
| 11104 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11105 | } |
| 11106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11107 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11108 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11110 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11111 | 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] | 11112 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | |
| 11114 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11115 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11117 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11118 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11119 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 | int kind1, kind2, kind; |
| 11122 | void *buf1, *buf2; |
| 11123 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11124 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11125 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11126 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11127 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | kind1 = PyUnicode_KIND(self); |
| 11130 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11131 | if (kind2 > kind1) { |
| 11132 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11133 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11134 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11135 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | buf1 = PyUnicode_DATA(self); |
| 11137 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11138 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11139 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | if (!buf2) { |
| 11141 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11142 | return NULL; |
| 11143 | } |
| 11144 | len1 = PyUnicode_GET_LENGTH(self); |
| 11145 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11146 | |
| 11147 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11148 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11149 | case PyUnicode_1BYTE_KIND: |
| 11150 | iresult = ucs1lib_count( |
| 11151 | ((Py_UCS1*)buf1) + start, end - start, |
| 11152 | buf2, len2, PY_SSIZE_T_MAX |
| 11153 | ); |
| 11154 | break; |
| 11155 | case PyUnicode_2BYTE_KIND: |
| 11156 | iresult = ucs2lib_count( |
| 11157 | ((Py_UCS2*)buf1) + start, end - start, |
| 11158 | buf2, len2, PY_SSIZE_T_MAX |
| 11159 | ); |
| 11160 | break; |
| 11161 | case PyUnicode_4BYTE_KIND: |
| 11162 | iresult = ucs4lib_count( |
| 11163 | ((Py_UCS4*)buf1) + start, end - start, |
| 11164 | buf2, len2, PY_SSIZE_T_MAX |
| 11165 | ); |
| 11166 | break; |
| 11167 | default: |
| 11168 | assert(0); iresult = 0; |
| 11169 | } |
| 11170 | |
| 11171 | result = PyLong_FromSsize_t(iresult); |
| 11172 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11173 | if (kind2 != kind) |
| 11174 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | |
| 11176 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11178 | return result; |
| 11179 | } |
| 11180 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11181 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11182 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11184 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11185 | 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] | 11186 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11187 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11188 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11189 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | |
| 11191 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11192 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11194 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | char *encoding = NULL; |
| 11196 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11197 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11199 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11201 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11202 | } |
| 11203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11204 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11205 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | \n\ |
| 11207 | 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] | 11208 | 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] | 11209 | |
| 11210 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11211 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11213 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11214 | Py_UCS4 ch; |
| 11215 | PyObject *u; |
| 11216 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11217 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11219 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11220 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11222 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11223 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11224 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11226 | if (PyUnicode_READY(self) == -1) |
| 11227 | return NULL; |
| 11228 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11229 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11230 | src_len = PyUnicode_GET_LENGTH(self); |
| 11231 | i = j = line_pos = 0; |
| 11232 | kind = PyUnicode_KIND(self); |
| 11233 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11234 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11235 | for (; i < src_len; i++) { |
| 11236 | ch = PyUnicode_READ(kind, src_data, i); |
| 11237 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11238 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11239 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11240 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11241 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11242 | goto overflow; |
| 11243 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11244 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11245 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11248 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11249 | goto overflow; |
| 11250 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11252 | if (ch == '\n' || ch == '\r') |
| 11253 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11255 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11256 | if (!found) |
| 11257 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11260 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | if (!u) |
| 11262 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11263 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11265 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11267 | for (; i < src_len; i++) { |
| 11268 | ch = PyUnicode_READ(kind, src_data, i); |
| 11269 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11270 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11271 | incr = tabsize - (line_pos % tabsize); |
| 11272 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11273 | FILL(kind, dest_data, ' ', j, incr); |
| 11274 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11275 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11276 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11277 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11278 | line_pos++; |
| 11279 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11280 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11281 | if (ch == '\n' || ch == '\r') |
| 11282 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11283 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11284 | } |
| 11285 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11286 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11287 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11288 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11289 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11290 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | } |
| 11292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11293 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11294 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11295 | \n\ |
| 11296 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11297 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | arguments start and end are interpreted as in slice notation.\n\ |
| 11299 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11300 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11301 | |
| 11302 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11303 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11304 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11305 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11306 | Py_ssize_t start; |
| 11307 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11308 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11309 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11310 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11311 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11314 | if (PyUnicode_READY(self) == -1) { |
| 11315 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11316 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11317 | } |
| 11318 | if (PyUnicode_READY(substring) == -1) { |
| 11319 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11320 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11321 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11323 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | |
| 11325 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11326 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | if (result == -2) |
| 11328 | return NULL; |
| 11329 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11330 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | } |
| 11332 | |
| 11333 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11334 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11335 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11336 | void *data; |
| 11337 | enum PyUnicode_Kind kind; |
| 11338 | Py_UCS4 ch; |
| 11339 | PyObject *res; |
| 11340 | |
| 11341 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11342 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11343 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11344 | } |
| 11345 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11346 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11347 | return NULL; |
| 11348 | } |
| 11349 | kind = PyUnicode_KIND(self); |
| 11350 | data = PyUnicode_DATA(self); |
| 11351 | ch = PyUnicode_READ(kind, data, index); |
| 11352 | if (ch < 256) |
| 11353 | return get_latin1_char(ch); |
| 11354 | |
| 11355 | res = PyUnicode_New(1, ch); |
| 11356 | if (res == NULL) |
| 11357 | return NULL; |
| 11358 | kind = PyUnicode_KIND(res); |
| 11359 | data = PyUnicode_DATA(res); |
| 11360 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11361 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11362 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11363 | } |
| 11364 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11365 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11366 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11367 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11368 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11370 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11371 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11372 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11373 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11374 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11375 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11376 | if (_PyUnicode_HASH(self) != -1) |
| 11377 | return _PyUnicode_HASH(self); |
| 11378 | if (PyUnicode_READY(self) == -1) |
| 11379 | return -1; |
| 11380 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11381 | /* |
| 11382 | We make the hash of the empty string be 0, rather than using |
| 11383 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11384 | */ |
| 11385 | if (len == 0) { |
| 11386 | _PyUnicode_HASH(self) = 0; |
| 11387 | return 0; |
| 11388 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11389 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11390 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11391 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11392 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11396 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11397 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11399 | 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] | 11400 | |
| 11401 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11402 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11404 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11405 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11406 | Py_ssize_t start; |
| 11407 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11408 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11409 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11410 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11413 | if (PyUnicode_READY(self) == -1) { |
| 11414 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11416 | } |
| 11417 | if (PyUnicode_READY(substring) == -1) { |
| 11418 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11419 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11421 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11422 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | |
| 11424 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11426 | if (result == -2) |
| 11427 | return NULL; |
| 11428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | if (result < 0) { |
| 11430 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11431 | return NULL; |
| 11432 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11433 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11434 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | } |
| 11436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11437 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11438 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11440 | 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] | 11441 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | |
| 11443 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11444 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | Py_ssize_t i, length; |
| 11447 | int kind; |
| 11448 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | int cased; |
| 11450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | if (PyUnicode_READY(self) == -1) |
| 11452 | return NULL; |
| 11453 | length = PyUnicode_GET_LENGTH(self); |
| 11454 | kind = PyUnicode_KIND(self); |
| 11455 | data = PyUnicode_DATA(self); |
| 11456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (length == 1) |
| 11459 | return PyBool_FromLong( |
| 11460 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11462 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11463 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11464 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11465 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11467 | for (i = 0; i < length; i++) { |
| 11468 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11469 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11471 | return PyBool_FromLong(0); |
| 11472 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11473 | cased = 1; |
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(cased); |
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(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11481 | 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] | 11482 | at least one cased character in S, 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_isupper(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; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | int cased; |
| 11491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11492 | if (PyUnicode_READY(self) == -1) |
| 11493 | return NULL; |
| 11494 | length = PyUnicode_GET_LENGTH(self); |
| 11495 | kind = PyUnicode_KIND(self); |
| 11496 | data = PyUnicode_DATA(self); |
| 11497 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | if (length == 1) |
| 11500 | return PyBool_FromLong( |
| 11501 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11503 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11504 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11505 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11506 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | for (i = 0; i < length; i++) { |
| 11509 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11511 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11512 | return PyBool_FromLong(0); |
| 11513 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11514 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11516 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | } |
| 11518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11520 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11522 | Return True if S is a titlecased string and there is at least one\n\ |
| 11523 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11524 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11525 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11526 | |
| 11527 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11528 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11530 | Py_ssize_t i, length; |
| 11531 | int kind; |
| 11532 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | int cased, previous_is_cased; |
| 11534 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | if (PyUnicode_READY(self) == -1) |
| 11536 | return NULL; |
| 11537 | length = PyUnicode_GET_LENGTH(self); |
| 11538 | kind = PyUnicode_KIND(self); |
| 11539 | data = PyUnicode_DATA(self); |
| 11540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | if (length == 1) { |
| 11543 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11544 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11545 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11546 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11548 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11549 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11550 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | cased = 0; |
| 11553 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11554 | for (i = 0; i < length; i++) { |
| 11555 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11556 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11557 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11558 | if (previous_is_cased) |
| 11559 | return PyBool_FromLong(0); |
| 11560 | previous_is_cased = 1; |
| 11561 | cased = 1; |
| 11562 | } |
| 11563 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11564 | if (!previous_is_cased) |
| 11565 | return PyBool_FromLong(0); |
| 11566 | previous_is_cased = 1; |
| 11567 | cased = 1; |
| 11568 | } |
| 11569 | else |
| 11570 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11572 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | } |
| 11574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11575 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11576 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11577 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11578 | Return True if all characters in S are whitespace\n\ |
| 11579 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | |
| 11581 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11582 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11583 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11584 | Py_ssize_t i, length; |
| 11585 | int kind; |
| 11586 | void *data; |
| 11587 | |
| 11588 | if (PyUnicode_READY(self) == -1) |
| 11589 | return NULL; |
| 11590 | length = PyUnicode_GET_LENGTH(self); |
| 11591 | kind = PyUnicode_KIND(self); |
| 11592 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11594 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 | if (length == 1) |
| 11596 | return PyBool_FromLong( |
| 11597 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11599 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11600 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11601 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11602 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11603 | for (i = 0; i < length; i++) { |
| 11604 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11605 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11606 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11608 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11609 | } |
| 11610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11611 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11612 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11613 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11614 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11615 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11616 | |
| 11617 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11618 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11619 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | Py_ssize_t i, length; |
| 11621 | int kind; |
| 11622 | void *data; |
| 11623 | |
| 11624 | if (PyUnicode_READY(self) == -1) |
| 11625 | return NULL; |
| 11626 | length = PyUnicode_GET_LENGTH(self); |
| 11627 | kind = PyUnicode_KIND(self); |
| 11628 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11629 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11630 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11631 | if (length == 1) |
| 11632 | return PyBool_FromLong( |
| 11633 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11634 | |
| 11635 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11637 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11639 | for (i = 0; i < length; i++) { |
| 11640 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11641 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11642 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11643 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11644 | } |
| 11645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11646 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11647 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11648 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11649 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11650 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11651 | |
| 11652 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11653 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | int kind; |
| 11656 | void *data; |
| 11657 | Py_ssize_t len, i; |
| 11658 | |
| 11659 | if (PyUnicode_READY(self) == -1) |
| 11660 | return NULL; |
| 11661 | |
| 11662 | kind = PyUnicode_KIND(self); |
| 11663 | data = PyUnicode_DATA(self); |
| 11664 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11665 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11666 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11667 | if (len == 1) { |
| 11668 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11669 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11670 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11671 | |
| 11672 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11674 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11676 | for (i = 0; i < len; i++) { |
| 11677 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11678 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11679 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11680 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11681 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11684 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11685 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11687 | 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] | 11688 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | |
| 11690 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11691 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 | Py_ssize_t i, length; |
| 11694 | int kind; |
| 11695 | void *data; |
| 11696 | |
| 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | length = PyUnicode_GET_LENGTH(self); |
| 11700 | kind = PyUnicode_KIND(self); |
| 11701 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11704 | if (length == 1) |
| 11705 | return PyBool_FromLong( |
| 11706 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11708 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11709 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11710 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11711 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11712 | for (i = 0; i < length; i++) { |
| 11713 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11714 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11715 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11716 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | } |
| 11718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11719 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11720 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11722 | Return True if all characters in S are digits\n\ |
| 11723 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11724 | |
| 11725 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11726 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11727 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11728 | Py_ssize_t i, length; |
| 11729 | int kind; |
| 11730 | void *data; |
| 11731 | |
| 11732 | if (PyUnicode_READY(self) == -1) |
| 11733 | return NULL; |
| 11734 | length = PyUnicode_GET_LENGTH(self); |
| 11735 | kind = PyUnicode_KIND(self); |
| 11736 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11738 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11739 | if (length == 1) { |
| 11740 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11741 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11742 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11744 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11745 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11746 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11748 | for (i = 0; i < length; i++) { |
| 11749 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11752 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11753 | } |
| 11754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11755 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11756 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11757 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11758 | 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] | 11759 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11760 | |
| 11761 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11762 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11763 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11764 | Py_ssize_t i, length; |
| 11765 | int kind; |
| 11766 | void *data; |
| 11767 | |
| 11768 | if (PyUnicode_READY(self) == -1) |
| 11769 | return NULL; |
| 11770 | length = PyUnicode_GET_LENGTH(self); |
| 11771 | kind = PyUnicode_KIND(self); |
| 11772 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | if (length == 1) |
| 11776 | return PyBool_FromLong( |
| 11777 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11779 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11780 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11781 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11782 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11783 | for (i = 0; i < length; i++) { |
| 11784 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11785 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11787 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11788 | } |
| 11789 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11790 | int |
| 11791 | PyUnicode_IsIdentifier(PyObject *self) |
| 11792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11793 | int kind; |
| 11794 | void *data; |
| 11795 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11796 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11797 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11798 | if (PyUnicode_READY(self) == -1) { |
| 11799 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11800 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | } |
| 11802 | |
| 11803 | /* Special case for empty strings */ |
| 11804 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11805 | return 0; |
| 11806 | kind = PyUnicode_KIND(self); |
| 11807 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11808 | |
| 11809 | /* PEP 3131 says that the first character must be in |
| 11810 | XID_Start and subsequent characters in XID_Continue, |
| 11811 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11812 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11813 | letters, digits, underscore). However, given the current |
| 11814 | definition of XID_Start and XID_Continue, it is sufficient |
| 11815 | to check just for these, except that _ must be allowed |
| 11816 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11818 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11819 | return 0; |
| 11820 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11821 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11822 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11823 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11824 | return 1; |
| 11825 | } |
| 11826 | |
| 11827 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11829 | \n\ |
| 11830 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11831 | to the language definition.\n\ |
| 11832 | \n\ |
| 11833 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11834 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11835 | |
| 11836 | static PyObject* |
| 11837 | unicode_isidentifier(PyObject *self) |
| 11838 | { |
| 11839 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11840 | } |
| 11841 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11842 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11844 | \n\ |
| 11845 | Return True if all characters in S are considered\n\ |
| 11846 | printable in repr() or S is empty, False otherwise."); |
| 11847 | |
| 11848 | static PyObject* |
| 11849 | unicode_isprintable(PyObject *self) |
| 11850 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11851 | Py_ssize_t i, length; |
| 11852 | int kind; |
| 11853 | void *data; |
| 11854 | |
| 11855 | if (PyUnicode_READY(self) == -1) |
| 11856 | return NULL; |
| 11857 | length = PyUnicode_GET_LENGTH(self); |
| 11858 | kind = PyUnicode_KIND(self); |
| 11859 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11860 | |
| 11861 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11862 | if (length == 1) |
| 11863 | return PyBool_FromLong( |
| 11864 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11866 | for (i = 0; i < length; i++) { |
| 11867 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11868 | Py_RETURN_FALSE; |
| 11869 | } |
| 11870 | } |
| 11871 | Py_RETURN_TRUE; |
| 11872 | } |
| 11873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11874 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11875 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11876 | \n\ |
| 11877 | 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] | 11878 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 | |
| 11880 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11881 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11883 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | } |
| 11885 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11886 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11887 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11889 | if (PyUnicode_READY(self) == -1) |
| 11890 | return -1; |
| 11891 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11892 | } |
| 11893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11894 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11895 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11896 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11897 | 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] | 11898 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11899 | |
| 11900 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11901 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11903 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | Py_UCS4 fillchar = ' '; |
| 11905 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11906 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11907 | return NULL; |
| 11908 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11909 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11910 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11912 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11913 | return unicode_result_unchanged(self); |
| 11914 | |
| 11915 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11918 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11919 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11920 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11921 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | |
| 11923 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11924 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11925 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11926 | if (PyUnicode_READY(self) == -1) |
| 11927 | return NULL; |
| 11928 | if (PyUnicode_IS_ASCII(self)) |
| 11929 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11930 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | } |
| 11932 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11933 | #define LEFTSTRIP 0 |
| 11934 | #define RIGHTSTRIP 1 |
| 11935 | #define BOTHSTRIP 2 |
| 11936 | |
| 11937 | /* Arrays indexed by above */ |
| 11938 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11939 | |
| 11940 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11941 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11942 | /* externally visible for str.strip(unicode) */ |
| 11943 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11944 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11945 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11946 | void *data; |
| 11947 | int kind; |
| 11948 | Py_ssize_t i, j, len; |
| 11949 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11950 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11951 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11952 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11953 | return NULL; |
| 11954 | |
| 11955 | kind = PyUnicode_KIND(self); |
| 11956 | data = PyUnicode_DATA(self); |
| 11957 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11958 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11959 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11960 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11961 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11962 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11963 | i = 0; |
| 11964 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11965 | while (i < len) { |
| 11966 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11967 | if (!BLOOM(sepmask, ch)) |
| 11968 | break; |
| 11969 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11970 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | i++; |
| 11972 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11973 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11974 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11975 | j = len; |
| 11976 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11977 | j--; |
| 11978 | while (j >= i) { |
| 11979 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11980 | if (!BLOOM(sepmask, ch)) |
| 11981 | break; |
| 11982 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11983 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11984 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11985 | } |
| 11986 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11987 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11988 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11989 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11990 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11991 | } |
| 11992 | |
| 11993 | PyObject* |
| 11994 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11995 | { |
| 11996 | unsigned char *data; |
| 11997 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11998 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12000 | if (PyUnicode_READY(self) == -1) |
| 12001 | return NULL; |
| 12002 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12003 | length = PyUnicode_GET_LENGTH(self); |
| 12004 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12005 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12006 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12007 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12008 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12009 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12010 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12011 | return NULL; |
| 12012 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12013 | if (start >= length || end < start) |
| 12014 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12015 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12016 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12017 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12018 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12019 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12020 | } |
| 12021 | else { |
| 12022 | kind = PyUnicode_KIND(self); |
| 12023 | data = PyUnicode_1BYTE_DATA(self); |
| 12024 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12025 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12026 | length); |
| 12027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12028 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12029 | |
| 12030 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12031 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12032 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12033 | Py_ssize_t len, i, j; |
| 12034 | |
| 12035 | if (PyUnicode_READY(self) == -1) |
| 12036 | return NULL; |
| 12037 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12038 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12039 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12040 | if (PyUnicode_IS_ASCII(self)) { |
| 12041 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12042 | |
| 12043 | i = 0; |
| 12044 | if (striptype != RIGHTSTRIP) { |
| 12045 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12046 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12047 | if (!_Py_ascii_whitespace[ch]) |
| 12048 | break; |
| 12049 | i++; |
| 12050 | } |
| 12051 | } |
| 12052 | |
| 12053 | j = len; |
| 12054 | if (striptype != LEFTSTRIP) { |
| 12055 | j--; |
| 12056 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12057 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12058 | if (!_Py_ascii_whitespace[ch]) |
| 12059 | break; |
| 12060 | j--; |
| 12061 | } |
| 12062 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12063 | } |
| 12064 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12065 | else { |
| 12066 | int kind = PyUnicode_KIND(self); |
| 12067 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12068 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12069 | i = 0; |
| 12070 | if (striptype != RIGHTSTRIP) { |
| 12071 | while (i < len) { |
| 12072 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12073 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12074 | break; |
| 12075 | i++; |
| 12076 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12077 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12078 | |
| 12079 | j = len; |
| 12080 | if (striptype != LEFTSTRIP) { |
| 12081 | j--; |
| 12082 | while (j >= i) { |
| 12083 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12084 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12085 | break; |
| 12086 | j--; |
| 12087 | } |
| 12088 | j++; |
| 12089 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12090 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12091 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12092 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12093 | } |
| 12094 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12095 | |
| 12096 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12097 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12098 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12099 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12100 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12101 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12102 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12103 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12104 | if (sep != NULL && sep != Py_None) { |
| 12105 | if (PyUnicode_Check(sep)) |
| 12106 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12107 | else { |
| 12108 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12109 | "%s arg must be None or str", |
| 12110 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12111 | return NULL; |
| 12112 | } |
| 12113 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12114 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12115 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | } |
| 12117 | |
| 12118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12119 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12120 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12121 | \n\ |
| 12122 | Return a copy of the string S with leading and trailing\n\ |
| 12123 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12124 | 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] | 12125 | |
| 12126 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12127 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12128 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12129 | if (PyTuple_GET_SIZE(args) == 0) |
| 12130 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12131 | else |
| 12132 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12133 | } |
| 12134 | |
| 12135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12136 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12137 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12138 | \n\ |
| 12139 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12140 | 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] | 12141 | |
| 12142 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12143 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12144 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12145 | if (PyTuple_GET_SIZE(args) == 0) |
| 12146 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12147 | else |
| 12148 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12149 | } |
| 12150 | |
| 12151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12152 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12153 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12154 | \n\ |
| 12155 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12156 | 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] | 12157 | |
| 12158 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12159 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12160 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12161 | if (PyTuple_GET_SIZE(args) == 0) |
| 12162 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12163 | else |
| 12164 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12165 | } |
| 12166 | |
| 12167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12168 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12169 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12171 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12172 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12174 | if (len < 1) |
| 12175 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12176 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12177 | /* no repeat, return original string */ |
| 12178 | if (len == 1) |
| 12179 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12180 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12181 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | return NULL; |
| 12183 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12184 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12185 | PyErr_SetString(PyExc_OverflowError, |
| 12186 | "repeated string is too long"); |
| 12187 | return NULL; |
| 12188 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12190 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12191 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12192 | if (!u) |
| 12193 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12194 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12197 | const int kind = PyUnicode_KIND(str); |
| 12198 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12199 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12200 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12201 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12202 | } |
| 12203 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12204 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12205 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12206 | ucs2[n] = fill_char; |
| 12207 | } else { |
| 12208 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12209 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12210 | for (n = 0; n < len; ++n) |
| 12211 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12212 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12213 | } |
| 12214 | else { |
| 12215 | /* number of characters copied this far */ |
| 12216 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12217 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12218 | char *to = (char *) PyUnicode_DATA(u); |
| 12219 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12220 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12221 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12222 | n = (done <= nchars-done) ? done : nchars-done; |
| 12223 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12224 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12225 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | } |
| 12227 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12228 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12229 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12230 | } |
| 12231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12232 | PyObject * |
| 12233 | PyUnicode_Replace(PyObject *obj, |
| 12234 | PyObject *subobj, |
| 12235 | PyObject *replobj, |
| 12236 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12237 | { |
| 12238 | PyObject *self; |
| 12239 | PyObject *str1; |
| 12240 | PyObject *str2; |
| 12241 | PyObject *result; |
| 12242 | |
| 12243 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12244 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12245 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12246 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12247 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12248 | Py_DECREF(self); |
| 12249 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12250 | } |
| 12251 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12252 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12253 | Py_DECREF(self); |
| 12254 | Py_DECREF(str1); |
| 12255 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12257 | if (PyUnicode_READY(self) == -1 || |
| 12258 | PyUnicode_READY(str1) == -1 || |
| 12259 | PyUnicode_READY(str2) == -1) |
| 12260 | result = NULL; |
| 12261 | else |
| 12262 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | Py_DECREF(self); |
| 12264 | Py_DECREF(str1); |
| 12265 | Py_DECREF(str2); |
| 12266 | return result; |
| 12267 | } |
| 12268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12269 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12270 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | \n\ |
| 12272 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12273 | old replaced by new. If the optional argument count is\n\ |
| 12274 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | |
| 12276 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12277 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12279 | PyObject *str1; |
| 12280 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12281 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12282 | PyObject *result; |
| 12283 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12284 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12286 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12287 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12288 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12289 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12290 | return NULL; |
| 12291 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12292 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12293 | Py_DECREF(str1); |
| 12294 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12295 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12296 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12297 | result = NULL; |
| 12298 | else |
| 12299 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | |
| 12301 | Py_DECREF(str1); |
| 12302 | Py_DECREF(str2); |
| 12303 | return result; |
| 12304 | } |
| 12305 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12306 | static PyObject * |
| 12307 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12309 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | Py_ssize_t isize; |
| 12311 | Py_ssize_t osize, squote, dquote, i, o; |
| 12312 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12313 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12315 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12316 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12317 | return NULL; |
| 12318 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12319 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12320 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12321 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | /* Compute length of output, quote characters, and |
| 12323 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12324 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12325 | max = 127; |
| 12326 | squote = dquote = 0; |
| 12327 | ikind = PyUnicode_KIND(unicode); |
| 12328 | for (i = 0; i < isize; i++) { |
| 12329 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12330 | switch (ch) { |
| 12331 | case '\'': squote++; osize++; break; |
| 12332 | case '"': dquote++; osize++; break; |
| 12333 | case '\\': case '\t': case '\r': case '\n': |
| 12334 | osize += 2; break; |
| 12335 | default: |
| 12336 | /* Fast-path ASCII */ |
| 12337 | if (ch < ' ' || ch == 0x7f) |
| 12338 | osize += 4; /* \xHH */ |
| 12339 | else if (ch < 0x7f) |
| 12340 | osize++; |
| 12341 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12342 | osize++; |
| 12343 | max = ch > max ? ch : max; |
| 12344 | } |
| 12345 | else if (ch < 0x100) |
| 12346 | osize += 4; /* \xHH */ |
| 12347 | else if (ch < 0x10000) |
| 12348 | osize += 6; /* \uHHHH */ |
| 12349 | else |
| 12350 | osize += 10; /* \uHHHHHHHH */ |
| 12351 | } |
| 12352 | } |
| 12353 | |
| 12354 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12355 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12356 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12357 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12358 | if (dquote) |
| 12359 | /* Both squote and dquote present. Use squote, |
| 12360 | and escape them */ |
| 12361 | osize += squote; |
| 12362 | else |
| 12363 | quote = '"'; |
| 12364 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12365 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12366 | |
| 12367 | repr = PyUnicode_New(osize, max); |
| 12368 | if (repr == NULL) |
| 12369 | return NULL; |
| 12370 | okind = PyUnicode_KIND(repr); |
| 12371 | odata = PyUnicode_DATA(repr); |
| 12372 | |
| 12373 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12374 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12375 | if (unchanged) { |
| 12376 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12377 | unicode, 0, |
| 12378 | isize); |
| 12379 | } |
| 12380 | else { |
| 12381 | for (i = 0, o = 1; i < isize; i++) { |
| 12382 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12383 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12384 | /* Escape quotes and backslashes */ |
| 12385 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12386 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12387 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12388 | continue; |
| 12389 | } |
| 12390 | |
| 12391 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12392 | if (ch == '\t') { |
| 12393 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12394 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12395 | } |
| 12396 | else if (ch == '\n') { |
| 12397 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12398 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12399 | } |
| 12400 | else if (ch == '\r') { |
| 12401 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12402 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12403 | } |
| 12404 | |
| 12405 | /* Map non-printable US ASCII to '\xhh' */ |
| 12406 | else if (ch < ' ' || ch == 0x7F) { |
| 12407 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12408 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12409 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12410 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12411 | } |
| 12412 | |
| 12413 | /* Copy ASCII characters as-is */ |
| 12414 | else if (ch < 0x7F) { |
| 12415 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12416 | } |
| 12417 | |
| 12418 | /* Non-ASCII characters */ |
| 12419 | else { |
| 12420 | /* Map Unicode whitespace and control characters |
| 12421 | (categories Z* and C* except ASCII space) |
| 12422 | */ |
| 12423 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12424 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12425 | /* Map 8-bit characters to '\xhh' */ |
| 12426 | if (ch <= 0xff) { |
| 12427 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12428 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12429 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12430 | } |
| 12431 | /* Map 16-bit characters to '\uxxxx' */ |
| 12432 | else if (ch <= 0xffff) { |
| 12433 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12435 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12436 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12437 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12438 | } |
| 12439 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12440 | else { |
| 12441 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12442 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12443 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12444 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12445 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12446 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12447 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12448 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12449 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12450 | } |
| 12451 | } |
| 12452 | /* Copy characters as-is */ |
| 12453 | else { |
| 12454 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12455 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12456 | } |
| 12457 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12458 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12460 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12461 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | } |
| 12463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12464 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12465 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12466 | \n\ |
| 12467 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12468 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12469 | arguments start and end are interpreted as in slice notation.\n\ |
| 12470 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12471 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | |
| 12473 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12474 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12475 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12476 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12477 | Py_ssize_t start; |
| 12478 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12479 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12480 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12481 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12482 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12483 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12485 | if (PyUnicode_READY(self) == -1) { |
| 12486 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12487 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12488 | } |
| 12489 | if (PyUnicode_READY(substring) == -1) { |
| 12490 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12491 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12492 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12493 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12494 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | |
| 12496 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12497 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | if (result == -2) |
| 12499 | return NULL; |
| 12500 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12501 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12502 | } |
| 12503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12504 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12505 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12507 | 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] | 12508 | |
| 12509 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12510 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12511 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12512 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12513 | Py_ssize_t start; |
| 12514 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12515 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12516 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12517 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12518 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12519 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12521 | if (PyUnicode_READY(self) == -1) { |
| 12522 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12523 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12524 | } |
| 12525 | if (PyUnicode_READY(substring) == -1) { |
| 12526 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12527 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12528 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12530 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | |
| 12532 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12533 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12534 | if (result == -2) |
| 12535 | return NULL; |
| 12536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12537 | if (result < 0) { |
| 12538 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12539 | return NULL; |
| 12540 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12541 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12542 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | } |
| 12544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12545 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12546 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12548 | 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] | 12549 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12550 | |
| 12551 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12552 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12553 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12554 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | Py_UCS4 fillchar = ' '; |
| 12556 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12557 | 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] | 12558 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12559 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12560 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | return NULL; |
| 12562 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12563 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12564 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12565 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12566 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12567 | } |
| 12568 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12569 | PyObject * |
| 12570 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12571 | { |
| 12572 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12574 | s = PyUnicode_FromObject(s); |
| 12575 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12576 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12577 | if (sep != NULL) { |
| 12578 | sep = PyUnicode_FromObject(sep); |
| 12579 | if (sep == NULL) { |
| 12580 | Py_DECREF(s); |
| 12581 | return NULL; |
| 12582 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12583 | } |
| 12584 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12585 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | |
| 12587 | Py_DECREF(s); |
| 12588 | Py_XDECREF(sep); |
| 12589 | return result; |
| 12590 | } |
| 12591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12592 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12593 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | \n\ |
| 12595 | Return a list of the words in S, using sep as the\n\ |
| 12596 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12597 | 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] | 12598 | whitespace string is a separator and empty strings are\n\ |
| 12599 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | |
| 12601 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12602 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12603 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12604 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12605 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12606 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12607 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12608 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12609 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12610 | return NULL; |
| 12611 | |
| 12612 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12613 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12614 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12615 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12616 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12617 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12618 | } |
| 12619 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12620 | PyObject * |
| 12621 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12622 | { |
| 12623 | PyObject* str_obj; |
| 12624 | PyObject* sep_obj; |
| 12625 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12626 | int kind1, kind2, kind; |
| 12627 | void *buf1 = NULL, *buf2 = NULL; |
| 12628 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12629 | |
| 12630 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12631 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12632 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12633 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12634 | if (!sep_obj) { |
| 12635 | Py_DECREF(str_obj); |
| 12636 | return NULL; |
| 12637 | } |
| 12638 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12639 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12640 | Py_DECREF(str_obj); |
| 12641 | return NULL; |
| 12642 | } |
| 12643 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12644 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12645 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12646 | kind = Py_MAX(kind1, kind2); |
| 12647 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12648 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12649 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12650 | if (!buf1) |
| 12651 | goto onError; |
| 12652 | buf2 = PyUnicode_DATA(sep_obj); |
| 12653 | if (kind2 != kind) |
| 12654 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12655 | if (!buf2) |
| 12656 | goto onError; |
| 12657 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12658 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12659 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12660 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12661 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12662 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12663 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12664 | else |
| 12665 | 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] | 12666 | break; |
| 12667 | case PyUnicode_2BYTE_KIND: |
| 12668 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12669 | break; |
| 12670 | case PyUnicode_4BYTE_KIND: |
| 12671 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12672 | break; |
| 12673 | default: |
| 12674 | assert(0); |
| 12675 | out = 0; |
| 12676 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12677 | |
| 12678 | Py_DECREF(sep_obj); |
| 12679 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12680 | if (kind1 != kind) |
| 12681 | PyMem_Free(buf1); |
| 12682 | if (kind2 != kind) |
| 12683 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12684 | |
| 12685 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12686 | onError: |
| 12687 | Py_DECREF(sep_obj); |
| 12688 | Py_DECREF(str_obj); |
| 12689 | if (kind1 != kind && buf1) |
| 12690 | PyMem_Free(buf1); |
| 12691 | if (kind2 != kind && buf2) |
| 12692 | PyMem_Free(buf2); |
| 12693 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12694 | } |
| 12695 | |
| 12696 | |
| 12697 | PyObject * |
| 12698 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12699 | { |
| 12700 | PyObject* str_obj; |
| 12701 | PyObject* sep_obj; |
| 12702 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12703 | int kind1, kind2, kind; |
| 12704 | void *buf1 = NULL, *buf2 = NULL; |
| 12705 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12706 | |
| 12707 | str_obj = PyUnicode_FromObject(str_in); |
| 12708 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12709 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12710 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12711 | if (!sep_obj) { |
| 12712 | Py_DECREF(str_obj); |
| 12713 | return NULL; |
| 12714 | } |
| 12715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12716 | kind1 = PyUnicode_KIND(str_in); |
| 12717 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12718 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12719 | buf1 = PyUnicode_DATA(str_in); |
| 12720 | if (kind1 != kind) |
| 12721 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12722 | if (!buf1) |
| 12723 | goto onError; |
| 12724 | buf2 = PyUnicode_DATA(sep_obj); |
| 12725 | if (kind2 != kind) |
| 12726 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12727 | if (!buf2) |
| 12728 | goto onError; |
| 12729 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12730 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12731 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12732 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12733 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12734 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12735 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12736 | else |
| 12737 | 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] | 12738 | break; |
| 12739 | case PyUnicode_2BYTE_KIND: |
| 12740 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12741 | break; |
| 12742 | case PyUnicode_4BYTE_KIND: |
| 12743 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12744 | break; |
| 12745 | default: |
| 12746 | assert(0); |
| 12747 | out = 0; |
| 12748 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12749 | |
| 12750 | Py_DECREF(sep_obj); |
| 12751 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | if (kind1 != kind) |
| 12753 | PyMem_Free(buf1); |
| 12754 | if (kind2 != kind) |
| 12755 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12756 | |
| 12757 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12758 | onError: |
| 12759 | Py_DECREF(sep_obj); |
| 12760 | Py_DECREF(str_obj); |
| 12761 | if (kind1 != kind && buf1) |
| 12762 | PyMem_Free(buf1); |
| 12763 | if (kind2 != kind && buf2) |
| 12764 | PyMem_Free(buf2); |
| 12765 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12766 | } |
| 12767 | |
| 12768 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12769 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12770 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12771 | 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] | 12772 | 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] | 12773 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12774 | |
| 12775 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12776 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12777 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12778 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12779 | } |
| 12780 | |
| 12781 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12782 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12783 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12784 | 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] | 12785 | 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] | 12786 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12787 | |
| 12788 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12789 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12790 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12791 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12792 | } |
| 12793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12794 | PyObject * |
| 12795 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12796 | { |
| 12797 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12798 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12799 | s = PyUnicode_FromObject(s); |
| 12800 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12801 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | if (sep != NULL) { |
| 12803 | sep = PyUnicode_FromObject(sep); |
| 12804 | if (sep == NULL) { |
| 12805 | Py_DECREF(s); |
| 12806 | return NULL; |
| 12807 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12808 | } |
| 12809 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12810 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12811 | |
| 12812 | Py_DECREF(s); |
| 12813 | Py_XDECREF(sep); |
| 12814 | return result; |
| 12815 | } |
| 12816 | |
| 12817 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12818 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12819 | \n\ |
| 12820 | Return a list of the words in S, using sep as the\n\ |
| 12821 | delimiter string, starting at the end of the string and\n\ |
| 12822 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12823 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12824 | is a separator."); |
| 12825 | |
| 12826 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12827 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12828 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12829 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12830 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12831 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12832 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12833 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12834 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12835 | return NULL; |
| 12836 | |
| 12837 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12838 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12839 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12840 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12841 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12842 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12843 | } |
| 12844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12845 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12846 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | \n\ |
| 12848 | 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] | 12849 | 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] | 12850 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12851 | |
| 12852 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12853 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12854 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12855 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12856 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12857 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12858 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12859 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12860 | return NULL; |
| 12861 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12862 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12863 | } |
| 12864 | |
| 12865 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12866 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12868 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12869 | } |
| 12870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12871 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12873 | \n\ |
| 12874 | 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] | 12875 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12876 | |
| 12877 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12878 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12879 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12880 | if (PyUnicode_READY(self) == -1) |
| 12881 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12882 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | } |
| 12884 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12885 | /*[clinic] |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 12886 | class str |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12887 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12888 | @staticmethod |
| 12889 | str.maketrans as unicode_maketrans |
| 12890 | |
| 12891 | x: object |
| 12892 | |
| 12893 | y: unicode=NULL |
| 12894 | |
| 12895 | z: unicode=NULL |
| 12896 | |
| 12897 | / |
| 12898 | |
| 12899 | Return a translation table usable for str.translate(). |
| 12900 | |
| 12901 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12902 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12903 | Character keys will be then converted to ordinals. |
| 12904 | If there are two arguments, they must be strings of equal length, and |
| 12905 | in the resulting dictionary, each character in x will be mapped to the |
| 12906 | character at the same position in y. If there is a third argument, it |
| 12907 | must be a string, whose characters will be mapped to None in the result. |
| 12908 | [clinic]*/ |
| 12909 | |
| 12910 | PyDoc_STRVAR(unicode_maketrans__doc__, |
| 12911 | "Return a translation table usable for str.translate().\n" |
| 12912 | "\n" |
| 12913 | "str.maketrans(x, y=None, z=None)\n" |
| 12914 | "\n" |
| 12915 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12916 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12917 | "Character keys will be then converted to ordinals.\n" |
| 12918 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12919 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12920 | "character at the same position in y. If there is a third argument, it\n" |
| 12921 | "must be a string, whose characters will be mapped to None in the result."); |
| 12922 | |
| 12923 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12924 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12925 | |
| 12926 | static PyObject * |
| 12927 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
| 12928 | |
| 12929 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12930 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12931 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12932 | PyObject *return_value = NULL; |
| 12933 | PyObject *x; |
| 12934 | PyObject *y = NULL; |
| 12935 | PyObject *z = NULL; |
| 12936 | |
| 12937 | if (!PyArg_ParseTuple(args, |
| 12938 | "O|UU:maketrans", |
| 12939 | &x, &y, &z)) |
| 12940 | goto exit; |
| 12941 | return_value = unicode_maketrans_impl(x, y, z); |
| 12942 | |
| 12943 | exit: |
| 12944 | return return_value; |
| 12945 | } |
| 12946 | |
| 12947 | static PyObject * |
| 12948 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
| 12949 | /*[clinic checksum: 137db9c3199e7906b7967009f511c24fa3235b5f]*/ |
| 12950 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12951 | PyObject *new = NULL, *key, *value; |
| 12952 | Py_ssize_t i = 0; |
| 12953 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12954 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12955 | new = PyDict_New(); |
| 12956 | if (!new) |
| 12957 | return NULL; |
| 12958 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12959 | int x_kind, y_kind, z_kind; |
| 12960 | void *x_data, *y_data, *z_data; |
| 12961 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12962 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12963 | if (!PyUnicode_Check(x)) { |
| 12964 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12965 | "be a string if there is a second argument"); |
| 12966 | goto err; |
| 12967 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12968 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12969 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12970 | "arguments must have equal length"); |
| 12971 | goto err; |
| 12972 | } |
| 12973 | /* 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] | 12974 | x_kind = PyUnicode_KIND(x); |
| 12975 | y_kind = PyUnicode_KIND(y); |
| 12976 | x_data = PyUnicode_DATA(x); |
| 12977 | y_data = PyUnicode_DATA(y); |
| 12978 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12979 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12980 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12981 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12982 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12983 | if (!value) { |
| 12984 | Py_DECREF(key); |
| 12985 | goto err; |
| 12986 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12987 | res = PyDict_SetItem(new, key, value); |
| 12988 | Py_DECREF(key); |
| 12989 | Py_DECREF(value); |
| 12990 | if (res < 0) |
| 12991 | goto err; |
| 12992 | } |
| 12993 | /* create entries for deleting chars in z */ |
| 12994 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12995 | z_kind = PyUnicode_KIND(z); |
| 12996 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12997 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12998 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12999 | if (!key) |
| 13000 | goto err; |
| 13001 | res = PyDict_SetItem(new, key, Py_None); |
| 13002 | Py_DECREF(key); |
| 13003 | if (res < 0) |
| 13004 | goto err; |
| 13005 | } |
| 13006 | } |
| 13007 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13008 | int kind; |
| 13009 | void *data; |
| 13010 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13011 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13012 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13013 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13014 | "to maketrans it must be a dict"); |
| 13015 | goto err; |
| 13016 | } |
| 13017 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13018 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13019 | if (PyUnicode_Check(key)) { |
| 13020 | /* convert string keys to integer keys */ |
| 13021 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13022 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13023 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13024 | "table must be of length 1"); |
| 13025 | goto err; |
| 13026 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13027 | kind = PyUnicode_KIND(key); |
| 13028 | data = PyUnicode_DATA(key); |
| 13029 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13030 | if (!newkey) |
| 13031 | goto err; |
| 13032 | res = PyDict_SetItem(new, newkey, value); |
| 13033 | Py_DECREF(newkey); |
| 13034 | if (res < 0) |
| 13035 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13036 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13037 | /* just keep integer keys */ |
| 13038 | if (PyDict_SetItem(new, key, value) < 0) |
| 13039 | goto err; |
| 13040 | } else { |
| 13041 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13042 | "be strings or integers"); |
| 13043 | goto err; |
| 13044 | } |
| 13045 | } |
| 13046 | } |
| 13047 | return new; |
| 13048 | err: |
| 13049 | Py_DECREF(new); |
| 13050 | return NULL; |
| 13051 | } |
| 13052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13053 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13054 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13055 | \n\ |
| 13056 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13057 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13058 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13059 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13060 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13061 | |
| 13062 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13063 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13064 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13065 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | } |
| 13067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13068 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13069 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13070 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13071 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13072 | |
| 13073 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13074 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13075 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13076 | if (PyUnicode_READY(self) == -1) |
| 13077 | return NULL; |
| 13078 | if (PyUnicode_IS_ASCII(self)) |
| 13079 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13080 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13081 | } |
| 13082 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13083 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13084 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13086 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13087 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13088 | |
| 13089 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13090 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13092 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13093 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13094 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13095 | int kind; |
| 13096 | void *data; |
| 13097 | Py_UCS4 chr; |
| 13098 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13099 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13100 | return NULL; |
| 13101 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13102 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13103 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13105 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13106 | return unicode_result_unchanged(self); |
| 13107 | |
| 13108 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13109 | |
| 13110 | u = pad(self, fill, 0, '0'); |
| 13111 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13112 | if (u == NULL) |
| 13113 | return NULL; |
| 13114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13115 | kind = PyUnicode_KIND(u); |
| 13116 | data = PyUnicode_DATA(u); |
| 13117 | chr = PyUnicode_READ(kind, data, fill); |
| 13118 | |
| 13119 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13121 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13122 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13123 | } |
| 13124 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13125 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13126 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13127 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13128 | |
| 13129 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13130 | static PyObject * |
| 13131 | unicode__decimal2ascii(PyObject *self) |
| 13132 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13133 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13134 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13135 | #endif |
| 13136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13137 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13138 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13139 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13140 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13141 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13142 | With optional end, stop comparing S at that position.\n\ |
| 13143 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13144 | |
| 13145 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13146 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13147 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13148 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13149 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13150 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13151 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13152 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13153 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13154 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13155 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13157 | if (PyTuple_Check(subobj)) { |
| 13158 | Py_ssize_t i; |
| 13159 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13160 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13161 | if (substring == NULL) |
| 13162 | return NULL; |
| 13163 | result = tailmatch(self, substring, start, end, -1); |
| 13164 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13165 | if (result == -1) |
| 13166 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13167 | if (result) { |
| 13168 | Py_RETURN_TRUE; |
| 13169 | } |
| 13170 | } |
| 13171 | /* nothing matched */ |
| 13172 | Py_RETURN_FALSE; |
| 13173 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13174 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13175 | if (substring == NULL) { |
| 13176 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13177 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13178 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13179 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13180 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13181 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13182 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13183 | if (result == -1) |
| 13184 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13185 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13186 | } |
| 13187 | |
| 13188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13189 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13190 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13191 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13192 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13193 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13194 | With optional end, stop comparing S at that position.\n\ |
| 13195 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13196 | |
| 13197 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13198 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13199 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13200 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13201 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13202 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13203 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13204 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13205 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13206 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13207 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13208 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13209 | if (PyTuple_Check(subobj)) { |
| 13210 | Py_ssize_t i; |
| 13211 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13212 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13213 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13214 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13215 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13216 | result = tailmatch(self, substring, start, end, +1); |
| 13217 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13218 | if (result == -1) |
| 13219 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13220 | if (result) { |
| 13221 | Py_RETURN_TRUE; |
| 13222 | } |
| 13223 | } |
| 13224 | Py_RETURN_FALSE; |
| 13225 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13226 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13227 | if (substring == NULL) { |
| 13228 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13229 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13230 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13231 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13232 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13233 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13234 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13235 | if (result == -1) |
| 13236 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13237 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | } |
| 13239 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13240 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13241 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13242 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13243 | if (!writer->readonly) |
| 13244 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13245 | else { |
| 13246 | /* Copy-on-write mode: set buffer size to 0 so |
| 13247 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13248 | * next write. */ |
| 13249 | writer->size = 0; |
| 13250 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13251 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13252 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13253 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13254 | } |
| 13255 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13256 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13257 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13258 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13259 | memset(writer, 0, sizeof(*writer)); |
| 13260 | #ifdef Py_DEBUG |
| 13261 | writer->kind = 5; /* invalid kind */ |
| 13262 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13263 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13264 | } |
| 13265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 | int |
| 13267 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13268 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13269 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13270 | #ifdef MS_WINDOWS |
| 13271 | /* On Windows, overallocate by 50% is the best factor */ |
| 13272 | # define OVERALLOCATE_FACTOR 2 |
| 13273 | #else |
| 13274 | /* On Linux, overallocate by 25% is the best factor */ |
| 13275 | # define OVERALLOCATE_FACTOR 4 |
| 13276 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13277 | Py_ssize_t newlen; |
| 13278 | PyObject *newbuffer; |
| 13279 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13280 | assert(length > 0); |
| 13281 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13282 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13283 | PyErr_NoMemory(); |
| 13284 | return -1; |
| 13285 | } |
| 13286 | newlen = writer->pos + length; |
| 13287 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13288 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13289 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13290 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13291 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13292 | if (writer->overallocate |
| 13293 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13294 | /* overallocate to limit the number of realloc() */ |
| 13295 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13296 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13297 | if (newlen < writer->min_length) |
| 13298 | newlen = writer->min_length; |
| 13299 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13300 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13301 | if (writer->buffer == NULL) |
| 13302 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13303 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13304 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13305 | if (writer->overallocate |
| 13306 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13307 | /* overallocate to limit the number of realloc() */ |
| 13308 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13309 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13310 | if (newlen < writer->min_length) |
| 13311 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13312 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13313 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13314 | /* resize + widen */ |
| 13315 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13316 | if (newbuffer == NULL) |
| 13317 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13318 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13319 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13320 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13321 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13322 | } |
| 13323 | else { |
| 13324 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13325 | if (newbuffer == NULL) |
| 13326 | return -1; |
| 13327 | } |
| 13328 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13329 | } |
| 13330 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13331 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13332 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13333 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13334 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13335 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13336 | writer->buffer, 0, writer->pos); |
| 13337 | Py_DECREF(writer->buffer); |
| 13338 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13339 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13340 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13341 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13342 | |
| 13343 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13344 | } |
| 13345 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13346 | Py_LOCAL_INLINE(int) |
| 13347 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13348 | { |
| 13349 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13350 | return -1; |
| 13351 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13352 | writer->pos++; |
| 13353 | return 0; |
| 13354 | } |
| 13355 | |
| 13356 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13357 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13358 | { |
| 13359 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13360 | } |
| 13361 | |
| 13362 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13363 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13364 | { |
| 13365 | Py_UCS4 maxchar; |
| 13366 | Py_ssize_t len; |
| 13367 | |
| 13368 | if (PyUnicode_READY(str) == -1) |
| 13369 | return -1; |
| 13370 | len = PyUnicode_GET_LENGTH(str); |
| 13371 | if (len == 0) |
| 13372 | return 0; |
| 13373 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13374 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13375 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13376 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13377 | Py_INCREF(str); |
| 13378 | writer->buffer = str; |
| 13379 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13380 | writer->pos += len; |
| 13381 | return 0; |
| 13382 | } |
| 13383 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13384 | return -1; |
| 13385 | } |
| 13386 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13387 | str, 0, len); |
| 13388 | writer->pos += len; |
| 13389 | return 0; |
| 13390 | } |
| 13391 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13392 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13393 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13394 | Py_ssize_t start, Py_ssize_t end) |
| 13395 | { |
| 13396 | Py_UCS4 maxchar; |
| 13397 | Py_ssize_t len; |
| 13398 | |
| 13399 | if (PyUnicode_READY(str) == -1) |
| 13400 | return -1; |
| 13401 | |
| 13402 | assert(0 <= start); |
| 13403 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13404 | assert(start <= end); |
| 13405 | |
| 13406 | if (end == 0) |
| 13407 | return 0; |
| 13408 | |
| 13409 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13410 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13411 | |
| 13412 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13413 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13414 | else |
| 13415 | maxchar = writer->maxchar; |
| 13416 | len = end - start; |
| 13417 | |
| 13418 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13419 | return -1; |
| 13420 | |
| 13421 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13422 | str, start, len); |
| 13423 | writer->pos += len; |
| 13424 | return 0; |
| 13425 | } |
| 13426 | |
| 13427 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13428 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13429 | const char *ascii, Py_ssize_t len) |
| 13430 | { |
| 13431 | if (len == -1) |
| 13432 | len = strlen(ascii); |
| 13433 | |
| 13434 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13435 | |
| 13436 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13437 | PyObject *str; |
| 13438 | |
| 13439 | str = _PyUnicode_FromASCII(ascii, len); |
| 13440 | if (str == NULL) |
| 13441 | return -1; |
| 13442 | |
| 13443 | writer->readonly = 1; |
| 13444 | writer->buffer = str; |
| 13445 | _PyUnicodeWriter_Update(writer); |
| 13446 | writer->pos += len; |
| 13447 | return 0; |
| 13448 | } |
| 13449 | |
| 13450 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13451 | return -1; |
| 13452 | |
| 13453 | switch (writer->kind) |
| 13454 | { |
| 13455 | case PyUnicode_1BYTE_KIND: |
| 13456 | { |
| 13457 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13458 | Py_UCS1 *data = writer->data; |
| 13459 | |
| 13460 | Py_MEMCPY(data + writer->pos, str, len); |
| 13461 | break; |
| 13462 | } |
| 13463 | case PyUnicode_2BYTE_KIND: |
| 13464 | { |
| 13465 | _PyUnicode_CONVERT_BYTES( |
| 13466 | Py_UCS1, Py_UCS2, |
| 13467 | ascii, ascii + len, |
| 13468 | (Py_UCS2 *)writer->data + writer->pos); |
| 13469 | break; |
| 13470 | } |
| 13471 | case PyUnicode_4BYTE_KIND: |
| 13472 | { |
| 13473 | _PyUnicode_CONVERT_BYTES( |
| 13474 | Py_UCS1, Py_UCS4, |
| 13475 | ascii, ascii + len, |
| 13476 | (Py_UCS4 *)writer->data + writer->pos); |
| 13477 | break; |
| 13478 | } |
| 13479 | default: |
| 13480 | assert(0); |
| 13481 | } |
| 13482 | |
| 13483 | writer->pos += len; |
| 13484 | return 0; |
| 13485 | } |
| 13486 | |
| 13487 | int |
| 13488 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13489 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13490 | { |
| 13491 | Py_UCS4 maxchar; |
| 13492 | |
| 13493 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13494 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13495 | return -1; |
| 13496 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13497 | writer->pos += len; |
| 13498 | return 0; |
| 13499 | } |
| 13500 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13501 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13502 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13503 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13504 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13505 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13506 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13507 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13508 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13509 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13510 | str = writer->buffer; |
| 13511 | writer->buffer = NULL; |
| 13512 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13513 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13514 | } |
| 13515 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13516 | PyObject *newbuffer; |
| 13517 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13518 | if (newbuffer == NULL) { |
| 13519 | Py_DECREF(writer->buffer); |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13520 | writer->buffer = NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13521 | return NULL; |
| 13522 | } |
| 13523 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13524 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13525 | str = writer->buffer; |
| 13526 | writer->buffer = NULL; |
| 13527 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13528 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13529 | } |
| 13530 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13531 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13532 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13533 | { |
| 13534 | Py_CLEAR(writer->buffer); |
| 13535 | } |
| 13536 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13537 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13538 | |
| 13539 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13541 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13542 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13543 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13544 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13545 | PyDoc_STRVAR(format_map__doc__, |
| 13546 | "S.format_map(mapping) -> str\n\ |
| 13547 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13548 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13549 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13550 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13551 | static PyObject * |
| 13552 | unicode__format__(PyObject* self, PyObject* args) |
| 13553 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13554 | PyObject *format_spec; |
| 13555 | _PyUnicodeWriter writer; |
| 13556 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13557 | |
| 13558 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13559 | return NULL; |
| 13560 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13561 | if (PyUnicode_READY(self) == -1) |
| 13562 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13563 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13564 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13565 | self, format_spec, 0, |
| 13566 | PyUnicode_GET_LENGTH(format_spec)); |
| 13567 | if (ret == -1) { |
| 13568 | _PyUnicodeWriter_Dealloc(&writer); |
| 13569 | return NULL; |
| 13570 | } |
| 13571 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13572 | } |
| 13573 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13574 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13575 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13576 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13577 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13578 | |
| 13579 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13580 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13581 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13582 | Py_ssize_t size; |
| 13583 | |
| 13584 | /* If it's a compact object, account for base structure + |
| 13585 | character data. */ |
| 13586 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13587 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13588 | else if (PyUnicode_IS_COMPACT(v)) |
| 13589 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13590 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13591 | else { |
| 13592 | /* If it is a two-block object, account for base object, and |
| 13593 | for character block if present. */ |
| 13594 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13595 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13596 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13597 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13598 | } |
| 13599 | /* 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] | 13600 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13601 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13602 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13603 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13604 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13605 | |
| 13606 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13607 | } |
| 13608 | |
| 13609 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13610 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13611 | |
| 13612 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13613 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13614 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13615 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13616 | if (!copy) |
| 13617 | return NULL; |
| 13618 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13619 | } |
| 13620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13621 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13622 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13623 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13624 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13625 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13626 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13627 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13628 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13629 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13630 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13631 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13632 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13633 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13634 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13635 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13636 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13637 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13638 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13639 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13640 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13641 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13642 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13643 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13644 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13645 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13646 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13647 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13648 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13649 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13650 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13651 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13652 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13653 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13654 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13655 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13656 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13657 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13658 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13659 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13660 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13661 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13662 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13663 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13664 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13665 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13666 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13667 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13668 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13669 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13670 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13671 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13672 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13673 | #endif |
| 13674 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13675 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13676 | {NULL, NULL} |
| 13677 | }; |
| 13678 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13679 | static PyObject * |
| 13680 | unicode_mod(PyObject *v, PyObject *w) |
| 13681 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13682 | if (!PyUnicode_Check(v)) |
| 13683 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13684 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13685 | } |
| 13686 | |
| 13687 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13688 | 0, /*nb_add*/ |
| 13689 | 0, /*nb_subtract*/ |
| 13690 | 0, /*nb_multiply*/ |
| 13691 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13692 | }; |
| 13693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13694 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13695 | (lenfunc) unicode_length, /* sq_length */ |
| 13696 | PyUnicode_Concat, /* sq_concat */ |
| 13697 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13698 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13699 | 0, /* sq_slice */ |
| 13700 | 0, /* sq_ass_item */ |
| 13701 | 0, /* sq_ass_slice */ |
| 13702 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13703 | }; |
| 13704 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13705 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13706 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13707 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13708 | if (PyUnicode_READY(self) == -1) |
| 13709 | return NULL; |
| 13710 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13711 | if (PyIndex_Check(item)) { |
| 13712 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13713 | if (i == -1 && PyErr_Occurred()) |
| 13714 | return NULL; |
| 13715 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13716 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13717 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13718 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13719 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13720 | PyObject *result; |
| 13721 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13722 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13723 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13725 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13726 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13727 | return NULL; |
| 13728 | } |
| 13729 | |
| 13730 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13731 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13732 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13733 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13734 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13735 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13736 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13737 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13738 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13739 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13740 | src_kind = PyUnicode_KIND(self); |
| 13741 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13742 | if (!PyUnicode_IS_ASCII(self)) { |
| 13743 | kind_limit = kind_maxchar_limit(src_kind); |
| 13744 | max_char = 0; |
| 13745 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13746 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13747 | if (ch > max_char) { |
| 13748 | max_char = ch; |
| 13749 | if (max_char >= kind_limit) |
| 13750 | break; |
| 13751 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13752 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13753 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13754 | else |
| 13755 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13756 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13757 | if (result == NULL) |
| 13758 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13759 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13760 | dest_data = PyUnicode_DATA(result); |
| 13761 | |
| 13762 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13763 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13764 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13765 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13766 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13767 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13768 | } else { |
| 13769 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13770 | return NULL; |
| 13771 | } |
| 13772 | } |
| 13773 | |
| 13774 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13775 | (lenfunc)unicode_length, /* mp_length */ |
| 13776 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13777 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13778 | }; |
| 13779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13781 | /* Helpers for PyUnicode_Format() */ |
| 13782 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13783 | struct unicode_formatter_t { |
| 13784 | PyObject *args; |
| 13785 | int args_owned; |
| 13786 | Py_ssize_t arglen, argidx; |
| 13787 | PyObject *dict; |
| 13788 | |
| 13789 | enum PyUnicode_Kind fmtkind; |
| 13790 | Py_ssize_t fmtcnt, fmtpos; |
| 13791 | void *fmtdata; |
| 13792 | PyObject *fmtstr; |
| 13793 | |
| 13794 | _PyUnicodeWriter writer; |
| 13795 | }; |
| 13796 | |
| 13797 | struct unicode_format_arg_t { |
| 13798 | Py_UCS4 ch; |
| 13799 | int flags; |
| 13800 | Py_ssize_t width; |
| 13801 | int prec; |
| 13802 | int sign; |
| 13803 | }; |
| 13804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13805 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13806 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13807 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13808 | Py_ssize_t argidx = ctx->argidx; |
| 13809 | |
| 13810 | if (argidx < ctx->arglen) { |
| 13811 | ctx->argidx++; |
| 13812 | if (ctx->arglen < 0) |
| 13813 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13814 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13815 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13816 | } |
| 13817 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13818 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13819 | return NULL; |
| 13820 | } |
| 13821 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13822 | /* 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] | 13823 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13824 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13825 | otherwise. |
| 13826 | |
| 13827 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13828 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13829 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13830 | PyObject **p_output, |
| 13831 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13832 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13833 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13834 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13835 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13836 | int prec; |
| 13837 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13839 | x = PyFloat_AsDouble(v); |
| 13840 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13841 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13842 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13843 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13844 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13845 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13846 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13847 | if (arg->flags & F_ALT) |
| 13848 | dtoa_flags = Py_DTSF_ALT; |
| 13849 | else |
| 13850 | dtoa_flags = 0; |
| 13851 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13852 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13853 | return -1; |
| 13854 | len = strlen(p); |
| 13855 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13856 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13857 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13858 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13859 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13860 | } |
| 13861 | else |
| 13862 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13863 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13864 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13865 | } |
| 13866 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13867 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13868 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13869 | * Python's regular ints. |
| 13870 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13871 | * The output string is of the form |
| 13872 | * "-"? ("0x" | "0X")? digit+ |
| 13873 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13874 | * set in flags. The case of hex digits will be correct, |
| 13875 | * There will be at least prec digits, zero-filled on the left if |
| 13876 | * necessary to get that many. |
| 13877 | * val object to be converted |
| 13878 | * flags bitmask of format flags; only F_ALT is looked at |
| 13879 | * prec minimum number of digits; 0-fill on left if needed |
| 13880 | * type a character in [duoxX]; u acts the same as d |
| 13881 | * |
| 13882 | * CAUTION: o, x and X conversions on regular ints can never |
| 13883 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13884 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13885 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13886 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13887 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13888 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13889 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13890 | Py_ssize_t i; |
| 13891 | int sign; /* 1 if '-', else 0 */ |
| 13892 | int len; /* number of characters */ |
| 13893 | Py_ssize_t llen; |
| 13894 | int numdigits; /* len == numnondigits + numdigits */ |
| 13895 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13896 | int prec = arg->prec; |
| 13897 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13898 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13899 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13900 | if (prec > INT_MAX-3) { |
| 13901 | PyErr_SetString(PyExc_OverflowError, |
| 13902 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13903 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13904 | } |
| 13905 | |
| 13906 | assert(PyLong_Check(val)); |
| 13907 | |
| 13908 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13909 | default: |
| 13910 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13911 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13912 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13913 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13914 | /* int and int subclasses should print numerically when a numeric */ |
| 13915 | /* format code is used (see issue18780) */ |
| 13916 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13917 | break; |
| 13918 | case 'o': |
| 13919 | numnondigits = 2; |
| 13920 | result = PyNumber_ToBase(val, 8); |
| 13921 | break; |
| 13922 | case 'x': |
| 13923 | case 'X': |
| 13924 | numnondigits = 2; |
| 13925 | result = PyNumber_ToBase(val, 16); |
| 13926 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13927 | } |
| 13928 | if (!result) |
| 13929 | return NULL; |
| 13930 | |
| 13931 | assert(unicode_modifiable(result)); |
| 13932 | assert(PyUnicode_IS_READY(result)); |
| 13933 | assert(PyUnicode_IS_ASCII(result)); |
| 13934 | |
| 13935 | /* To modify the string in-place, there can only be one reference. */ |
| 13936 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13937 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13938 | PyErr_BadInternalCall(); |
| 13939 | return NULL; |
| 13940 | } |
| 13941 | buf = PyUnicode_DATA(result); |
| 13942 | llen = PyUnicode_GET_LENGTH(result); |
| 13943 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13944 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13945 | PyErr_SetString(PyExc_ValueError, |
| 13946 | "string too large in _PyBytes_FormatLong"); |
| 13947 | return NULL; |
| 13948 | } |
| 13949 | len = (int)llen; |
| 13950 | sign = buf[0] == '-'; |
| 13951 | numnondigits += sign; |
| 13952 | numdigits = len - numnondigits; |
| 13953 | assert(numdigits > 0); |
| 13954 | |
| 13955 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13956 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13957 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13958 | assert(buf[sign] == '0'); |
| 13959 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13960 | buf[sign+1] == 'o'); |
| 13961 | numnondigits -= 2; |
| 13962 | buf += 2; |
| 13963 | len -= 2; |
| 13964 | if (sign) |
| 13965 | buf[0] = '-'; |
| 13966 | assert(len == numnondigits + numdigits); |
| 13967 | assert(numdigits > 0); |
| 13968 | } |
| 13969 | |
| 13970 | /* Fill with leading zeroes to meet minimum width. */ |
| 13971 | if (prec > numdigits) { |
| 13972 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13973 | numnondigits + prec); |
| 13974 | char *b1; |
| 13975 | if (!r1) { |
| 13976 | Py_DECREF(result); |
| 13977 | return NULL; |
| 13978 | } |
| 13979 | b1 = PyBytes_AS_STRING(r1); |
| 13980 | for (i = 0; i < numnondigits; ++i) |
| 13981 | *b1++ = *buf++; |
| 13982 | for (i = 0; i < prec - numdigits; i++) |
| 13983 | *b1++ = '0'; |
| 13984 | for (i = 0; i < numdigits; i++) |
| 13985 | *b1++ = *buf++; |
| 13986 | *b1 = '\0'; |
| 13987 | Py_DECREF(result); |
| 13988 | result = r1; |
| 13989 | buf = PyBytes_AS_STRING(result); |
| 13990 | len = numnondigits + prec; |
| 13991 | } |
| 13992 | |
| 13993 | /* Fix up case for hex conversions. */ |
| 13994 | if (type == 'X') { |
| 13995 | /* Need to convert all lower case letters to upper case. |
| 13996 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13997 | for (i = 0; i < len; i++) |
| 13998 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13999 | buf[i] -= 'a'-'A'; |
| 14000 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14001 | if (!PyUnicode_Check(result) |
| 14002 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14003 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14004 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14005 | Py_DECREF(result); |
| 14006 | result = unicode; |
| 14007 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14008 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14009 | if (PyUnicode_Resize(&result, len) < 0) |
| 14010 | Py_CLEAR(result); |
| 14011 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14012 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14013 | } |
| 14014 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14015 | /* Format an integer. |
| 14016 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14017 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14018 | * -1 and raise an exception on error */ |
| 14019 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14020 | mainformatlong(PyObject *v, |
| 14021 | struct unicode_format_arg_t *arg, |
| 14022 | PyObject **p_output, |
| 14023 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14024 | { |
| 14025 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14026 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14027 | |
| 14028 | if (!PyNumber_Check(v)) |
| 14029 | goto wrongtype; |
| 14030 | |
| 14031 | if (!PyLong_Check(v)) { |
| 14032 | iobj = PyNumber_Long(v); |
| 14033 | if (iobj == NULL) { |
| 14034 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14035 | goto wrongtype; |
| 14036 | return -1; |
| 14037 | } |
| 14038 | assert(PyLong_Check(iobj)); |
| 14039 | } |
| 14040 | else { |
| 14041 | iobj = v; |
| 14042 | Py_INCREF(iobj); |
| 14043 | } |
| 14044 | |
| 14045 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14046 | && arg->width == -1 && arg->prec == -1 |
| 14047 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14048 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14049 | { |
| 14050 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14051 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14052 | int base; |
| 14053 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14054 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14055 | { |
| 14056 | default: |
| 14057 | assert(0 && "'type' not in [diuoxX]"); |
| 14058 | case 'd': |
| 14059 | case 'i': |
| 14060 | case 'u': |
| 14061 | base = 10; |
| 14062 | break; |
| 14063 | case 'o': |
| 14064 | base = 8; |
| 14065 | break; |
| 14066 | case 'x': |
| 14067 | case 'X': |
| 14068 | base = 16; |
| 14069 | break; |
| 14070 | } |
| 14071 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14072 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14073 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14074 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14075 | } |
| 14076 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14077 | return 1; |
| 14078 | } |
| 14079 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14080 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14081 | Py_DECREF(iobj); |
| 14082 | if (res == NULL) |
| 14083 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14084 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14085 | return 0; |
| 14086 | |
| 14087 | wrongtype: |
| 14088 | PyErr_Format(PyExc_TypeError, |
| 14089 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14090 | "not %.200s", |
| 14091 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14092 | return -1; |
| 14093 | } |
| 14094 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14095 | static Py_UCS4 |
| 14096 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14097 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14098 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14099 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14100 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14101 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14102 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14103 | goto onError; |
| 14104 | } |
| 14105 | else { |
| 14106 | /* Integer input truncated to a character */ |
| 14107 | long x; |
| 14108 | x = PyLong_AsLong(v); |
| 14109 | if (x == -1 && PyErr_Occurred()) |
| 14110 | goto onError; |
| 14111 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14112 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14113 | PyErr_SetString(PyExc_OverflowError, |
| 14114 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14115 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14116 | } |
| 14117 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14118 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14119 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14120 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14121 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14122 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14123 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14124 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14125 | } |
| 14126 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14127 | /* Parse options of an argument: flags, width, precision. |
| 14128 | Handle also "%(name)" syntax. |
| 14129 | |
| 14130 | Return 0 if the argument has been formatted into arg->str. |
| 14131 | Return 1 if the argument has been written into ctx->writer, |
| 14132 | Raise an exception and return -1 on error. */ |
| 14133 | static int |
| 14134 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14135 | struct unicode_format_arg_t *arg) |
| 14136 | { |
| 14137 | #define FORMAT_READ(ctx) \ |
| 14138 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14139 | |
| 14140 | PyObject *v; |
| 14141 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14142 | if (arg->ch == '(') { |
| 14143 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14144 | Py_ssize_t keystart; |
| 14145 | Py_ssize_t keylen; |
| 14146 | PyObject *key; |
| 14147 | int pcount = 1; |
| 14148 | |
| 14149 | if (ctx->dict == NULL) { |
| 14150 | PyErr_SetString(PyExc_TypeError, |
| 14151 | "format requires a mapping"); |
| 14152 | return -1; |
| 14153 | } |
| 14154 | ++ctx->fmtpos; |
| 14155 | --ctx->fmtcnt; |
| 14156 | keystart = ctx->fmtpos; |
| 14157 | /* Skip over balanced parentheses */ |
| 14158 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14159 | arg->ch = FORMAT_READ(ctx); |
| 14160 | if (arg->ch == ')') |
| 14161 | --pcount; |
| 14162 | else if (arg->ch == '(') |
| 14163 | ++pcount; |
| 14164 | ctx->fmtpos++; |
| 14165 | } |
| 14166 | keylen = ctx->fmtpos - keystart - 1; |
| 14167 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14168 | PyErr_SetString(PyExc_ValueError, |
| 14169 | "incomplete format key"); |
| 14170 | return -1; |
| 14171 | } |
| 14172 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14173 | keystart, keystart + keylen); |
| 14174 | if (key == NULL) |
| 14175 | return -1; |
| 14176 | if (ctx->args_owned) { |
| 14177 | Py_DECREF(ctx->args); |
| 14178 | ctx->args_owned = 0; |
| 14179 | } |
| 14180 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14181 | Py_DECREF(key); |
| 14182 | if (ctx->args == NULL) |
| 14183 | return -1; |
| 14184 | ctx->args_owned = 1; |
| 14185 | ctx->arglen = -1; |
| 14186 | ctx->argidx = -2; |
| 14187 | } |
| 14188 | |
| 14189 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14190 | while (--ctx->fmtcnt >= 0) { |
| 14191 | arg->ch = FORMAT_READ(ctx); |
| 14192 | ctx->fmtpos++; |
| 14193 | switch (arg->ch) { |
| 14194 | case '-': arg->flags |= F_LJUST; continue; |
| 14195 | case '+': arg->flags |= F_SIGN; continue; |
| 14196 | case ' ': arg->flags |= F_BLANK; continue; |
| 14197 | case '#': arg->flags |= F_ALT; continue; |
| 14198 | case '0': arg->flags |= F_ZERO; continue; |
| 14199 | } |
| 14200 | break; |
| 14201 | } |
| 14202 | |
| 14203 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14204 | if (arg->ch == '*') { |
| 14205 | v = unicode_format_getnextarg(ctx); |
| 14206 | if (v == NULL) |
| 14207 | return -1; |
| 14208 | if (!PyLong_Check(v)) { |
| 14209 | PyErr_SetString(PyExc_TypeError, |
| 14210 | "* wants int"); |
| 14211 | return -1; |
| 14212 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14213 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14214 | if (arg->width == -1 && PyErr_Occurred()) |
| 14215 | return -1; |
| 14216 | if (arg->width < 0) { |
| 14217 | arg->flags |= F_LJUST; |
| 14218 | arg->width = -arg->width; |
| 14219 | } |
| 14220 | if (--ctx->fmtcnt >= 0) { |
| 14221 | arg->ch = FORMAT_READ(ctx); |
| 14222 | ctx->fmtpos++; |
| 14223 | } |
| 14224 | } |
| 14225 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14226 | arg->width = arg->ch - '0'; |
| 14227 | while (--ctx->fmtcnt >= 0) { |
| 14228 | arg->ch = FORMAT_READ(ctx); |
| 14229 | ctx->fmtpos++; |
| 14230 | if (arg->ch < '0' || arg->ch > '9') |
| 14231 | break; |
| 14232 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14233 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14234 | '0' and '9', casting to int is safe. */ |
| 14235 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14236 | PyErr_SetString(PyExc_ValueError, |
| 14237 | "width too big"); |
| 14238 | return -1; |
| 14239 | } |
| 14240 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14241 | } |
| 14242 | } |
| 14243 | |
| 14244 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14245 | if (arg->ch == '.') { |
| 14246 | arg->prec = 0; |
| 14247 | if (--ctx->fmtcnt >= 0) { |
| 14248 | arg->ch = FORMAT_READ(ctx); |
| 14249 | ctx->fmtpos++; |
| 14250 | } |
| 14251 | if (arg->ch == '*') { |
| 14252 | v = unicode_format_getnextarg(ctx); |
| 14253 | if (v == NULL) |
| 14254 | return -1; |
| 14255 | if (!PyLong_Check(v)) { |
| 14256 | PyErr_SetString(PyExc_TypeError, |
| 14257 | "* wants int"); |
| 14258 | return -1; |
| 14259 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14260 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14261 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14262 | return -1; |
| 14263 | if (arg->prec < 0) |
| 14264 | arg->prec = 0; |
| 14265 | if (--ctx->fmtcnt >= 0) { |
| 14266 | arg->ch = FORMAT_READ(ctx); |
| 14267 | ctx->fmtpos++; |
| 14268 | } |
| 14269 | } |
| 14270 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14271 | arg->prec = arg->ch - '0'; |
| 14272 | while (--ctx->fmtcnt >= 0) { |
| 14273 | arg->ch = FORMAT_READ(ctx); |
| 14274 | ctx->fmtpos++; |
| 14275 | if (arg->ch < '0' || arg->ch > '9') |
| 14276 | break; |
| 14277 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14278 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14279 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14280 | return -1; |
| 14281 | } |
| 14282 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14283 | } |
| 14284 | } |
| 14285 | } |
| 14286 | |
| 14287 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14288 | if (ctx->fmtcnt >= 0) { |
| 14289 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14290 | if (--ctx->fmtcnt >= 0) { |
| 14291 | arg->ch = FORMAT_READ(ctx); |
| 14292 | ctx->fmtpos++; |
| 14293 | } |
| 14294 | } |
| 14295 | } |
| 14296 | if (ctx->fmtcnt < 0) { |
| 14297 | PyErr_SetString(PyExc_ValueError, |
| 14298 | "incomplete format"); |
| 14299 | return -1; |
| 14300 | } |
| 14301 | return 0; |
| 14302 | |
| 14303 | #undef FORMAT_READ |
| 14304 | } |
| 14305 | |
| 14306 | /* Format one argument. Supported conversion specifiers: |
| 14307 | |
| 14308 | - "s", "r", "a": any type |
| 14309 | - "i", "d", "u", "o", "x", "X": int |
| 14310 | - "e", "E", "f", "F", "g", "G": float |
| 14311 | - "c": int or str (1 character) |
| 14312 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14313 | When possible, the output is written directly into the Unicode writer |
| 14314 | (ctx->writer). A string is created when padding is required. |
| 14315 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14316 | Return 0 if the argument has been formatted into *p_str, |
| 14317 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14318 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14319 | static int |
| 14320 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14321 | struct unicode_format_arg_t *arg, |
| 14322 | PyObject **p_str) |
| 14323 | { |
| 14324 | PyObject *v; |
| 14325 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14326 | |
| 14327 | if (ctx->fmtcnt == 0) |
| 14328 | ctx->writer.overallocate = 0; |
| 14329 | |
| 14330 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14331 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14332 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14333 | return 1; |
| 14334 | } |
| 14335 | |
| 14336 | v = unicode_format_getnextarg(ctx); |
| 14337 | if (v == NULL) |
| 14338 | return -1; |
| 14339 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14340 | |
| 14341 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14342 | case 's': |
| 14343 | case 'r': |
| 14344 | case 'a': |
| 14345 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14346 | /* Fast path */ |
| 14347 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14348 | return -1; |
| 14349 | return 1; |
| 14350 | } |
| 14351 | |
| 14352 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14353 | *p_str = v; |
| 14354 | Py_INCREF(*p_str); |
| 14355 | } |
| 14356 | else { |
| 14357 | if (arg->ch == 's') |
| 14358 | *p_str = PyObject_Str(v); |
| 14359 | else if (arg->ch == 'r') |
| 14360 | *p_str = PyObject_Repr(v); |
| 14361 | else |
| 14362 | *p_str = PyObject_ASCII(v); |
| 14363 | } |
| 14364 | break; |
| 14365 | |
| 14366 | case 'i': |
| 14367 | case 'd': |
| 14368 | case 'u': |
| 14369 | case 'o': |
| 14370 | case 'x': |
| 14371 | case 'X': |
| 14372 | { |
| 14373 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14374 | if (ret != 0) |
| 14375 | return ret; |
| 14376 | arg->sign = 1; |
| 14377 | break; |
| 14378 | } |
| 14379 | |
| 14380 | case 'e': |
| 14381 | case 'E': |
| 14382 | case 'f': |
| 14383 | case 'F': |
| 14384 | case 'g': |
| 14385 | case 'G': |
| 14386 | if (arg->width == -1 && arg->prec == -1 |
| 14387 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14388 | { |
| 14389 | /* Fast path */ |
| 14390 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14391 | return -1; |
| 14392 | return 1; |
| 14393 | } |
| 14394 | |
| 14395 | arg->sign = 1; |
| 14396 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14397 | return -1; |
| 14398 | break; |
| 14399 | |
| 14400 | case 'c': |
| 14401 | { |
| 14402 | Py_UCS4 ch = formatchar(v); |
| 14403 | if (ch == (Py_UCS4) -1) |
| 14404 | return -1; |
| 14405 | if (arg->width == -1 && arg->prec == -1) { |
| 14406 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14407 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14408 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14409 | return 1; |
| 14410 | } |
| 14411 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14412 | break; |
| 14413 | } |
| 14414 | |
| 14415 | default: |
| 14416 | PyErr_Format(PyExc_ValueError, |
| 14417 | "unsupported format character '%c' (0x%x) " |
| 14418 | "at index %zd", |
| 14419 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14420 | (int)arg->ch, |
| 14421 | ctx->fmtpos - 1); |
| 14422 | return -1; |
| 14423 | } |
| 14424 | if (*p_str == NULL) |
| 14425 | return -1; |
| 14426 | assert (PyUnicode_Check(*p_str)); |
| 14427 | return 0; |
| 14428 | } |
| 14429 | |
| 14430 | static int |
| 14431 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14432 | struct unicode_format_arg_t *arg, |
| 14433 | PyObject *str) |
| 14434 | { |
| 14435 | Py_ssize_t len; |
| 14436 | enum PyUnicode_Kind kind; |
| 14437 | void *pbuf; |
| 14438 | Py_ssize_t pindex; |
| 14439 | Py_UCS4 signchar; |
| 14440 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14441 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14442 | Py_ssize_t sublen; |
| 14443 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14444 | Py_UCS4 fill; |
| 14445 | |
| 14446 | fill = ' '; |
| 14447 | if (arg->sign && arg->flags & F_ZERO) |
| 14448 | fill = '0'; |
| 14449 | |
| 14450 | if (PyUnicode_READY(str) == -1) |
| 14451 | return -1; |
| 14452 | |
| 14453 | len = PyUnicode_GET_LENGTH(str); |
| 14454 | if ((arg->width == -1 || arg->width <= len) |
| 14455 | && (arg->prec == -1 || arg->prec >= len) |
| 14456 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14457 | { |
| 14458 | /* Fast path */ |
| 14459 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14460 | return -1; |
| 14461 | return 0; |
| 14462 | } |
| 14463 | |
| 14464 | /* Truncate the string for "s", "r" and "a" formats |
| 14465 | if the precision is set */ |
| 14466 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14467 | if (arg->prec >= 0 && len > arg->prec) |
| 14468 | len = arg->prec; |
| 14469 | } |
| 14470 | |
| 14471 | /* Adjust sign and width */ |
| 14472 | kind = PyUnicode_KIND(str); |
| 14473 | pbuf = PyUnicode_DATA(str); |
| 14474 | pindex = 0; |
| 14475 | signchar = '\0'; |
| 14476 | if (arg->sign) { |
| 14477 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14478 | if (ch == '-' || ch == '+') { |
| 14479 | signchar = ch; |
| 14480 | len--; |
| 14481 | pindex++; |
| 14482 | } |
| 14483 | else if (arg->flags & F_SIGN) |
| 14484 | signchar = '+'; |
| 14485 | else if (arg->flags & F_BLANK) |
| 14486 | signchar = ' '; |
| 14487 | else |
| 14488 | arg->sign = 0; |
| 14489 | } |
| 14490 | if (arg->width < len) |
| 14491 | arg->width = len; |
| 14492 | |
| 14493 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14494 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14495 | if (!(arg->flags & F_LJUST)) { |
| 14496 | if (arg->sign) { |
| 14497 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14498 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14499 | } |
| 14500 | else { |
| 14501 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14502 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14503 | } |
| 14504 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14505 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14506 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14507 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14508 | } |
| 14509 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14510 | buflen = arg->width; |
| 14511 | if (arg->sign && len == arg->width) |
| 14512 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14513 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14514 | return -1; |
| 14515 | |
| 14516 | /* Write the sign if needed */ |
| 14517 | if (arg->sign) { |
| 14518 | if (fill != ' ') { |
| 14519 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14520 | writer->pos += 1; |
| 14521 | } |
| 14522 | if (arg->width > len) |
| 14523 | arg->width--; |
| 14524 | } |
| 14525 | |
| 14526 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14527 | if the alternate form is used. |
| 14528 | For example, write "0x" for the "%#x" format. */ |
| 14529 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14530 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14531 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14532 | if (fill != ' ') { |
| 14533 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14534 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14535 | writer->pos += 2; |
| 14536 | pindex += 2; |
| 14537 | } |
| 14538 | arg->width -= 2; |
| 14539 | if (arg->width < 0) |
| 14540 | arg->width = 0; |
| 14541 | len -= 2; |
| 14542 | } |
| 14543 | |
| 14544 | /* Pad left with the fill character if needed */ |
| 14545 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14546 | sublen = arg->width - len; |
| 14547 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14548 | writer->pos += sublen; |
| 14549 | arg->width = len; |
| 14550 | } |
| 14551 | |
| 14552 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14553 | the alternate form is used */ |
| 14554 | if (fill == ' ') { |
| 14555 | if (arg->sign) { |
| 14556 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14557 | writer->pos += 1; |
| 14558 | } |
| 14559 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14560 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14561 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14562 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14563 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14564 | writer->pos += 2; |
| 14565 | pindex += 2; |
| 14566 | } |
| 14567 | } |
| 14568 | |
| 14569 | /* Write characters */ |
| 14570 | if (len) { |
| 14571 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14572 | str, pindex, len); |
| 14573 | writer->pos += len; |
| 14574 | } |
| 14575 | |
| 14576 | /* Pad right with the fill character if needed */ |
| 14577 | if (arg->width > len) { |
| 14578 | sublen = arg->width - len; |
| 14579 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14580 | writer->pos += sublen; |
| 14581 | } |
| 14582 | return 0; |
| 14583 | } |
| 14584 | |
| 14585 | /* Helper of PyUnicode_Format(): format one arg. |
| 14586 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14587 | static int |
| 14588 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14589 | { |
| 14590 | struct unicode_format_arg_t arg; |
| 14591 | PyObject *str; |
| 14592 | int ret; |
| 14593 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14594 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14595 | arg.flags = 0; |
| 14596 | arg.width = -1; |
| 14597 | arg.prec = -1; |
| 14598 | arg.sign = 0; |
| 14599 | str = NULL; |
| 14600 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14601 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14602 | if (ret == -1) |
| 14603 | return -1; |
| 14604 | |
| 14605 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14606 | if (ret == -1) |
| 14607 | return -1; |
| 14608 | |
| 14609 | if (ret != 1) { |
| 14610 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14611 | Py_DECREF(str); |
| 14612 | if (ret == -1) |
| 14613 | return -1; |
| 14614 | } |
| 14615 | |
| 14616 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14617 | PyErr_SetString(PyExc_TypeError, |
| 14618 | "not all arguments converted during string formatting"); |
| 14619 | return -1; |
| 14620 | } |
| 14621 | return 0; |
| 14622 | } |
| 14623 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14624 | PyObject * |
| 14625 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14626 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14627 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14629 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14630 | PyErr_BadInternalCall(); |
| 14631 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14632 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14633 | |
| 14634 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14635 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14636 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14637 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14638 | Py_DECREF(ctx.fmtstr); |
| 14639 | return NULL; |
| 14640 | } |
| 14641 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14642 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14643 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14644 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14645 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14646 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14647 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14648 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14650 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14651 | ctx.arglen = PyTuple_Size(args); |
| 14652 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14653 | } |
| 14654 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14655 | ctx.arglen = -1; |
| 14656 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14657 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14658 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14659 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14660 | ctx.dict = args; |
| 14661 | else |
| 14662 | ctx.dict = NULL; |
| 14663 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14664 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14665 | while (--ctx.fmtcnt >= 0) { |
| 14666 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14667 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14668 | |
| 14669 | nonfmtpos = ctx.fmtpos++; |
| 14670 | while (ctx.fmtcnt >= 0 && |
| 14671 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14672 | ctx.fmtpos++; |
| 14673 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14674 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14675 | if (ctx.fmtcnt < 0) { |
| 14676 | ctx.fmtpos--; |
| 14677 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14678 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14679 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14680 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14681 | nonfmtpos, ctx.fmtpos) < 0) |
| 14682 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14683 | } |
| 14684 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14685 | ctx.fmtpos++; |
| 14686 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14687 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14688 | } |
| 14689 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14690 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14691 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14692 | PyErr_SetString(PyExc_TypeError, |
| 14693 | "not all arguments converted during string formatting"); |
| 14694 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14695 | } |
| 14696 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14697 | if (ctx.args_owned) { |
| 14698 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14699 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14700 | Py_DECREF(ctx.fmtstr); |
| 14701 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14703 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14704 | Py_DECREF(ctx.fmtstr); |
| 14705 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14706 | if (ctx.args_owned) { |
| 14707 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14708 | } |
| 14709 | return NULL; |
| 14710 | } |
| 14711 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14712 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14713 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14714 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14715 | static PyObject * |
| 14716 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14717 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14718 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14719 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14720 | char *encoding = NULL; |
| 14721 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14722 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14723 | if (type != &PyUnicode_Type) |
| 14724 | return unicode_subtype_new(type, args, kwds); |
| 14725 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14726 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14727 | return NULL; |
| 14728 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14729 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14730 | if (encoding == NULL && errors == NULL) |
| 14731 | return PyObject_Str(x); |
| 14732 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14733 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14734 | } |
| 14735 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14736 | static PyObject * |
| 14737 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14738 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14739 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14740 | Py_ssize_t length, char_size; |
| 14741 | int share_wstr, share_utf8; |
| 14742 | unsigned int kind; |
| 14743 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14745 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14746 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14747 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14748 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14750 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14751 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14752 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14753 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14754 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14755 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14756 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14757 | if (self == NULL) { |
| 14758 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14759 | return NULL; |
| 14760 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14761 | kind = PyUnicode_KIND(unicode); |
| 14762 | length = PyUnicode_GET_LENGTH(unicode); |
| 14763 | |
| 14764 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14765 | #ifdef Py_DEBUG |
| 14766 | _PyUnicode_HASH(self) = -1; |
| 14767 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14768 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14769 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14770 | _PyUnicode_STATE(self).interned = 0; |
| 14771 | _PyUnicode_STATE(self).kind = kind; |
| 14772 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14773 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14774 | _PyUnicode_STATE(self).ready = 1; |
| 14775 | _PyUnicode_WSTR(self) = NULL; |
| 14776 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14777 | _PyUnicode_UTF8(self) = NULL; |
| 14778 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14779 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14780 | |
| 14781 | share_utf8 = 0; |
| 14782 | share_wstr = 0; |
| 14783 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14784 | char_size = 1; |
| 14785 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14786 | share_utf8 = 1; |
| 14787 | } |
| 14788 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14789 | char_size = 2; |
| 14790 | if (sizeof(wchar_t) == 2) |
| 14791 | share_wstr = 1; |
| 14792 | } |
| 14793 | else { |
| 14794 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14795 | char_size = 4; |
| 14796 | if (sizeof(wchar_t) == 4) |
| 14797 | share_wstr = 1; |
| 14798 | } |
| 14799 | |
| 14800 | /* Ensure we won't overflow the length. */ |
| 14801 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14802 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14803 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14804 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14805 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14806 | if (data == NULL) { |
| 14807 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14808 | goto onError; |
| 14809 | } |
| 14810 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14811 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14812 | if (share_utf8) { |
| 14813 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14814 | _PyUnicode_UTF8(self) = data; |
| 14815 | } |
| 14816 | if (share_wstr) { |
| 14817 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14818 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14819 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14820 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14821 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14822 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14823 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14824 | #ifdef Py_DEBUG |
| 14825 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14826 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14827 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14828 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14829 | |
| 14830 | onError: |
| 14831 | Py_DECREF(unicode); |
| 14832 | Py_DECREF(self); |
| 14833 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14834 | } |
| 14835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14836 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14837 | "str(object='') -> str\n\ |
| 14838 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14839 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14840 | Create a new string object from the given object. If encoding or\n\ |
| 14841 | errors is specified, then the object must expose a data buffer\n\ |
| 14842 | that will be decoded using the given encoding and error handler.\n\ |
| 14843 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14844 | or repr(object).\n\ |
| 14845 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14846 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14847 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14848 | static PyObject *unicode_iter(PyObject *seq); |
| 14849 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14850 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14851 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14852 | "str", /* tp_name */ |
| 14853 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14854 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14855 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14856 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14857 | 0, /* tp_print */ |
| 14858 | 0, /* tp_getattr */ |
| 14859 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14860 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14861 | unicode_repr, /* tp_repr */ |
| 14862 | &unicode_as_number, /* tp_as_number */ |
| 14863 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14864 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14865 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14866 | 0, /* tp_call*/ |
| 14867 | (reprfunc) unicode_str, /* tp_str */ |
| 14868 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14869 | 0, /* tp_setattro */ |
| 14870 | 0, /* tp_as_buffer */ |
| 14871 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14872 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14873 | unicode_doc, /* tp_doc */ |
| 14874 | 0, /* tp_traverse */ |
| 14875 | 0, /* tp_clear */ |
| 14876 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14877 | 0, /* tp_weaklistoffset */ |
| 14878 | unicode_iter, /* tp_iter */ |
| 14879 | 0, /* tp_iternext */ |
| 14880 | unicode_methods, /* tp_methods */ |
| 14881 | 0, /* tp_members */ |
| 14882 | 0, /* tp_getset */ |
| 14883 | &PyBaseObject_Type, /* tp_base */ |
| 14884 | 0, /* tp_dict */ |
| 14885 | 0, /* tp_descr_get */ |
| 14886 | 0, /* tp_descr_set */ |
| 14887 | 0, /* tp_dictoffset */ |
| 14888 | 0, /* tp_init */ |
| 14889 | 0, /* tp_alloc */ |
| 14890 | unicode_new, /* tp_new */ |
| 14891 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14892 | }; |
| 14893 | |
| 14894 | /* Initialize the Unicode implementation */ |
| 14895 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14896 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14897 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14898 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14899 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14900 | 0x000A, /* LINE FEED */ |
| 14901 | 0x000D, /* CARRIAGE RETURN */ |
| 14902 | 0x001C, /* FILE SEPARATOR */ |
| 14903 | 0x001D, /* GROUP SEPARATOR */ |
| 14904 | 0x001E, /* RECORD SEPARATOR */ |
| 14905 | 0x0085, /* NEXT LINE */ |
| 14906 | 0x2028, /* LINE SEPARATOR */ |
| 14907 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14908 | }; |
| 14909 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14910 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14911 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14912 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14913 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14914 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14915 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14916 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14917 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14918 | |
| 14919 | /* initialize the linebreak bloom filter */ |
| 14920 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14921 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14922 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14923 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14924 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14925 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14926 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14927 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14928 | Py_FatalError("Can't initialize field name iterator type"); |
| 14929 | |
| 14930 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14931 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14932 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14933 | #ifdef HAVE_MBCS |
| 14934 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14935 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14936 | PyErr_SetFromWindowsErr(0); |
| 14937 | return -1; |
| 14938 | } |
| 14939 | #endif |
| 14940 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14941 | } |
| 14942 | |
| 14943 | /* Finalize the Unicode implementation */ |
| 14944 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14945 | int |
| 14946 | PyUnicode_ClearFreeList(void) |
| 14947 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14948 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14949 | } |
| 14950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14951 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14952 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14953 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14954 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14955 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14956 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14957 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14958 | for (i = 0; i < 256; i++) |
| 14959 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14960 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14961 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14962 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14963 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14964 | void |
| 14965 | PyUnicode_InternInPlace(PyObject **p) |
| 14966 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14967 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14968 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14969 | #ifdef Py_DEBUG |
| 14970 | assert(s != NULL); |
| 14971 | assert(_PyUnicode_CHECK(s)); |
| 14972 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14973 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14974 | return; |
| 14975 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14976 | /* If it's a subclass, we don't really know what putting |
| 14977 | it in the interned dict might do. */ |
| 14978 | if (!PyUnicode_CheckExact(s)) |
| 14979 | return; |
| 14980 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14981 | return; |
| 14982 | if (interned == NULL) { |
| 14983 | interned = PyDict_New(); |
| 14984 | if (interned == NULL) { |
| 14985 | PyErr_Clear(); /* Don't leave an exception */ |
| 14986 | return; |
| 14987 | } |
| 14988 | } |
| 14989 | /* It might be that the GetItem call fails even |
| 14990 | though the key is present in the dictionary, |
| 14991 | namely when this happens during a stack overflow. */ |
| 14992 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14993 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14994 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14995 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14996 | if (t) { |
| 14997 | Py_INCREF(t); |
| 14998 | Py_DECREF(*p); |
| 14999 | *p = t; |
| 15000 | return; |
| 15001 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15002 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15003 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15004 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15005 | PyErr_Clear(); |
| 15006 | PyThreadState_GET()->recursion_critical = 0; |
| 15007 | return; |
| 15008 | } |
| 15009 | PyThreadState_GET()->recursion_critical = 0; |
| 15010 | /* The two references in interned are not counted by refcnt. |
| 15011 | The deallocator will take care of this */ |
| 15012 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15013 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15014 | } |
| 15015 | |
| 15016 | void |
| 15017 | PyUnicode_InternImmortal(PyObject **p) |
| 15018 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15019 | PyUnicode_InternInPlace(p); |
| 15020 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15021 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15022 | Py_INCREF(*p); |
| 15023 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15024 | } |
| 15025 | |
| 15026 | PyObject * |
| 15027 | PyUnicode_InternFromString(const char *cp) |
| 15028 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15029 | PyObject *s = PyUnicode_FromString(cp); |
| 15030 | if (s == NULL) |
| 15031 | return NULL; |
| 15032 | PyUnicode_InternInPlace(&s); |
| 15033 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15034 | } |
| 15035 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15036 | void |
| 15037 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15038 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15039 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15040 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15041 | Py_ssize_t i, n; |
| 15042 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15043 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15044 | if (interned == NULL || !PyDict_Check(interned)) |
| 15045 | return; |
| 15046 | keys = PyDict_Keys(interned); |
| 15047 | if (keys == NULL || !PyList_Check(keys)) { |
| 15048 | PyErr_Clear(); |
| 15049 | return; |
| 15050 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15051 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15052 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15053 | detector, interned unicode strings are not forcibly deallocated; |
| 15054 | rather, we give them their stolen references back, and then clear |
| 15055 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15056 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15057 | n = PyList_GET_SIZE(keys); |
| 15058 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15059 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15060 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15061 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15062 | if (PyUnicode_READY(s) == -1) { |
| 15063 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15064 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15065 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15066 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15067 | case SSTATE_NOT_INTERNED: |
| 15068 | /* XXX Shouldn't happen */ |
| 15069 | break; |
| 15070 | case SSTATE_INTERNED_IMMORTAL: |
| 15071 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15072 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15073 | break; |
| 15074 | case SSTATE_INTERNED_MORTAL: |
| 15075 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15076 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15077 | break; |
| 15078 | default: |
| 15079 | Py_FatalError("Inconsistent interned string state."); |
| 15080 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15081 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15082 | } |
| 15083 | fprintf(stderr, "total size of all interned strings: " |
| 15084 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15085 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15086 | Py_DECREF(keys); |
| 15087 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15088 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15089 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15090 | |
| 15091 | |
| 15092 | /********************* Unicode Iterator **************************/ |
| 15093 | |
| 15094 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15095 | PyObject_HEAD |
| 15096 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15097 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15098 | } unicodeiterobject; |
| 15099 | |
| 15100 | static void |
| 15101 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15102 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15103 | _PyObject_GC_UNTRACK(it); |
| 15104 | Py_XDECREF(it->it_seq); |
| 15105 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15106 | } |
| 15107 | |
| 15108 | static int |
| 15109 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15110 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15111 | Py_VISIT(it->it_seq); |
| 15112 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15113 | } |
| 15114 | |
| 15115 | static PyObject * |
| 15116 | unicodeiter_next(unicodeiterobject *it) |
| 15117 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15118 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15119 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15120 | assert(it != NULL); |
| 15121 | seq = it->it_seq; |
| 15122 | if (seq == NULL) |
| 15123 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15124 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15126 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15127 | int kind = PyUnicode_KIND(seq); |
| 15128 | void *data = PyUnicode_DATA(seq); |
| 15129 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15130 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15131 | if (item != NULL) |
| 15132 | ++it->it_index; |
| 15133 | return item; |
| 15134 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15135 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15136 | Py_DECREF(seq); |
| 15137 | it->it_seq = NULL; |
| 15138 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15139 | } |
| 15140 | |
| 15141 | static PyObject * |
| 15142 | unicodeiter_len(unicodeiterobject *it) |
| 15143 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15144 | Py_ssize_t len = 0; |
| 15145 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15146 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15147 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15148 | } |
| 15149 | |
| 15150 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15151 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15152 | static PyObject * |
| 15153 | unicodeiter_reduce(unicodeiterobject *it) |
| 15154 | { |
| 15155 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15156 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15157 | it->it_seq, it->it_index); |
| 15158 | } else { |
| 15159 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15160 | if (u == NULL) |
| 15161 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15162 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15163 | } |
| 15164 | } |
| 15165 | |
| 15166 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15167 | |
| 15168 | static PyObject * |
| 15169 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15170 | { |
| 15171 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15172 | if (index == -1 && PyErr_Occurred()) |
| 15173 | return NULL; |
| 15174 | if (index < 0) |
| 15175 | index = 0; |
| 15176 | it->it_index = index; |
| 15177 | Py_RETURN_NONE; |
| 15178 | } |
| 15179 | |
| 15180 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15181 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15182 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15183 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15184 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15185 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15186 | reduce_doc}, |
| 15187 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15188 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15189 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15190 | }; |
| 15191 | |
| 15192 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15193 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15194 | "str_iterator", /* tp_name */ |
| 15195 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15196 | 0, /* tp_itemsize */ |
| 15197 | /* methods */ |
| 15198 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15199 | 0, /* tp_print */ |
| 15200 | 0, /* tp_getattr */ |
| 15201 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15202 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15203 | 0, /* tp_repr */ |
| 15204 | 0, /* tp_as_number */ |
| 15205 | 0, /* tp_as_sequence */ |
| 15206 | 0, /* tp_as_mapping */ |
| 15207 | 0, /* tp_hash */ |
| 15208 | 0, /* tp_call */ |
| 15209 | 0, /* tp_str */ |
| 15210 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15211 | 0, /* tp_setattro */ |
| 15212 | 0, /* tp_as_buffer */ |
| 15213 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15214 | 0, /* tp_doc */ |
| 15215 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15216 | 0, /* tp_clear */ |
| 15217 | 0, /* tp_richcompare */ |
| 15218 | 0, /* tp_weaklistoffset */ |
| 15219 | PyObject_SelfIter, /* tp_iter */ |
| 15220 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15221 | unicodeiter_methods, /* tp_methods */ |
| 15222 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15223 | }; |
| 15224 | |
| 15225 | static PyObject * |
| 15226 | unicode_iter(PyObject *seq) |
| 15227 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15228 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15229 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15230 | if (!PyUnicode_Check(seq)) { |
| 15231 | PyErr_BadInternalCall(); |
| 15232 | return NULL; |
| 15233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15234 | if (PyUnicode_READY(seq) == -1) |
| 15235 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15236 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15237 | if (it == NULL) |
| 15238 | return NULL; |
| 15239 | it->it_index = 0; |
| 15240 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15241 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15242 | _PyObject_GC_TRACK(it); |
| 15243 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15244 | } |
| 15245 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15246 | |
| 15247 | size_t |
| 15248 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15249 | { |
| 15250 | int res = 0; |
| 15251 | while(*u++) |
| 15252 | res++; |
| 15253 | return res; |
| 15254 | } |
| 15255 | |
| 15256 | Py_UNICODE* |
| 15257 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15258 | { |
| 15259 | Py_UNICODE *u = s1; |
| 15260 | while ((*u++ = *s2++)); |
| 15261 | return s1; |
| 15262 | } |
| 15263 | |
| 15264 | Py_UNICODE* |
| 15265 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15266 | { |
| 15267 | Py_UNICODE *u = s1; |
| 15268 | while ((*u++ = *s2++)) |
| 15269 | if (n-- == 0) |
| 15270 | break; |
| 15271 | return s1; |
| 15272 | } |
| 15273 | |
| 15274 | Py_UNICODE* |
| 15275 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15276 | { |
| 15277 | Py_UNICODE *u1 = s1; |
| 15278 | u1 += Py_UNICODE_strlen(u1); |
| 15279 | Py_UNICODE_strcpy(u1, s2); |
| 15280 | return s1; |
| 15281 | } |
| 15282 | |
| 15283 | int |
| 15284 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15285 | { |
| 15286 | while (*s1 && *s2 && *s1 == *s2) |
| 15287 | s1++, s2++; |
| 15288 | if (*s1 && *s2) |
| 15289 | return (*s1 < *s2) ? -1 : +1; |
| 15290 | if (*s1) |
| 15291 | return 1; |
| 15292 | if (*s2) |
| 15293 | return -1; |
| 15294 | return 0; |
| 15295 | } |
| 15296 | |
| 15297 | int |
| 15298 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15299 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15300 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15301 | for (; n != 0; n--) { |
| 15302 | u1 = *s1; |
| 15303 | u2 = *s2; |
| 15304 | if (u1 != u2) |
| 15305 | return (u1 < u2) ? -1 : +1; |
| 15306 | if (u1 == '\0') |
| 15307 | return 0; |
| 15308 | s1++; |
| 15309 | s2++; |
| 15310 | } |
| 15311 | return 0; |
| 15312 | } |
| 15313 | |
| 15314 | Py_UNICODE* |
| 15315 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15316 | { |
| 15317 | const Py_UNICODE *p; |
| 15318 | for (p = s; *p; p++) |
| 15319 | if (*p == c) |
| 15320 | return (Py_UNICODE*)p; |
| 15321 | return NULL; |
| 15322 | } |
| 15323 | |
| 15324 | Py_UNICODE* |
| 15325 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15326 | { |
| 15327 | const Py_UNICODE *p; |
| 15328 | p = s + Py_UNICODE_strlen(s); |
| 15329 | while (p != s) { |
| 15330 | p--; |
| 15331 | if (*p == c) |
| 15332 | return (Py_UNICODE*)p; |
| 15333 | } |
| 15334 | return NULL; |
| 15335 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15336 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15337 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15338 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15339 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15340 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15341 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15342 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15343 | if (!PyUnicode_Check(unicode)) { |
| 15344 | PyErr_BadArgument(); |
| 15345 | return NULL; |
| 15346 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15347 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15348 | if (u == NULL) |
| 15349 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15350 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15351 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15352 | PyErr_NoMemory(); |
| 15353 | return NULL; |
| 15354 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15355 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15356 | size *= sizeof(Py_UNICODE); |
| 15357 | copy = PyMem_Malloc(size); |
| 15358 | if (copy == NULL) { |
| 15359 | PyErr_NoMemory(); |
| 15360 | return NULL; |
| 15361 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15362 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15363 | return copy; |
| 15364 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15365 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15366 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15367 | to the string.Formatter class implemented in Python. */ |
| 15368 | |
| 15369 | static PyMethodDef _string_methods[] = { |
| 15370 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15371 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15372 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15373 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15374 | {NULL, NULL} |
| 15375 | }; |
| 15376 | |
| 15377 | static struct PyModuleDef _string_module = { |
| 15378 | PyModuleDef_HEAD_INIT, |
| 15379 | "_string", |
| 15380 | PyDoc_STR("string helper module"), |
| 15381 | 0, |
| 15382 | _string_methods, |
| 15383 | NULL, |
| 15384 | NULL, |
| 15385 | NULL, |
| 15386 | NULL |
| 15387 | }; |
| 15388 | |
| 15389 | PyMODINIT_FUNC |
| 15390 | PyInit__string(void) |
| 15391 | { |
| 15392 | return PyModule_Create(&_string_module); |
| 15393 | } |
| 15394 | |
| 15395 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15396 | #ifdef __cplusplus |
| 15397 | } |
| 15398 | #endif |