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 { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 143 | to_type *_to = (to_type *) to; \ |
| 144 | const from_type *_iter = (begin); \ |
| 145 | const from_type *_end = (end); \ |
| 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); |
| 899 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 900 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 901 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 902 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 903 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 904 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 906 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 907 | * the caller fails before initializing str -- unicode_resize() |
| 908 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 909 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 910 | * We don't want unicode_resize to read uninitialized memory in |
| 911 | * that case. |
| 912 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 913 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 914 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 915 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 916 | _PyUnicode_HASH(unicode) = -1; |
| 917 | _PyUnicode_STATE(unicode).interned = 0; |
| 918 | _PyUnicode_STATE(unicode).kind = 0; |
| 919 | _PyUnicode_STATE(unicode).compact = 0; |
| 920 | _PyUnicode_STATE(unicode).ready = 0; |
| 921 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 922 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 924 | _PyUnicode_UTF8(unicode) = NULL; |
| 925 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 926 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 927 | return unicode; |
| 928 | } |
| 929 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 930 | static const char* |
| 931 | unicode_kind_name(PyObject *unicode) |
| 932 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 933 | /* don't check consistency: unicode_kind_name() is called from |
| 934 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 935 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 936 | { |
| 937 | if (!PyUnicode_IS_READY(unicode)) |
| 938 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 939 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 940 | { |
| 941 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 942 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | return "legacy ascii"; |
| 944 | else |
| 945 | return "legacy latin1"; |
| 946 | case PyUnicode_2BYTE_KIND: |
| 947 | return "legacy UCS2"; |
| 948 | case PyUnicode_4BYTE_KIND: |
| 949 | return "legacy UCS4"; |
| 950 | default: |
| 951 | return "<legacy invalid kind>"; |
| 952 | } |
| 953 | } |
| 954 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 955 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 956 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 957 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 958 | return "ascii"; |
| 959 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 960 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 961 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 962 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 963 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 964 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 965 | default: |
| 966 | return "<invalid compact kind>"; |
| 967 | } |
| 968 | } |
| 969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 971 | /* Functions wrapping macros for use in debugger */ |
| 972 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 973 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | void *_PyUnicode_compact_data(void *unicode) { |
| 977 | return _PyUnicode_COMPACT_DATA(unicode); |
| 978 | } |
| 979 | void *_PyUnicode_data(void *unicode){ |
| 980 | printf("obj %p\n", unicode); |
| 981 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 982 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 983 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 984 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 985 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 986 | return PyUnicode_DATA(unicode); |
| 987 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 988 | |
| 989 | void |
| 990 | _PyUnicode_Dump(PyObject *op) |
| 991 | { |
| 992 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 993 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 994 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 995 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 996 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 997 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 998 | { |
| 999 | if (ascii->state.ascii) |
| 1000 | data = (ascii + 1); |
| 1001 | else |
| 1002 | data = (compact + 1); |
| 1003 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1004 | else |
| 1005 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1006 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1007 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1008 | if (ascii->wstr == data) |
| 1009 | printf("shared "); |
| 1010 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1011 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1012 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1013 | printf(" (%zu), ", compact->wstr_length); |
| 1014 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1015 | printf("shared "); |
| 1016 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1017 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1018 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1019 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1020 | #endif |
| 1021 | |
| 1022 | PyObject * |
| 1023 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1024 | { |
| 1025 | PyObject *obj; |
| 1026 | PyCompactUnicodeObject *unicode; |
| 1027 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1028 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1029 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | Py_ssize_t char_size; |
| 1031 | Py_ssize_t struct_size; |
| 1032 | |
| 1033 | /* Optimization for empty strings */ |
| 1034 | if (size == 0 && unicode_empty != NULL) { |
| 1035 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1036 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1039 | is_ascii = 0; |
| 1040 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1041 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1042 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1043 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | char_size = 1; |
| 1045 | is_ascii = 1; |
| 1046 | struct_size = sizeof(PyASCIIObject); |
| 1047 | } |
| 1048 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1049 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | char_size = 1; |
| 1051 | } |
| 1052 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1053 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | char_size = 2; |
| 1055 | if (sizeof(wchar_t) == 2) |
| 1056 | is_sharing = 1; |
| 1057 | } |
| 1058 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1059 | if (maxchar > MAX_UNICODE) { |
| 1060 | PyErr_SetString(PyExc_SystemError, |
| 1061 | "invalid maximum character passed to PyUnicode_New"); |
| 1062 | return NULL; |
| 1063 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1064 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1065 | char_size = 4; |
| 1066 | if (sizeof(wchar_t) == 4) |
| 1067 | is_sharing = 1; |
| 1068 | } |
| 1069 | |
| 1070 | /* Ensure we won't overflow the size. */ |
| 1071 | if (size < 0) { |
| 1072 | PyErr_SetString(PyExc_SystemError, |
| 1073 | "Negative size passed to PyUnicode_New"); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1077 | return PyErr_NoMemory(); |
| 1078 | |
| 1079 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1080 | * PyObject_New() so we are able to allocate space for the object and |
| 1081 | * it's data buffer. |
| 1082 | */ |
| 1083 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1084 | if (obj == NULL) |
| 1085 | return PyErr_NoMemory(); |
| 1086 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1087 | if (obj == NULL) |
| 1088 | return NULL; |
| 1089 | |
| 1090 | unicode = (PyCompactUnicodeObject *)obj; |
| 1091 | if (is_ascii) |
| 1092 | data = ((PyASCIIObject*)obj) + 1; |
| 1093 | else |
| 1094 | data = unicode + 1; |
| 1095 | _PyUnicode_LENGTH(unicode) = size; |
| 1096 | _PyUnicode_HASH(unicode) = -1; |
| 1097 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1098 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | _PyUnicode_STATE(unicode).compact = 1; |
| 1100 | _PyUnicode_STATE(unicode).ready = 1; |
| 1101 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1102 | if (is_ascii) { |
| 1103 | ((char*)data)[size] = 0; |
| 1104 | _PyUnicode_WSTR(unicode) = NULL; |
| 1105 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1106 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | ((char*)data)[size] = 0; |
| 1108 | _PyUnicode_WSTR(unicode) = NULL; |
| 1109 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1111 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1112 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 | else { |
| 1114 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1115 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1116 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1118 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | ((Py_UCS4*)data)[size] = 0; |
| 1120 | if (is_sharing) { |
| 1121 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1122 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1123 | } |
| 1124 | else { |
| 1125 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1126 | _PyUnicode_WSTR(unicode) = NULL; |
| 1127 | } |
| 1128 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1129 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1130 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1131 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1132 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1133 | return obj; |
| 1134 | } |
| 1135 | |
| 1136 | #if SIZEOF_WCHAR_T == 2 |
| 1137 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1138 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1139 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1140 | |
| 1141 | This function assumes that unicode can hold one more code point than wstr |
| 1142 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1143 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | 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] | 1145 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { |
| 1147 | const wchar_t *iter; |
| 1148 | Py_UCS4 *ucs4_out; |
| 1149 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1150 | assert(unicode != NULL); |
| 1151 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1153 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1154 | |
| 1155 | for (iter = begin; iter < end; ) { |
| 1156 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1157 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1158 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1159 | && (iter+1) < end |
| 1160 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1162 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | iter += 2; |
| 1164 | } |
| 1165 | else { |
| 1166 | *ucs4_out++ = *iter; |
| 1167 | iter++; |
| 1168 | } |
| 1169 | } |
| 1170 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1171 | _PyUnicode_GET_LENGTH(unicode))); |
| 1172 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | } |
| 1174 | #endif |
| 1175 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1176 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1177 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1178 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1179 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1180 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1181 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1182 | return -1; |
| 1183 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1187 | static int |
| 1188 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1189 | PyObject *from, Py_ssize_t from_start, |
| 1190 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1191 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1192 | unsigned int from_kind, to_kind; |
| 1193 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1195 | assert(0 <= how_many); |
| 1196 | assert(0 <= from_start); |
| 1197 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1198 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1199 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1200 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1201 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1202 | assert(PyUnicode_Check(to)); |
| 1203 | assert(PyUnicode_IS_READY(to)); |
| 1204 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1205 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1206 | if (how_many == 0) |
| 1207 | return 0; |
| 1208 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1210 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1212 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1213 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1214 | #ifdef Py_DEBUG |
| 1215 | if (!check_maxchar |
| 1216 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1217 | { |
| 1218 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1219 | Py_UCS4 ch; |
| 1220 | Py_ssize_t i; |
| 1221 | for (i=0; i < how_many; i++) { |
| 1222 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1223 | assert(ch <= to_maxchar); |
| 1224 | } |
| 1225 | } |
| 1226 | #endif |
| 1227 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1228 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1229 | if (check_maxchar |
| 1230 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1231 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1232 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1233 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1234 | Py_UCS4 max_char; |
| 1235 | max_char = ucs1lib_find_max_char(from_data, |
| 1236 | (Py_UCS1*)from_data + how_many); |
| 1237 | if (max_char >= 128) |
| 1238 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1239 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1240 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1241 | (char*)from_data + from_kind * from_start, |
| 1242 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1243 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1244 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1245 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1246 | { |
| 1247 | _PyUnicode_CONVERT_BYTES( |
| 1248 | Py_UCS1, Py_UCS2, |
| 1249 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1250 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1251 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1252 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1253 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1254 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1255 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1256 | { |
| 1257 | _PyUnicode_CONVERT_BYTES( |
| 1258 | Py_UCS1, Py_UCS4, |
| 1259 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1260 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1261 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1262 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1263 | } |
| 1264 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1265 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1266 | { |
| 1267 | _PyUnicode_CONVERT_BYTES( |
| 1268 | Py_UCS2, Py_UCS4, |
| 1269 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1270 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1271 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1272 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1273 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1274 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1275 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1276 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1277 | if (!check_maxchar) { |
| 1278 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1279 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1280 | { |
| 1281 | _PyUnicode_CONVERT_BYTES( |
| 1282 | Py_UCS2, Py_UCS1, |
| 1283 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1284 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1285 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1286 | ); |
| 1287 | } |
| 1288 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1289 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1290 | { |
| 1291 | _PyUnicode_CONVERT_BYTES( |
| 1292 | Py_UCS4, Py_UCS1, |
| 1293 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1294 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1295 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1296 | ); |
| 1297 | } |
| 1298 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1299 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1300 | { |
| 1301 | _PyUnicode_CONVERT_BYTES( |
| 1302 | Py_UCS4, Py_UCS2, |
| 1303 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1304 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1305 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1306 | ); |
| 1307 | } |
| 1308 | else { |
| 1309 | assert(0); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1313 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1314 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1315 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1316 | Py_ssize_t i; |
| 1317 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1318 | for (i=0; i < how_many; i++) { |
| 1319 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1320 | if (ch > to_maxchar) |
| 1321 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1322 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1323 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
| 1328 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1329 | void |
| 1330 | _PyUnicode_FastCopyCharacters( |
| 1331 | PyObject *to, Py_ssize_t to_start, |
| 1332 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1333 | { |
| 1334 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1335 | } |
| 1336 | |
| 1337 | Py_ssize_t |
| 1338 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1339 | PyObject *from, Py_ssize_t from_start, |
| 1340 | Py_ssize_t how_many) |
| 1341 | { |
| 1342 | int err; |
| 1343 | |
| 1344 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1345 | PyErr_BadInternalCall(); |
| 1346 | return -1; |
| 1347 | } |
| 1348 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1349 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1350 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1351 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1352 | return -1; |
| 1353 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1354 | if (from_start < 0) { |
| 1355 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | if (to_start < 0) { |
| 1359 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1360 | return -1; |
| 1361 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1362 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1363 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1364 | PyErr_Format(PyExc_SystemError, |
| 1365 | "Cannot write %zi characters at %zi " |
| 1366 | "in a string of %zi characters", |
| 1367 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1368 | return -1; |
| 1369 | } |
| 1370 | |
| 1371 | if (how_many == 0) |
| 1372 | return 0; |
| 1373 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1374 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1375 | return -1; |
| 1376 | |
| 1377 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1378 | if (err) { |
| 1379 | PyErr_Format(PyExc_SystemError, |
| 1380 | "Cannot copy %s characters " |
| 1381 | "into a string of %s characters", |
| 1382 | unicode_kind_name(from), |
| 1383 | unicode_kind_name(to)); |
| 1384 | return -1; |
| 1385 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1386 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1389 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1390 | correct string length can be computed before converting a string to UCS4. |
| 1391 | This function counts single surrogates as a character and not as a pair. |
| 1392 | |
| 1393 | Return 0 on success, or -1 on error. */ |
| 1394 | static int |
| 1395 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1396 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | { |
| 1398 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1399 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1401 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | *num_surrogates = 0; |
| 1403 | *maxchar = 0; |
| 1404 | |
| 1405 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1407 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1408 | && (iter+1) < end |
| 1409 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1410 | { |
| 1411 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1412 | ++(*num_surrogates); |
| 1413 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | } |
| 1415 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1417 | { |
| 1418 | ch = *iter; |
| 1419 | iter++; |
| 1420 | } |
| 1421 | if (ch > *maxchar) { |
| 1422 | *maxchar = ch; |
| 1423 | if (*maxchar > MAX_UNICODE) { |
| 1424 | PyErr_Format(PyExc_ValueError, |
| 1425 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1426 | ch); |
| 1427 | return -1; |
| 1428 | } |
| 1429 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1430 | } |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1434 | int |
| 1435 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1436 | { |
| 1437 | wchar_t *end; |
| 1438 | Py_UCS4 maxchar = 0; |
| 1439 | Py_ssize_t num_surrogates; |
| 1440 | #if SIZEOF_WCHAR_T == 2 |
| 1441 | Py_ssize_t length_wo_surrogates; |
| 1442 | #endif |
| 1443 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1444 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1445 | strings were created using _PyObject_New() and where no canonical |
| 1446 | representation (the str field) has been set yet aka strings |
| 1447 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1448 | assert(_PyUnicode_CHECK(unicode)); |
| 1449 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1450 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1451 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1453 | /* Actually, it should neither be interned nor be anything else: */ |
| 1454 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1456 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1457 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1458 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1460 | |
| 1461 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1462 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1463 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | PyErr_NoMemory(); |
| 1465 | return -1; |
| 1466 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1467 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | _PyUnicode_WSTR(unicode), end, |
| 1469 | PyUnicode_1BYTE_DATA(unicode)); |
| 1470 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1471 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1472 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1473 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1474 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1475 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1476 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | } |
| 1478 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1479 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1480 | _PyUnicode_UTF8(unicode) = NULL; |
| 1481 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1482 | } |
| 1483 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1484 | _PyUnicode_WSTR(unicode) = NULL; |
| 1485 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1486 | } |
| 1487 | /* In this case we might have to convert down from 4-byte native |
| 1488 | wchar_t to 2-byte unicode. */ |
| 1489 | else if (maxchar < 65536) { |
| 1490 | assert(num_surrogates == 0 && |
| 1491 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1492 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1493 | #if SIZEOF_WCHAR_T == 2 |
| 1494 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1495 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1496 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1497 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1498 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1499 | _PyUnicode_UTF8(unicode) = NULL; |
| 1500 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1501 | #else |
| 1502 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1503 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1504 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1505 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1506 | PyErr_NoMemory(); |
| 1507 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1508 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1509 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1510 | _PyUnicode_WSTR(unicode), end, |
| 1511 | PyUnicode_2BYTE_DATA(unicode)); |
| 1512 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1513 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1514 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1515 | _PyUnicode_UTF8(unicode) = NULL; |
| 1516 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1517 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1518 | _PyUnicode_WSTR(unicode) = NULL; |
| 1519 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1520 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1521 | } |
| 1522 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1523 | else { |
| 1524 | #if SIZEOF_WCHAR_T == 2 |
| 1525 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1526 | new normalized 4-byte version. */ |
| 1527 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1528 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1529 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1530 | PyErr_NoMemory(); |
| 1531 | return -1; |
| 1532 | } |
| 1533 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1534 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1535 | _PyUnicode_UTF8(unicode) = NULL; |
| 1536 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1537 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1538 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1539 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1540 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1541 | _PyUnicode_WSTR(unicode) = NULL; |
| 1542 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1543 | #else |
| 1544 | assert(num_surrogates == 0); |
| 1545 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1546 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1547 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1548 | _PyUnicode_UTF8(unicode) = NULL; |
| 1549 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1550 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1551 | #endif |
| 1552 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1553 | } |
| 1554 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1555 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1559 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1560 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1561 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1562 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1563 | case SSTATE_NOT_INTERNED: |
| 1564 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1565 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1566 | case SSTATE_INTERNED_MORTAL: |
| 1567 | /* revive dead object temporarily for DelItem */ |
| 1568 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1569 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 | Py_FatalError( |
| 1571 | "deletion of interned string failed"); |
| 1572 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | case SSTATE_INTERNED_IMMORTAL: |
| 1575 | Py_FatalError("Immortal interned string died."); |
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 | default: |
| 1578 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1581 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1582 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1583 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1584 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1585 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1586 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1587 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1588 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1591 | #ifdef Py_DEBUG |
| 1592 | static int |
| 1593 | unicode_is_singleton(PyObject *unicode) |
| 1594 | { |
| 1595 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1596 | if (unicode == unicode_empty) |
| 1597 | return 1; |
| 1598 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1599 | { |
| 1600 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1601 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1602 | return 1; |
| 1603 | } |
| 1604 | return 0; |
| 1605 | } |
| 1606 | #endif |
| 1607 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1608 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1609 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1610 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1611 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1612 | if (Py_REFCNT(unicode) != 1) |
| 1613 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1614 | if (_PyUnicode_HASH(unicode) != -1) |
| 1615 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1616 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1617 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1618 | if (!PyUnicode_CheckExact(unicode)) |
| 1619 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1620 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1621 | /* singleton refcount is greater than 1 */ |
| 1622 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1623 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1624 | return 1; |
| 1625 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1626 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1627 | static int |
| 1628 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1629 | { |
| 1630 | PyObject *unicode; |
| 1631 | Py_ssize_t old_length; |
| 1632 | |
| 1633 | assert(p_unicode != NULL); |
| 1634 | unicode = *p_unicode; |
| 1635 | |
| 1636 | assert(unicode != NULL); |
| 1637 | assert(PyUnicode_Check(unicode)); |
| 1638 | assert(0 <= length); |
| 1639 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1640 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1641 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1642 | else |
| 1643 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1644 | if (old_length == length) |
| 1645 | return 0; |
| 1646 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1647 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1648 | _Py_INCREF_UNICODE_EMPTY(); |
| 1649 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1650 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1651 | Py_DECREF(*p_unicode); |
| 1652 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1656 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1657 | PyObject *copy = resize_copy(unicode, length); |
| 1658 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1659 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1660 | Py_DECREF(*p_unicode); |
| 1661 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1662 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1665 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1666 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1667 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1668 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1669 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1670 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1671 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1672 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1675 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1676 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1677 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1678 | PyObject *unicode; |
| 1679 | if (p_unicode == NULL) { |
| 1680 | PyErr_BadInternalCall(); |
| 1681 | return -1; |
| 1682 | } |
| 1683 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1684 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1685 | { |
| 1686 | PyErr_BadInternalCall(); |
| 1687 | return -1; |
| 1688 | } |
| 1689 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1690 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1691 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1692 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1693 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1694 | WARNING: The function doesn't copy the terminating null character and |
| 1695 | doesn't check the maximum character (may write a latin1 character in an |
| 1696 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1697 | static void |
| 1698 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1699 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1700 | { |
| 1701 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1702 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1703 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1704 | |
| 1705 | switch (kind) { |
| 1706 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1708 | #ifdef Py_DEBUG |
| 1709 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1710 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1711 | (const Py_UCS1*)str, |
| 1712 | (const Py_UCS1*)str + len); |
| 1713 | assert(maxchar < 128); |
| 1714 | } |
| 1715 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1716 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1717 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1718 | } |
| 1719 | case PyUnicode_2BYTE_KIND: { |
| 1720 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1721 | Py_UCS2 *ucs2 = start; |
| 1722 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1723 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1724 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1725 | *ucs2 = (Py_UCS2)*str; |
| 1726 | |
| 1727 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1728 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1729 | } |
| 1730 | default: { |
| 1731 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1732 | Py_UCS4 *ucs4 = start; |
| 1733 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1734 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1735 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1736 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1737 | *ucs4 = (Py_UCS4)*str; |
| 1738 | |
| 1739 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | static PyObject* |
| 1746 | get_latin1_char(unsigned char ch) |
| 1747 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1748 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1750 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 | if (!unicode) |
| 1752 | return NULL; |
| 1753 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1754 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1755 | unicode_latin1[ch] = unicode; |
| 1756 | } |
| 1757 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1758 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1761 | PyObject * |
| 1762 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1764 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | Py_UCS4 maxchar = 0; |
| 1766 | Py_ssize_t num_surrogates; |
| 1767 | |
| 1768 | if (u == NULL) |
| 1769 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1770 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1771 | /* If the Unicode data is known at construction time, we can apply |
| 1772 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1773 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1774 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1775 | if (size == 0) |
| 1776 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1777 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 | /* Single character Unicode objects in the Latin-1 range are |
| 1779 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1780 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | return get_latin1_char((unsigned char)*u); |
| 1782 | |
| 1783 | /* If not empty and not single character, copy the Unicode data |
| 1784 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1785 | if (find_maxchar_surrogates(u, u + size, |
| 1786 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | return NULL; |
| 1788 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1789 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1790 | if (!unicode) |
| 1791 | return NULL; |
| 1792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1793 | switch (PyUnicode_KIND(unicode)) { |
| 1794 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1795 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1797 | break; |
| 1798 | case PyUnicode_2BYTE_KIND: |
| 1799 | #if Py_UNICODE_SIZE == 2 |
| 1800 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1801 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1802 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1803 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1804 | #endif |
| 1805 | break; |
| 1806 | case PyUnicode_4BYTE_KIND: |
| 1807 | #if SIZEOF_WCHAR_T == 2 |
| 1808 | /* This is the only case which has to process surrogates, thus |
| 1809 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1810 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1811 | #else |
| 1812 | assert(num_surrogates == 0); |
| 1813 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1814 | #endif |
| 1815 | break; |
| 1816 | default: |
| 1817 | assert(0 && "Impossible state"); |
| 1818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1820 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1823 | PyObject * |
| 1824 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1825 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1826 | if (size < 0) { |
| 1827 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1828 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1829 | return NULL; |
| 1830 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1831 | if (u != NULL) |
| 1832 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1833 | else |
| 1834 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1837 | PyObject * |
| 1838 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1839 | { |
| 1840 | size_t size = strlen(u); |
| 1841 | if (size > PY_SSIZE_T_MAX) { |
| 1842 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1843 | return NULL; |
| 1844 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1845 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1848 | PyObject * |
| 1849 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1850 | { |
| 1851 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1852 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1853 | strlen(id->string), |
| 1854 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1855 | if (!id->object) |
| 1856 | return NULL; |
| 1857 | PyUnicode_InternInPlace(&id->object); |
| 1858 | assert(!id->next); |
| 1859 | id->next = static_strings; |
| 1860 | static_strings = id; |
| 1861 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1862 | return id->object; |
| 1863 | } |
| 1864 | |
| 1865 | void |
| 1866 | _PyUnicode_ClearStaticStrings() |
| 1867 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1868 | _Py_Identifier *tmp, *s = static_strings; |
| 1869 | while (s) { |
| 1870 | Py_DECREF(s->object); |
| 1871 | s->object = NULL; |
| 1872 | tmp = s->next; |
| 1873 | s->next = NULL; |
| 1874 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1875 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1876 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1879 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1880 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1881 | PyObject* |
| 1882 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1883 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1884 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1885 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1886 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1887 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1888 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1889 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1890 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1891 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1892 | unicode = PyUnicode_New(size, 127); |
| 1893 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1894 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1895 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1896 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1897 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1900 | static Py_UCS4 |
| 1901 | kind_maxchar_limit(unsigned int kind) |
| 1902 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1903 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1904 | case PyUnicode_1BYTE_KIND: |
| 1905 | return 0x80; |
| 1906 | case PyUnicode_2BYTE_KIND: |
| 1907 | return 0x100; |
| 1908 | case PyUnicode_4BYTE_KIND: |
| 1909 | return 0x10000; |
| 1910 | default: |
| 1911 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1912 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1916 | Py_LOCAL_INLINE(Py_UCS4) |
| 1917 | align_maxchar(Py_UCS4 maxchar) |
| 1918 | { |
| 1919 | if (maxchar <= 127) |
| 1920 | return 127; |
| 1921 | else if (maxchar <= 255) |
| 1922 | return 255; |
| 1923 | else if (maxchar <= 65535) |
| 1924 | return 65535; |
| 1925 | else |
| 1926 | return MAX_UNICODE; |
| 1927 | } |
| 1928 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1929 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1930 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1931 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1933 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1934 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1935 | if (size == 0) |
| 1936 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1937 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1938 | if (size == 1) |
| 1939 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1940 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1941 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1942 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 | if (!res) |
| 1944 | return NULL; |
| 1945 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1946 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1947 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1950 | static PyObject* |
| 1951 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1952 | { |
| 1953 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1954 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1955 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1956 | if (size == 0) |
| 1957 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1958 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1959 | if (size == 1) { |
| 1960 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1961 | int kind; |
| 1962 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1963 | if (ch < 256) |
| 1964 | return get_latin1_char((unsigned char)ch); |
| 1965 | |
| 1966 | res = PyUnicode_New(1, ch); |
| 1967 | if (res == NULL) |
| 1968 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1969 | kind = PyUnicode_KIND(res); |
| 1970 | data = PyUnicode_DATA(res); |
| 1971 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1972 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1973 | return res; |
| 1974 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1975 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 | if (!res) |
| 1979 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1980 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1982 | else { |
| 1983 | _PyUnicode_CONVERT_BYTES( |
| 1984 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1985 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1986 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | return res; |
| 1988 | } |
| 1989 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1990 | static PyObject* |
| 1991 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | { |
| 1993 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1994 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1995 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1996 | if (size == 0) |
| 1997 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1998 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1999 | if (size == 1) { |
| 2000 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2001 | int kind; |
| 2002 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2003 | if (ch < 256) |
| 2004 | return get_latin1_char((unsigned char)ch); |
| 2005 | |
| 2006 | res = PyUnicode_New(1, ch); |
| 2007 | if (res == NULL) |
| 2008 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2009 | kind = PyUnicode_KIND(res); |
| 2010 | data = PyUnicode_DATA(res); |
| 2011 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2012 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 2013 | return res; |
| 2014 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2015 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2016 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2017 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2018 | if (!res) |
| 2019 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2020 | if (max_char < 256) |
| 2021 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2022 | PyUnicode_1BYTE_DATA(res)); |
| 2023 | else if (max_char < 0x10000) |
| 2024 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2025 | PyUnicode_2BYTE_DATA(res)); |
| 2026 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2027 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2028 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2029 | return res; |
| 2030 | } |
| 2031 | |
| 2032 | PyObject* |
| 2033 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2034 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2035 | if (size < 0) { |
| 2036 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2037 | return NULL; |
| 2038 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2039 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2040 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2041 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2042 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2043 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2044 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2045 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2046 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2047 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2048 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2052 | Py_UCS4 |
| 2053 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2054 | { |
| 2055 | enum PyUnicode_Kind kind; |
| 2056 | void *startptr, *endptr; |
| 2057 | |
| 2058 | assert(PyUnicode_IS_READY(unicode)); |
| 2059 | assert(0 <= start); |
| 2060 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2061 | assert(start <= end); |
| 2062 | |
| 2063 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2064 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2065 | |
| 2066 | if (start == end) |
| 2067 | return 127; |
| 2068 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2069 | if (PyUnicode_IS_ASCII(unicode)) |
| 2070 | return 127; |
| 2071 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2072 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2073 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2074 | endptr = (char *)startptr + end * kind; |
| 2075 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2076 | switch(kind) { |
| 2077 | case PyUnicode_1BYTE_KIND: |
| 2078 | return ucs1lib_find_max_char(startptr, endptr); |
| 2079 | case PyUnicode_2BYTE_KIND: |
| 2080 | return ucs2lib_find_max_char(startptr, endptr); |
| 2081 | case PyUnicode_4BYTE_KIND: |
| 2082 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2083 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2084 | assert(0); |
| 2085 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2089 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2090 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2091 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2092 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2093 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2094 | { |
| 2095 | PyObject *unicode, *copy; |
| 2096 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2097 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2098 | unsigned int kind; |
| 2099 | |
| 2100 | assert(p_unicode != NULL); |
| 2101 | unicode = *p_unicode; |
| 2102 | assert(PyUnicode_IS_READY(unicode)); |
| 2103 | if (PyUnicode_IS_ASCII(unicode)) |
| 2104 | return; |
| 2105 | |
| 2106 | len = PyUnicode_GET_LENGTH(unicode); |
| 2107 | kind = PyUnicode_KIND(unicode); |
| 2108 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2109 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2110 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2111 | if (max_char >= 128) |
| 2112 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2113 | } |
| 2114 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2115 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2116 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2117 | if (max_char >= 256) |
| 2118 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2119 | } |
| 2120 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2121 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2122 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2123 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2124 | if (max_char >= 0x10000) |
| 2125 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2126 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2127 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2128 | if (copy != NULL) |
| 2129 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2130 | Py_DECREF(unicode); |
| 2131 | *p_unicode = copy; |
| 2132 | } |
| 2133 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2134 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2135 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2136 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2137 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2138 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2139 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2140 | if (!PyUnicode_Check(unicode)) { |
| 2141 | PyErr_BadInternalCall(); |
| 2142 | return NULL; |
| 2143 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2144 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2145 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2146 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2147 | length = PyUnicode_GET_LENGTH(unicode); |
| 2148 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2149 | if (!copy) |
| 2150 | return NULL; |
| 2151 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2152 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2153 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2154 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2155 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2156 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2157 | } |
| 2158 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2159 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2160 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2161 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | |
| 2163 | void* |
| 2164 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2165 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2166 | Py_ssize_t len; |
| 2167 | void *result; |
| 2168 | unsigned int skind; |
| 2169 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2170 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2171 | return NULL; |
| 2172 | |
| 2173 | len = PyUnicode_GET_LENGTH(s); |
| 2174 | skind = PyUnicode_KIND(s); |
| 2175 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2176 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 | return NULL; |
| 2178 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2179 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2180 | case PyUnicode_2BYTE_KIND: |
| 2181 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2182 | if (!result) |
| 2183 | return PyErr_NoMemory(); |
| 2184 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2185 | _PyUnicode_CONVERT_BYTES( |
| 2186 | Py_UCS1, Py_UCS2, |
| 2187 | PyUnicode_1BYTE_DATA(s), |
| 2188 | PyUnicode_1BYTE_DATA(s) + len, |
| 2189 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2190 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2191 | case PyUnicode_4BYTE_KIND: |
| 2192 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2193 | if (!result) |
| 2194 | return PyErr_NoMemory(); |
| 2195 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2196 | _PyUnicode_CONVERT_BYTES( |
| 2197 | Py_UCS2, Py_UCS4, |
| 2198 | PyUnicode_2BYTE_DATA(s), |
| 2199 | PyUnicode_2BYTE_DATA(s) + len, |
| 2200 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2202 | else { |
| 2203 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2204 | _PyUnicode_CONVERT_BYTES( |
| 2205 | Py_UCS1, Py_UCS4, |
| 2206 | PyUnicode_1BYTE_DATA(s), |
| 2207 | PyUnicode_1BYTE_DATA(s) + len, |
| 2208 | result); |
| 2209 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2210 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2211 | default: |
| 2212 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2213 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2214 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2215 | return NULL; |
| 2216 | } |
| 2217 | |
| 2218 | static Py_UCS4* |
| 2219 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2220 | int copy_null) |
| 2221 | { |
| 2222 | int kind; |
| 2223 | void *data; |
| 2224 | Py_ssize_t len, targetlen; |
| 2225 | if (PyUnicode_READY(string) == -1) |
| 2226 | return NULL; |
| 2227 | kind = PyUnicode_KIND(string); |
| 2228 | data = PyUnicode_DATA(string); |
| 2229 | len = PyUnicode_GET_LENGTH(string); |
| 2230 | targetlen = len; |
| 2231 | if (copy_null) |
| 2232 | targetlen++; |
| 2233 | if (!target) { |
| 2234 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2235 | PyErr_NoMemory(); |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2239 | if (!target) { |
| 2240 | PyErr_NoMemory(); |
| 2241 | return NULL; |
| 2242 | } |
| 2243 | } |
| 2244 | else { |
| 2245 | if (targetsize < targetlen) { |
| 2246 | PyErr_Format(PyExc_SystemError, |
| 2247 | "string is longer than the buffer"); |
| 2248 | if (copy_null && 0 < targetsize) |
| 2249 | target[0] = 0; |
| 2250 | return NULL; |
| 2251 | } |
| 2252 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2253 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2254 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2255 | _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] | 2256 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2257 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2258 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2259 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2260 | } |
| 2261 | else { |
| 2262 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2265 | if (copy_null) |
| 2266 | target[len] = 0; |
| 2267 | return target; |
| 2268 | } |
| 2269 | |
| 2270 | Py_UCS4* |
| 2271 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2272 | int copy_null) |
| 2273 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2274 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2275 | PyErr_BadInternalCall(); |
| 2276 | return NULL; |
| 2277 | } |
| 2278 | return as_ucs4(string, target, targetsize, copy_null); |
| 2279 | } |
| 2280 | |
| 2281 | Py_UCS4* |
| 2282 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2283 | { |
| 2284 | return as_ucs4(string, NULL, 0, 1); |
| 2285 | } |
| 2286 | |
| 2287 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2288 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2289 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2290 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2292 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2294 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2295 | PyErr_BadInternalCall(); |
| 2296 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2299 | if (size == -1) { |
| 2300 | size = wcslen(w); |
| 2301 | } |
| 2302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2303 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2307 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2308 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2309 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2310 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2311 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2312 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2313 | if (longflag) |
| 2314 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2315 | else if (longlongflag) { |
| 2316 | /* longlongflag should only ever be nonzero on machines with |
| 2317 | HAVE_LONG_LONG defined */ |
| 2318 | #ifdef HAVE_LONG_LONG |
| 2319 | char *f = PY_FORMAT_LONG_LONG; |
| 2320 | while (*f) |
| 2321 | *fmt++ = *f++; |
| 2322 | #else |
| 2323 | /* we shouldn't ever get here */ |
| 2324 | assert(0); |
| 2325 | *fmt++ = 'l'; |
| 2326 | #endif |
| 2327 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2328 | else if (size_tflag) { |
| 2329 | char *f = PY_FORMAT_SIZE_T; |
| 2330 | while (*f) |
| 2331 | *fmt++ = *f++; |
| 2332 | } |
| 2333 | *fmt++ = c; |
| 2334 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2337 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2338 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2339 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2340 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2341 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2342 | static int |
| 2343 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2344 | Py_ssize_t width, Py_ssize_t precision) |
| 2345 | { |
| 2346 | Py_ssize_t length, fill, arglen; |
| 2347 | Py_UCS4 maxchar; |
| 2348 | |
| 2349 | if (PyUnicode_READY(str) == -1) |
| 2350 | return -1; |
| 2351 | |
| 2352 | length = PyUnicode_GET_LENGTH(str); |
| 2353 | if ((precision == -1 || precision >= length) |
| 2354 | && width <= length) |
| 2355 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2356 | |
| 2357 | if (precision != -1) |
| 2358 | length = Py_MIN(precision, length); |
| 2359 | |
| 2360 | arglen = Py_MAX(length, width); |
| 2361 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2362 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2363 | else |
| 2364 | maxchar = writer->maxchar; |
| 2365 | |
| 2366 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2367 | return -1; |
| 2368 | |
| 2369 | if (width > length) { |
| 2370 | fill = width - length; |
| 2371 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2372 | return -1; |
| 2373 | writer->pos += fill; |
| 2374 | } |
| 2375 | |
| 2376 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2377 | str, 0, length); |
| 2378 | writer->pos += length; |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
| 2382 | static int |
| 2383 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2384 | Py_ssize_t width, Py_ssize_t precision) |
| 2385 | { |
| 2386 | /* UTF-8 */ |
| 2387 | Py_ssize_t length; |
| 2388 | PyObject *unicode; |
| 2389 | int res; |
| 2390 | |
| 2391 | length = strlen(str); |
| 2392 | if (precision != -1) |
| 2393 | length = Py_MIN(length, precision); |
| 2394 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2395 | if (unicode == NULL) |
| 2396 | return -1; |
| 2397 | |
| 2398 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2399 | Py_DECREF(unicode); |
| 2400 | return res; |
| 2401 | } |
| 2402 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2403 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2404 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2405 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2406 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2407 | const char *p; |
| 2408 | Py_ssize_t len; |
| 2409 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2410 | Py_ssize_t width; |
| 2411 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2412 | int longflag; |
| 2413 | int longlongflag; |
| 2414 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2415 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2416 | |
| 2417 | p = f; |
| 2418 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2419 | zeropad = 0; |
| 2420 | if (*f == '0') { |
| 2421 | zeropad = 1; |
| 2422 | f++; |
| 2423 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2424 | |
| 2425 | /* 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] | 2426 | width = -1; |
| 2427 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2428 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2429 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2430 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2431 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2432 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2433 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2434 | return NULL; |
| 2435 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2436 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2437 | f++; |
| 2438 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2439 | } |
| 2440 | precision = -1; |
| 2441 | if (*f == '.') { |
| 2442 | f++; |
| 2443 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2444 | precision = (*f - '0'); |
| 2445 | f++; |
| 2446 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2447 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2448 | PyErr_SetString(PyExc_ValueError, |
| 2449 | "precision too big"); |
| 2450 | return NULL; |
| 2451 | } |
| 2452 | precision = (precision * 10) + (*f - '0'); |
| 2453 | f++; |
| 2454 | } |
| 2455 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2456 | if (*f == '%') { |
| 2457 | /* "%.3%s" => f points to "3" */ |
| 2458 | f--; |
| 2459 | } |
| 2460 | } |
| 2461 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2462 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2463 | f--; |
| 2464 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2465 | |
| 2466 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2467 | longflag = 0; |
| 2468 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2469 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2470 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2471 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2472 | longflag = 1; |
| 2473 | ++f; |
| 2474 | } |
| 2475 | #ifdef HAVE_LONG_LONG |
| 2476 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2477 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2478 | longlongflag = 1; |
| 2479 | f += 2; |
| 2480 | } |
| 2481 | #endif |
| 2482 | } |
| 2483 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2484 | 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] | 2485 | size_tflag = 1; |
| 2486 | ++f; |
| 2487 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2488 | |
| 2489 | if (f[1] == '\0') |
| 2490 | writer->overallocate = 0; |
| 2491 | |
| 2492 | switch (*f) { |
| 2493 | case 'c': |
| 2494 | { |
| 2495 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2496 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2497 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2498 | "character argument not in range(0x110000)"); |
| 2499 | return NULL; |
| 2500 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2501 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2502 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2503 | break; |
| 2504 | } |
| 2505 | |
| 2506 | case 'i': |
| 2507 | case 'd': |
| 2508 | case 'u': |
| 2509 | case 'x': |
| 2510 | { |
| 2511 | /* used by sprintf */ |
| 2512 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2513 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2514 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2515 | |
| 2516 | if (*f == 'u') { |
| 2517 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2518 | |
| 2519 | if (longflag) |
| 2520 | len = sprintf(buffer, fmt, |
| 2521 | va_arg(*vargs, unsigned long)); |
| 2522 | #ifdef HAVE_LONG_LONG |
| 2523 | else if (longlongflag) |
| 2524 | len = sprintf(buffer, fmt, |
| 2525 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2526 | #endif |
| 2527 | else if (size_tflag) |
| 2528 | len = sprintf(buffer, fmt, |
| 2529 | va_arg(*vargs, size_t)); |
| 2530 | else |
| 2531 | len = sprintf(buffer, fmt, |
| 2532 | va_arg(*vargs, unsigned int)); |
| 2533 | } |
| 2534 | else if (*f == 'x') { |
| 2535 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2536 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2537 | } |
| 2538 | else { |
| 2539 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2540 | |
| 2541 | if (longflag) |
| 2542 | len = sprintf(buffer, fmt, |
| 2543 | va_arg(*vargs, long)); |
| 2544 | #ifdef HAVE_LONG_LONG |
| 2545 | else if (longlongflag) |
| 2546 | len = sprintf(buffer, fmt, |
| 2547 | va_arg(*vargs, PY_LONG_LONG)); |
| 2548 | #endif |
| 2549 | else if (size_tflag) |
| 2550 | len = sprintf(buffer, fmt, |
| 2551 | va_arg(*vargs, Py_ssize_t)); |
| 2552 | else |
| 2553 | len = sprintf(buffer, fmt, |
| 2554 | va_arg(*vargs, int)); |
| 2555 | } |
| 2556 | assert(len >= 0); |
| 2557 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2558 | if (precision < len) |
| 2559 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2560 | |
| 2561 | arglen = Py_MAX(precision, width); |
| 2562 | assert(ucs1lib_find_max_char((Py_UCS1*)buffer, (Py_UCS1*)buffer + len) <= 127); |
| 2563 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2564 | return NULL; |
| 2565 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2566 | if (width > precision) { |
| 2567 | Py_UCS4 fillchar; |
| 2568 | fill = width - precision; |
| 2569 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2570 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2571 | return NULL; |
| 2572 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2573 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2574 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2575 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2576 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2577 | return NULL; |
| 2578 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2579 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2580 | |
| 2581 | unicode_write_cstr(writer->buffer, writer->pos, buffer, len); |
| 2582 | writer->pos += len; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2583 | break; |
| 2584 | } |
| 2585 | |
| 2586 | case 'p': |
| 2587 | { |
| 2588 | char number[MAX_LONG_LONG_CHARS]; |
| 2589 | |
| 2590 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2591 | assert(len >= 0); |
| 2592 | |
| 2593 | /* %p is ill-defined: ensure leading 0x. */ |
| 2594 | if (number[1] == 'X') |
| 2595 | number[1] = 'x'; |
| 2596 | else if (number[1] != 'x') { |
| 2597 | memmove(number + 2, number, |
| 2598 | strlen(number) + 1); |
| 2599 | number[0] = '0'; |
| 2600 | number[1] = 'x'; |
| 2601 | len += 2; |
| 2602 | } |
| 2603 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2604 | assert(ucs1lib_find_max_char((Py_UCS1*)number, (Py_UCS1*)number + len) <= 127); |
| 2605 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2606 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2607 | unicode_write_cstr(writer->buffer, writer->pos, number, len); |
| 2608 | writer->pos += len; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2609 | break; |
| 2610 | } |
| 2611 | |
| 2612 | case 's': |
| 2613 | { |
| 2614 | /* UTF-8 */ |
| 2615 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2616 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2617 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2618 | break; |
| 2619 | } |
| 2620 | |
| 2621 | case 'U': |
| 2622 | { |
| 2623 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2624 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2625 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2626 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2627 | return NULL; |
| 2628 | break; |
| 2629 | } |
| 2630 | |
| 2631 | case 'V': |
| 2632 | { |
| 2633 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2634 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2635 | if (obj) { |
| 2636 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2637 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2638 | return NULL; |
| 2639 | } |
| 2640 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2641 | assert(str != NULL); |
| 2642 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2643 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2644 | } |
| 2645 | break; |
| 2646 | } |
| 2647 | |
| 2648 | case 'S': |
| 2649 | { |
| 2650 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2651 | PyObject *str; |
| 2652 | assert(obj); |
| 2653 | str = PyObject_Str(obj); |
| 2654 | if (!str) |
| 2655 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2656 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2657 | Py_DECREF(str); |
| 2658 | return NULL; |
| 2659 | } |
| 2660 | Py_DECREF(str); |
| 2661 | break; |
| 2662 | } |
| 2663 | |
| 2664 | case 'R': |
| 2665 | { |
| 2666 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2667 | PyObject *repr; |
| 2668 | assert(obj); |
| 2669 | repr = PyObject_Repr(obj); |
| 2670 | if (!repr) |
| 2671 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2672 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2673 | Py_DECREF(repr); |
| 2674 | return NULL; |
| 2675 | } |
| 2676 | Py_DECREF(repr); |
| 2677 | break; |
| 2678 | } |
| 2679 | |
| 2680 | case 'A': |
| 2681 | { |
| 2682 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2683 | PyObject *ascii; |
| 2684 | assert(obj); |
| 2685 | ascii = PyObject_ASCII(obj); |
| 2686 | if (!ascii) |
| 2687 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2688 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2689 | Py_DECREF(ascii); |
| 2690 | return NULL; |
| 2691 | } |
| 2692 | Py_DECREF(ascii); |
| 2693 | break; |
| 2694 | } |
| 2695 | |
| 2696 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2697 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2698 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2699 | break; |
| 2700 | |
| 2701 | default: |
| 2702 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2703 | of the format string to the output string. (we cannot just |
| 2704 | skip the code, since there's no way to know what's in the |
| 2705 | argument list) */ |
| 2706 | len = strlen(p); |
| 2707 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2708 | return NULL; |
| 2709 | f = p+len; |
| 2710 | return f; |
| 2711 | } |
| 2712 | |
| 2713 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2714 | return f; |
| 2715 | } |
| 2716 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2717 | PyObject * |
| 2718 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2719 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2720 | va_list vargs2; |
| 2721 | const char *f; |
| 2722 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2723 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2724 | _PyUnicodeWriter_Init(&writer); |
| 2725 | writer.min_length = strlen(format) + 100; |
| 2726 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2727 | |
| 2728 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2729 | Copy it to be able to pass a reference to a subfunction. */ |
| 2730 | Py_VA_COPY(vargs2, vargs); |
| 2731 | |
| 2732 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2733 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2734 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2735 | if (f == NULL) |
| 2736 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2738 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2739 | const char *p; |
| 2740 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2741 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2742 | p = f; |
| 2743 | do |
| 2744 | { |
| 2745 | if ((unsigned char)*p > 127) { |
| 2746 | PyErr_Format(PyExc_ValueError, |
| 2747 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2748 | "string, got a non-ASCII byte: 0x%02x", |
| 2749 | (unsigned char)*p); |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | p++; |
| 2753 | } |
| 2754 | while (*p != '\0' && *p != '%'); |
| 2755 | len = p - f; |
| 2756 | |
| 2757 | if (*p == '\0') |
| 2758 | writer.overallocate = 0; |
| 2759 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2760 | goto fail; |
| 2761 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2762 | writer.pos += len; |
| 2763 | |
| 2764 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2765 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2766 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2767 | return _PyUnicodeWriter_Finish(&writer); |
| 2768 | |
| 2769 | fail: |
| 2770 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2771 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2774 | PyObject * |
| 2775 | PyUnicode_FromFormat(const char *format, ...) |
| 2776 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 | PyObject* ret; |
| 2778 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2779 | |
| 2780 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2781 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2782 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2784 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2785 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2786 | va_end(vargs); |
| 2787 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | #ifdef HAVE_WCHAR_H |
| 2791 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2792 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2793 | convert a Unicode object to a wide character string. |
| 2794 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2795 | - 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] | 2796 | character) required to convert the unicode object. Ignore size argument. |
| 2797 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2798 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2799 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2800 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2801 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2802 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2803 | wchar_t *w, |
| 2804 | Py_ssize_t size) |
| 2805 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2806 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2807 | const wchar_t *wstr; |
| 2808 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2809 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 | if (wstr == NULL) |
| 2811 | return -1; |
| 2812 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2813 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2814 | if (size > res) |
| 2815 | size = res + 1; |
| 2816 | else |
| 2817 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2818 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2819 | return res; |
| 2820 | } |
| 2821 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2826 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2827 | wchar_t *w, |
| 2828 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2829 | { |
| 2830 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2831 | PyErr_BadInternalCall(); |
| 2832 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2834 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2837 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2838 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2839 | Py_ssize_t *size) |
| 2840 | { |
| 2841 | wchar_t* buffer; |
| 2842 | Py_ssize_t buflen; |
| 2843 | |
| 2844 | if (unicode == NULL) { |
| 2845 | PyErr_BadInternalCall(); |
| 2846 | return NULL; |
| 2847 | } |
| 2848 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2849 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2850 | if (buflen == -1) |
| 2851 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2852 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2853 | PyErr_NoMemory(); |
| 2854 | return NULL; |
| 2855 | } |
| 2856 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2857 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2858 | if (buffer == NULL) { |
| 2859 | PyErr_NoMemory(); |
| 2860 | return NULL; |
| 2861 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2862 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2863 | if (buflen == -1) { |
| 2864 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2865 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2866 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2867 | if (size != NULL) |
| 2868 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2869 | return buffer; |
| 2870 | } |
| 2871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2872 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2874 | PyObject * |
| 2875 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2876 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2877 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2878 | void *data; |
| 2879 | int kind; |
| 2880 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2881 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2882 | PyErr_SetString(PyExc_ValueError, |
| 2883 | "chr() arg not in range(0x110000)"); |
| 2884 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2885 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2886 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2887 | if ((Py_UCS4)ordinal < 256) |
| 2888 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2889 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2890 | v = PyUnicode_New(1, ordinal); |
| 2891 | if (v == NULL) |
| 2892 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2893 | kind = PyUnicode_KIND(v); |
| 2894 | data = PyUnicode_DATA(v); |
| 2895 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2896 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2897 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2900 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2901 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2903 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2905 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2906 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2907 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2908 | Py_INCREF(obj); |
| 2909 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2910 | } |
| 2911 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2912 | /* For a Unicode subtype that's not a Unicode object, |
| 2913 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2914 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2915 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2916 | PyErr_Format(PyExc_TypeError, |
| 2917 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2918 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2919 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2922 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2923 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2924 | const char *encoding, |
| 2925 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2926 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2927 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2928 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | PyErr_BadInternalCall(); |
| 2932 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2933 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2935 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2936 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2937 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2938 | _Py_RETURN_UNICODE_EMPTY(); |
| 2939 | v = PyUnicode_Decode( |
| 2940 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2941 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2942 | return v; |
| 2943 | } |
| 2944 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2945 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2946 | PyErr_SetString(PyExc_TypeError, |
| 2947 | "decoding str is not supported"); |
| 2948 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2949 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2950 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2951 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2952 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2953 | PyErr_Format(PyExc_TypeError, |
| 2954 | "coercing to str: need bytes, bytearray " |
| 2955 | "or buffer-like object, %.80s found", |
| 2956 | Py_TYPE(obj)->tp_name); |
| 2957 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2958 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2959 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2960 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2961 | PyBuffer_Release(&buffer); |
| 2962 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2964 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2965 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2966 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2967 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2970 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2971 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2972 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2973 | int |
| 2974 | _Py_normalize_encoding(const char *encoding, |
| 2975 | char *lower, |
| 2976 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2977 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2978 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2979 | char *l; |
| 2980 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2981 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2982 | if (encoding == NULL) { |
| 2983 | strcpy(lower, "utf-8"); |
| 2984 | return 1; |
| 2985 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2986 | e = encoding; |
| 2987 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2988 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2989 | while (*e) { |
| 2990 | if (l == l_end) |
| 2991 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2992 | if (Py_ISUPPER(*e)) { |
| 2993 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2994 | } |
| 2995 | else if (*e == '_') { |
| 2996 | *l++ = '-'; |
| 2997 | e++; |
| 2998 | } |
| 2999 | else { |
| 3000 | *l++ = *e++; |
| 3001 | } |
| 3002 | } |
| 3003 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3004 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3007 | PyObject * |
| 3008 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3009 | Py_ssize_t size, |
| 3010 | const char *encoding, |
| 3011 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3012 | { |
| 3013 | PyObject *buffer = NULL, *unicode; |
| 3014 | Py_buffer info; |
| 3015 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3016 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3017 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3018 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3019 | if ((strcmp(lower, "utf-8") == 0) || |
| 3020 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3021 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3022 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3023 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame^] | 3024 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3025 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3026 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3027 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3028 | else if (strcmp(lower, "mbcs") == 0) |
| 3029 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3030 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3031 | else if (strcmp(lower, "ascii") == 0) |
| 3032 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3033 | else if (strcmp(lower, "utf-16") == 0) |
| 3034 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3035 | else if (strcmp(lower, "utf-32") == 0) |
| 3036 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3037 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | |
| 3039 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3040 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3041 | 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] | 3042 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3043 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | if (buffer == NULL) |
| 3045 | goto onError; |
| 3046 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3047 | if (unicode == NULL) |
| 3048 | goto onError; |
| 3049 | if (!PyUnicode_Check(unicode)) { |
| 3050 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3051 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3052 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | Py_DECREF(unicode); |
| 3054 | goto onError; |
| 3055 | } |
| 3056 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3057 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3058 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3059 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | Py_XDECREF(buffer); |
| 3061 | return NULL; |
| 3062 | } |
| 3063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3064 | PyObject * |
| 3065 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3066 | const char *encoding, |
| 3067 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3068 | { |
| 3069 | PyObject *v; |
| 3070 | |
| 3071 | if (!PyUnicode_Check(unicode)) { |
| 3072 | PyErr_BadArgument(); |
| 3073 | goto onError; |
| 3074 | } |
| 3075 | |
| 3076 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3077 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3078 | |
| 3079 | /* Decode via the codec registry */ |
| 3080 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3081 | if (v == NULL) |
| 3082 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3083 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3084 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3085 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3086 | return NULL; |
| 3087 | } |
| 3088 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3089 | PyObject * |
| 3090 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3091 | const char *encoding, |
| 3092 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3093 | { |
| 3094 | PyObject *v; |
| 3095 | |
| 3096 | if (!PyUnicode_Check(unicode)) { |
| 3097 | PyErr_BadArgument(); |
| 3098 | goto onError; |
| 3099 | } |
| 3100 | |
| 3101 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3102 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3103 | |
| 3104 | /* Decode via the codec registry */ |
| 3105 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3106 | if (v == NULL) |
| 3107 | goto onError; |
| 3108 | if (!PyUnicode_Check(v)) { |
| 3109 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3110 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3111 | Py_TYPE(v)->tp_name); |
| 3112 | Py_DECREF(v); |
| 3113 | goto onError; |
| 3114 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3115 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3116 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3117 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3118 | return NULL; |
| 3119 | } |
| 3120 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3121 | PyObject * |
| 3122 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3123 | Py_ssize_t size, |
| 3124 | const char *encoding, |
| 3125 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3126 | { |
| 3127 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3128 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | unicode = PyUnicode_FromUnicode(s, size); |
| 3130 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3131 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3133 | Py_DECREF(unicode); |
| 3134 | return v; |
| 3135 | } |
| 3136 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3137 | PyObject * |
| 3138 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3139 | const char *encoding, |
| 3140 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3141 | { |
| 3142 | PyObject *v; |
| 3143 | |
| 3144 | if (!PyUnicode_Check(unicode)) { |
| 3145 | PyErr_BadArgument(); |
| 3146 | goto onError; |
| 3147 | } |
| 3148 | |
| 3149 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3150 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3151 | |
| 3152 | /* Encode via the codec registry */ |
| 3153 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3154 | if (v == NULL) |
| 3155 | goto onError; |
| 3156 | return v; |
| 3157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3158 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3159 | return NULL; |
| 3160 | } |
| 3161 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3162 | static size_t |
| 3163 | wcstombs_errorpos(const wchar_t *wstr) |
| 3164 | { |
| 3165 | size_t len; |
| 3166 | #if SIZEOF_WCHAR_T == 2 |
| 3167 | wchar_t buf[3]; |
| 3168 | #else |
| 3169 | wchar_t buf[2]; |
| 3170 | #endif |
| 3171 | char outbuf[MB_LEN_MAX]; |
| 3172 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3173 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3174 | #if SIZEOF_WCHAR_T == 2 |
| 3175 | buf[2] = 0; |
| 3176 | #else |
| 3177 | buf[1] = 0; |
| 3178 | #endif |
| 3179 | start = wstr; |
| 3180 | while (*wstr != L'\0') |
| 3181 | { |
| 3182 | previous = wstr; |
| 3183 | #if SIZEOF_WCHAR_T == 2 |
| 3184 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3185 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3186 | { |
| 3187 | buf[0] = wstr[0]; |
| 3188 | buf[1] = wstr[1]; |
| 3189 | wstr += 2; |
| 3190 | } |
| 3191 | else { |
| 3192 | buf[0] = *wstr; |
| 3193 | buf[1] = 0; |
| 3194 | wstr++; |
| 3195 | } |
| 3196 | #else |
| 3197 | buf[0] = *wstr; |
| 3198 | wstr++; |
| 3199 | #endif |
| 3200 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3201 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3202 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3206 | return 0; |
| 3207 | } |
| 3208 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3209 | static int |
| 3210 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3211 | { |
| 3212 | if (errors == NULL) { |
| 3213 | *surrogateescape = 0; |
| 3214 | return 0; |
| 3215 | } |
| 3216 | |
| 3217 | if (strcmp(errors, "strict") == 0) { |
| 3218 | *surrogateescape = 0; |
| 3219 | return 0; |
| 3220 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3221 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3222 | *surrogateescape = 1; |
| 3223 | return 0; |
| 3224 | } |
| 3225 | PyErr_Format(PyExc_ValueError, |
| 3226 | "only 'strict' and 'surrogateescape' error handlers " |
| 3227 | "are supported, not '%s'", |
| 3228 | errors); |
| 3229 | return -1; |
| 3230 | } |
| 3231 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3232 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3233 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3234 | { |
| 3235 | Py_ssize_t wlen, wlen2; |
| 3236 | wchar_t *wstr; |
| 3237 | PyObject *bytes = NULL; |
| 3238 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3239 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3240 | PyObject *exc; |
| 3241 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3242 | int surrogateescape; |
| 3243 | |
| 3244 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3245 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3246 | |
| 3247 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3248 | if (wstr == NULL) |
| 3249 | return NULL; |
| 3250 | |
| 3251 | wlen2 = wcslen(wstr); |
| 3252 | if (wlen2 != wlen) { |
| 3253 | PyMem_Free(wstr); |
| 3254 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3255 | return NULL; |
| 3256 | } |
| 3257 | |
| 3258 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3259 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3260 | char *str; |
| 3261 | |
| 3262 | str = _Py_wchar2char(wstr, &error_pos); |
| 3263 | if (str == NULL) { |
| 3264 | if (error_pos == (size_t)-1) { |
| 3265 | PyErr_NoMemory(); |
| 3266 | PyMem_Free(wstr); |
| 3267 | return NULL; |
| 3268 | } |
| 3269 | else { |
| 3270 | goto encode_error; |
| 3271 | } |
| 3272 | } |
| 3273 | PyMem_Free(wstr); |
| 3274 | |
| 3275 | bytes = PyBytes_FromString(str); |
| 3276 | PyMem_Free(str); |
| 3277 | } |
| 3278 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3279 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3280 | size_t len, len2; |
| 3281 | |
| 3282 | len = wcstombs(NULL, wstr, 0); |
| 3283 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3284 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3285 | goto encode_error; |
| 3286 | } |
| 3287 | |
| 3288 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3289 | if (bytes == NULL) { |
| 3290 | PyMem_Free(wstr); |
| 3291 | return NULL; |
| 3292 | } |
| 3293 | |
| 3294 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3295 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3296 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3297 | goto encode_error; |
| 3298 | } |
| 3299 | PyMem_Free(wstr); |
| 3300 | } |
| 3301 | return bytes; |
| 3302 | |
| 3303 | encode_error: |
| 3304 | errmsg = strerror(errno); |
| 3305 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3306 | |
| 3307 | if (error_pos == (size_t)-1) |
| 3308 | error_pos = wcstombs_errorpos(wstr); |
| 3309 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3310 | PyMem_Free(wstr); |
| 3311 | Py_XDECREF(bytes); |
| 3312 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3313 | if (errmsg != NULL) { |
| 3314 | size_t errlen; |
| 3315 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3316 | if (wstr != NULL) { |
| 3317 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3318 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3319 | } else |
| 3320 | errmsg = NULL; |
| 3321 | } |
| 3322 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3323 | reason = PyUnicode_FromString( |
| 3324 | "wcstombs() encountered an unencodable " |
| 3325 | "wide character"); |
| 3326 | if (reason == NULL) |
| 3327 | return NULL; |
| 3328 | |
| 3329 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3330 | "locale", unicode, |
| 3331 | (Py_ssize_t)error_pos, |
| 3332 | (Py_ssize_t)(error_pos+1), |
| 3333 | reason); |
| 3334 | Py_DECREF(reason); |
| 3335 | if (exc != NULL) { |
| 3336 | PyCodec_StrictErrors(exc); |
| 3337 | Py_XDECREF(exc); |
| 3338 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3339 | return NULL; |
| 3340 | } |
| 3341 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3342 | PyObject * |
| 3343 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3344 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3345 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3346 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3347 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3348 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3349 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3350 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3351 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3352 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3353 | the Python codec requires to encode at least its own filename. Use the C |
| 3354 | version of the locale codec until the codec registry is initialized and |
| 3355 | the Python codec is loaded. |
| 3356 | |
| 3357 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3358 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3359 | subinterpreters. */ |
| 3360 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3361 | return PyUnicode_AsEncodedString(unicode, |
| 3362 | Py_FileSystemDefaultEncoding, |
| 3363 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3364 | } |
| 3365 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3366 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3367 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3368 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3371 | PyObject * |
| 3372 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3373 | const char *encoding, |
| 3374 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3375 | { |
| 3376 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3377 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | if (!PyUnicode_Check(unicode)) { |
| 3380 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3381 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3383 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3384 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3385 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3386 | if ((strcmp(lower, "utf-8") == 0) || |
| 3387 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3388 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3389 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3390 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3391 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3392 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3393 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3394 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3395 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame^] | 3396 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3397 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3398 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3399 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3400 | else if (strcmp(lower, "mbcs") == 0) |
| 3401 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3402 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3403 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3404 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3406 | |
| 3407 | /* Encode via the codec registry */ |
| 3408 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3409 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3410 | return NULL; |
| 3411 | |
| 3412 | /* The normal path */ |
| 3413 | if (PyBytes_Check(v)) |
| 3414 | return v; |
| 3415 | |
| 3416 | /* 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] | 3417 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3418 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3419 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3420 | |
| 3421 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3422 | "encoder %s returned bytearray instead of bytes", |
| 3423 | encoding); |
| 3424 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3425 | Py_DECREF(v); |
| 3426 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3427 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3428 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3429 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3430 | Py_DECREF(v); |
| 3431 | return b; |
| 3432 | } |
| 3433 | |
| 3434 | PyErr_Format(PyExc_TypeError, |
| 3435 | "encoder did not return a bytes object (type=%.400s)", |
| 3436 | Py_TYPE(v)->tp_name); |
| 3437 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3438 | return NULL; |
| 3439 | } |
| 3440 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3441 | PyObject * |
| 3442 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3443 | const char *encoding, |
| 3444 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3445 | { |
| 3446 | PyObject *v; |
| 3447 | |
| 3448 | if (!PyUnicode_Check(unicode)) { |
| 3449 | PyErr_BadArgument(); |
| 3450 | goto onError; |
| 3451 | } |
| 3452 | |
| 3453 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3454 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3455 | |
| 3456 | /* Encode via the codec registry */ |
| 3457 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3458 | if (v == NULL) |
| 3459 | goto onError; |
| 3460 | if (!PyUnicode_Check(v)) { |
| 3461 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3462 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3463 | Py_TYPE(v)->tp_name); |
| 3464 | Py_DECREF(v); |
| 3465 | goto onError; |
| 3466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3469 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | return NULL; |
| 3471 | } |
| 3472 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3473 | static size_t |
| 3474 | mbstowcs_errorpos(const char *str, size_t len) |
| 3475 | { |
| 3476 | #ifdef HAVE_MBRTOWC |
| 3477 | const char *start = str; |
| 3478 | mbstate_t mbs; |
| 3479 | size_t converted; |
| 3480 | wchar_t ch; |
| 3481 | |
| 3482 | memset(&mbs, 0, sizeof mbs); |
| 3483 | while (len) |
| 3484 | { |
| 3485 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3486 | if (converted == 0) |
| 3487 | /* Reached end of string */ |
| 3488 | break; |
| 3489 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3490 | /* Conversion error or incomplete character */ |
| 3491 | return str - start; |
| 3492 | } |
| 3493 | else { |
| 3494 | str += converted; |
| 3495 | len -= converted; |
| 3496 | } |
| 3497 | } |
| 3498 | /* failed to find the undecodable byte sequence */ |
| 3499 | return 0; |
| 3500 | #endif |
| 3501 | return 0; |
| 3502 | } |
| 3503 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3504 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3505 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3506 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3507 | { |
| 3508 | wchar_t smallbuf[256]; |
| 3509 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3510 | wchar_t *wstr; |
| 3511 | size_t wlen, wlen2; |
| 3512 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3513 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3514 | size_t error_pos; |
| 3515 | char *errmsg; |
| 3516 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3517 | |
| 3518 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3519 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3520 | |
| 3521 | if (str[len] != '\0' || len != strlen(str)) { |
| 3522 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3523 | return NULL; |
| 3524 | } |
| 3525 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3526 | if (surrogateescape) { |
| 3527 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3528 | wstr = _Py_char2wchar(str, &wlen); |
| 3529 | if (wstr == NULL) { |
| 3530 | if (wlen == (size_t)-1) |
| 3531 | PyErr_NoMemory(); |
| 3532 | else |
| 3533 | PyErr_SetFromErrno(PyExc_OSError); |
| 3534 | return NULL; |
| 3535 | } |
| 3536 | |
| 3537 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3538 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3539 | } |
| 3540 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3541 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3542 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3543 | wlen = mbstowcs(NULL, str, 0); |
| 3544 | #else |
| 3545 | wlen = len; |
| 3546 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3547 | if (wlen == (size_t)-1) |
| 3548 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3549 | if (wlen+1 <= smallbuf_len) { |
| 3550 | wstr = smallbuf; |
| 3551 | } |
| 3552 | else { |
| 3553 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3554 | return PyErr_NoMemory(); |
| 3555 | |
| 3556 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3557 | if (!wstr) |
| 3558 | return PyErr_NoMemory(); |
| 3559 | } |
| 3560 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3561 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3562 | if (wlen2 == (size_t)-1) { |
| 3563 | if (wstr != smallbuf) |
| 3564 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3565 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3566 | } |
| 3567 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3568 | assert(wlen2 == wlen); |
| 3569 | #endif |
| 3570 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3571 | if (wstr != smallbuf) |
| 3572 | PyMem_Free(wstr); |
| 3573 | } |
| 3574 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3575 | |
| 3576 | decode_error: |
| 3577 | errmsg = strerror(errno); |
| 3578 | assert(errmsg != NULL); |
| 3579 | |
| 3580 | error_pos = mbstowcs_errorpos(str, len); |
| 3581 | if (errmsg != NULL) { |
| 3582 | size_t errlen; |
| 3583 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3584 | if (wstr != NULL) { |
| 3585 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3586 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3587 | } else |
| 3588 | errmsg = NULL; |
| 3589 | } |
| 3590 | if (errmsg == NULL) |
| 3591 | reason = PyUnicode_FromString( |
| 3592 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3593 | if (reason == NULL) |
| 3594 | return NULL; |
| 3595 | |
| 3596 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3597 | "locale", str, len, |
| 3598 | (Py_ssize_t)error_pos, |
| 3599 | (Py_ssize_t)(error_pos+1), |
| 3600 | reason); |
| 3601 | Py_DECREF(reason); |
| 3602 | if (exc != NULL) { |
| 3603 | PyCodec_StrictErrors(exc); |
| 3604 | Py_XDECREF(exc); |
| 3605 | } |
| 3606 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3610 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3611 | { |
| 3612 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3613 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | |
| 3617 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3618 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3619 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3620 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3621 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3622 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3623 | PyObject* |
| 3624 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3625 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3626 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3627 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3628 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3629 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3630 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3631 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3632 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3633 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3634 | the Python codec requires to encode at least its own filename. Use the C |
| 3635 | version of the locale codec until the codec registry is initialized and |
| 3636 | the Python codec is loaded. |
| 3637 | |
| 3638 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3639 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3640 | subinterpreters. */ |
| 3641 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3642 | return PyUnicode_Decode(s, size, |
| 3643 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3644 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3645 | } |
| 3646 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3647 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3648 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3649 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3652 | |
| 3653 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3654 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3655 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3656 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3657 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3658 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3659 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3660 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3661 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3662 | if (pos == -1) |
| 3663 | return 0; |
| 3664 | else |
| 3665 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3666 | } |
| 3667 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3668 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3669 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3670 | { |
| 3671 | PyObject *output = NULL; |
| 3672 | Py_ssize_t size; |
| 3673 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3674 | if (arg == NULL) { |
| 3675 | Py_DECREF(*(PyObject**)addr); |
| 3676 | return 1; |
| 3677 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3678 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3679 | output = arg; |
| 3680 | Py_INCREF(output); |
| 3681 | } |
| 3682 | else { |
| 3683 | arg = PyUnicode_FromObject(arg); |
| 3684 | if (!arg) |
| 3685 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3686 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3687 | Py_DECREF(arg); |
| 3688 | if (!output) |
| 3689 | return 0; |
| 3690 | if (!PyBytes_Check(output)) { |
| 3691 | Py_DECREF(output); |
| 3692 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3693 | return 0; |
| 3694 | } |
| 3695 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3696 | size = PyBytes_GET_SIZE(output); |
| 3697 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3698 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3699 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3700 | Py_DECREF(output); |
| 3701 | return 0; |
| 3702 | } |
| 3703 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3704 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3708 | int |
| 3709 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3710 | { |
| 3711 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3712 | if (arg == NULL) { |
| 3713 | Py_DECREF(*(PyObject**)addr); |
| 3714 | return 1; |
| 3715 | } |
| 3716 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3717 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3718 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3719 | output = arg; |
| 3720 | Py_INCREF(output); |
| 3721 | } |
| 3722 | else { |
| 3723 | arg = PyBytes_FromObject(arg); |
| 3724 | if (!arg) |
| 3725 | return 0; |
| 3726 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3727 | PyBytes_GET_SIZE(arg)); |
| 3728 | Py_DECREF(arg); |
| 3729 | if (!output) |
| 3730 | return 0; |
| 3731 | if (!PyUnicode_Check(output)) { |
| 3732 | Py_DECREF(output); |
| 3733 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3734 | return 0; |
| 3735 | } |
| 3736 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3737 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3738 | Py_DECREF(output); |
| 3739 | return 0; |
| 3740 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3742 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3743 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3744 | Py_DECREF(output); |
| 3745 | return 0; |
| 3746 | } |
| 3747 | *(PyObject**)addr = output; |
| 3748 | return Py_CLEANUP_SUPPORTED; |
| 3749 | } |
| 3750 | |
| 3751 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3752 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3753 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3754 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3755 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3757 | if (!PyUnicode_Check(unicode)) { |
| 3758 | PyErr_BadArgument(); |
| 3759 | return NULL; |
| 3760 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3761 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3762 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3763 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3764 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3765 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3767 | if (bytes == NULL) |
| 3768 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3769 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3770 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3771 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3772 | Py_DECREF(bytes); |
| 3773 | return NULL; |
| 3774 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3775 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3776 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3777 | PyBytes_AS_STRING(bytes), |
| 3778 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3779 | Py_DECREF(bytes); |
| 3780 | } |
| 3781 | |
| 3782 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3783 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3784 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3789 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3790 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3791 | } |
| 3792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3793 | Py_UNICODE * |
| 3794 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3795 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3796 | const unsigned char *one_byte; |
| 3797 | #if SIZEOF_WCHAR_T == 4 |
| 3798 | const Py_UCS2 *two_bytes; |
| 3799 | #else |
| 3800 | const Py_UCS4 *four_bytes; |
| 3801 | const Py_UCS4 *ucs4_end; |
| 3802 | Py_ssize_t num_surrogates; |
| 3803 | #endif |
| 3804 | wchar_t *w; |
| 3805 | wchar_t *wchar_end; |
| 3806 | |
| 3807 | if (!PyUnicode_Check(unicode)) { |
| 3808 | PyErr_BadArgument(); |
| 3809 | return NULL; |
| 3810 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3813 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3814 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3816 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3817 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3818 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3819 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3820 | num_surrogates = 0; |
| 3821 | |
| 3822 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3823 | if (*four_bytes > 0xFFFF) |
| 3824 | ++num_surrogates; |
| 3825 | } |
| 3826 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3827 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3828 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3829 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3830 | PyErr_NoMemory(); |
| 3831 | return NULL; |
| 3832 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3833 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3835 | w = _PyUnicode_WSTR(unicode); |
| 3836 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3837 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3838 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3839 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3840 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3842 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3843 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | } |
| 3845 | else |
| 3846 | *w = *four_bytes; |
| 3847 | |
| 3848 | if (w > wchar_end) { |
| 3849 | assert(0 && "Miscalculated string end"); |
| 3850 | } |
| 3851 | } |
| 3852 | *w = 0; |
| 3853 | #else |
| 3854 | /* sizeof(wchar_t) == 4 */ |
| 3855 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3856 | "should share memory already."); |
| 3857 | return NULL; |
| 3858 | #endif |
| 3859 | } |
| 3860 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3861 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3862 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3863 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3864 | PyErr_NoMemory(); |
| 3865 | return NULL; |
| 3866 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3867 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3868 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3869 | w = _PyUnicode_WSTR(unicode); |
| 3870 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3872 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3873 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | for (; w < wchar_end; ++one_byte, ++w) |
| 3875 | *w = *one_byte; |
| 3876 | /* null-terminate the wstr */ |
| 3877 | *w = 0; |
| 3878 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3879 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3880 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3881 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3883 | *w = *two_bytes; |
| 3884 | /* null-terminate the wstr */ |
| 3885 | *w = 0; |
| 3886 | #else |
| 3887 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3888 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3889 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | Py_FatalError("Impossible unicode object state, wstr " |
| 3891 | "and str should share memory already."); |
| 3892 | return NULL; |
| 3893 | #endif |
| 3894 | } |
| 3895 | else { |
| 3896 | assert(0 && "This should never happen."); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3901 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3902 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3905 | Py_UNICODE * |
| 3906 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3912 | Py_ssize_t |
| 3913 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3914 | { |
| 3915 | if (!PyUnicode_Check(unicode)) { |
| 3916 | PyErr_BadArgument(); |
| 3917 | goto onError; |
| 3918 | } |
| 3919 | return PyUnicode_GET_SIZE(unicode); |
| 3920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3921 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3922 | return -1; |
| 3923 | } |
| 3924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3925 | Py_ssize_t |
| 3926 | PyUnicode_GetLength(PyObject *unicode) |
| 3927 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3928 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3929 | PyErr_BadArgument(); |
| 3930 | return -1; |
| 3931 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3932 | if (PyUnicode_READY(unicode) == -1) |
| 3933 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3934 | return PyUnicode_GET_LENGTH(unicode); |
| 3935 | } |
| 3936 | |
| 3937 | Py_UCS4 |
| 3938 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3939 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3940 | void *data; |
| 3941 | int kind; |
| 3942 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3943 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3944 | PyErr_BadArgument(); |
| 3945 | return (Py_UCS4)-1; |
| 3946 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3947 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3948 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | return (Py_UCS4)-1; |
| 3950 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3951 | data = PyUnicode_DATA(unicode); |
| 3952 | kind = PyUnicode_KIND(unicode); |
| 3953 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | int |
| 3957 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3958 | { |
| 3959 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3960 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | return -1; |
| 3962 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3963 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3964 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3965 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3966 | return -1; |
| 3967 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3968 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3969 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3970 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3971 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3972 | return -1; |
| 3973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3974 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3975 | index, ch); |
| 3976 | return 0; |
| 3977 | } |
| 3978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3979 | const char * |
| 3980 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3981 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3982 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3985 | /* create or adjust a UnicodeDecodeError */ |
| 3986 | static void |
| 3987 | make_decode_exception(PyObject **exceptionObject, |
| 3988 | const char *encoding, |
| 3989 | const char *input, Py_ssize_t length, |
| 3990 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3991 | const char *reason) |
| 3992 | { |
| 3993 | if (*exceptionObject == NULL) { |
| 3994 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3995 | encoding, input, length, startpos, endpos, reason); |
| 3996 | } |
| 3997 | else { |
| 3998 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3999 | goto onError; |
| 4000 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4001 | goto onError; |
| 4002 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4003 | goto onError; |
| 4004 | } |
| 4005 | return; |
| 4006 | |
| 4007 | onError: |
| 4008 | Py_DECREF(*exceptionObject); |
| 4009 | *exceptionObject = NULL; |
| 4010 | } |
| 4011 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4012 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | /* error handling callback helper: |
| 4014 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4015 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4016 | and adjust various state variables. |
| 4017 | return 0 on success, -1 on error |
| 4018 | */ |
| 4019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4020 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4021 | unicode_decode_call_errorhandler_wchar( |
| 4022 | const char *errors, PyObject **errorHandler, |
| 4023 | const char *encoding, const char *reason, |
| 4024 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4025 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4026 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4027 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4028 | 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] | 4029 | |
| 4030 | PyObject *restuple = NULL; |
| 4031 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4032 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4033 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4034 | Py_ssize_t requiredsize; |
| 4035 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4036 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4037 | wchar_t *repwstr; |
| 4038 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4040 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4041 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4042 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4043 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | *errorHandler = PyCodec_LookupError(errors); |
| 4045 | if (*errorHandler == NULL) |
| 4046 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4049 | make_decode_exception(exceptionObject, |
| 4050 | encoding, |
| 4051 | *input, *inend - *input, |
| 4052 | *startinpos, *endinpos, |
| 4053 | reason); |
| 4054 | if (*exceptionObject == NULL) |
| 4055 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4056 | |
| 4057 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4058 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4059 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4060 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4061 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | } |
| 4064 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4066 | |
| 4067 | /* Copy back the bytes variables, which might have been modified by the |
| 4068 | callback */ |
| 4069 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4070 | if (!inputobj) |
| 4071 | goto onError; |
| 4072 | if (!PyBytes_Check(inputobj)) { |
| 4073 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4074 | } |
| 4075 | *input = PyBytes_AS_STRING(inputobj); |
| 4076 | insize = PyBytes_GET_SIZE(inputobj); |
| 4077 | *inend = *input + insize; |
| 4078 | /* we can DECREF safely, as the exception has another reference, |
| 4079 | so the object won't go away. */ |
| 4080 | Py_DECREF(inputobj); |
| 4081 | |
| 4082 | if (newpos<0) |
| 4083 | newpos = insize+newpos; |
| 4084 | if (newpos<0 || newpos>insize) { |
| 4085 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4086 | goto onError; |
| 4087 | } |
| 4088 | |
| 4089 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4090 | if (repwstr == NULL) |
| 4091 | goto onError; |
| 4092 | /* need more space? (at least enough for what we |
| 4093 | have+the replacement+the rest of the string (starting |
| 4094 | at the new input position), so we won't have to check space |
| 4095 | when there are no errors in the rest of the string) */ |
| 4096 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4097 | if (requiredsize > outsize) { |
| 4098 | if (requiredsize < 2*outsize) |
| 4099 | requiredsize = 2*outsize; |
| 4100 | if (unicode_resize(output, requiredsize) < 0) |
| 4101 | goto onError; |
| 4102 | } |
| 4103 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4104 | *outpos += repwlen; |
| 4105 | |
| 4106 | *endinpos = newpos; |
| 4107 | *inptr = *input + newpos; |
| 4108 | |
| 4109 | /* we made it! */ |
| 4110 | Py_XDECREF(restuple); |
| 4111 | return 0; |
| 4112 | |
| 4113 | onError: |
| 4114 | Py_XDECREF(restuple); |
| 4115 | return -1; |
| 4116 | } |
| 4117 | #endif /* HAVE_MBCS */ |
| 4118 | |
| 4119 | static int |
| 4120 | unicode_decode_call_errorhandler_writer( |
| 4121 | const char *errors, PyObject **errorHandler, |
| 4122 | const char *encoding, const char *reason, |
| 4123 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4124 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4125 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4126 | { |
| 4127 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4128 | |
| 4129 | PyObject *restuple = NULL; |
| 4130 | PyObject *repunicode = NULL; |
| 4131 | Py_ssize_t insize; |
| 4132 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4133 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4134 | PyObject *inputobj = NULL; |
| 4135 | |
| 4136 | if (*errorHandler == NULL) { |
| 4137 | *errorHandler = PyCodec_LookupError(errors); |
| 4138 | if (*errorHandler == NULL) |
| 4139 | goto onError; |
| 4140 | } |
| 4141 | |
| 4142 | make_decode_exception(exceptionObject, |
| 4143 | encoding, |
| 4144 | *input, *inend - *input, |
| 4145 | *startinpos, *endinpos, |
| 4146 | reason); |
| 4147 | if (*exceptionObject == NULL) |
| 4148 | goto onError; |
| 4149 | |
| 4150 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4151 | if (restuple == NULL) |
| 4152 | goto onError; |
| 4153 | if (!PyTuple_Check(restuple)) { |
| 4154 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4155 | goto onError; |
| 4156 | } |
| 4157 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4158 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4159 | |
| 4160 | /* Copy back the bytes variables, which might have been modified by the |
| 4161 | callback */ |
| 4162 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4163 | if (!inputobj) |
| 4164 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4165 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4167 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4168 | *input = PyBytes_AS_STRING(inputobj); |
| 4169 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4170 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4171 | /* we can DECREF safely, as the exception has another reference, |
| 4172 | so the object won't go away. */ |
| 4173 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4174 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4177 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4179 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4180 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4181 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4182 | if (PyUnicode_READY(repunicode) < 0) |
| 4183 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4184 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 4185 | writer->min_length += replen; |
| 4186 | if (replen > 1) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4187 | writer->overallocate = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4188 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4189 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4190 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4192 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4193 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4195 | Py_XDECREF(restuple); |
| 4196 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4198 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4199 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4200 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4203 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4204 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4205 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4206 | |
| 4207 | /* Three simple macros defining base-64. */ |
| 4208 | |
| 4209 | /* Is c a base-64 character? */ |
| 4210 | |
| 4211 | #define IS_BASE64(c) \ |
| 4212 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4213 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4214 | ((c) >= '0' && (c) <= '9') || \ |
| 4215 | (c) == '+' || (c) == '/') |
| 4216 | |
| 4217 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4218 | |
| 4219 | #define FROM_BASE64(c) \ |
| 4220 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4221 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4222 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4223 | (c) == '+' ? 62 : 63) |
| 4224 | |
| 4225 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4226 | |
| 4227 | #define TO_BASE64(n) \ |
| 4228 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4229 | |
| 4230 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4231 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4232 | * byte not decoding to itself is the + which begins a base64 |
| 4233 | * string. */ |
| 4234 | |
| 4235 | #define DECODE_DIRECT(c) \ |
| 4236 | ((c) <= 127 && (c) != '+') |
| 4237 | |
| 4238 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4239 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4240 | * the above). See RFC2152. This array identifies these different |
| 4241 | * sets: |
| 4242 | * 0 : "Set D" |
| 4243 | * alphanumeric and '(),-./:? |
| 4244 | * 1 : "Set O" |
| 4245 | * !"#$%&*;<=>@[]^_`{|} |
| 4246 | * 2 : "whitespace" |
| 4247 | * ht nl cr sp |
| 4248 | * 3 : special (must be base64 encoded) |
| 4249 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4250 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4252 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4253 | char utf7_category[128] = { |
| 4254 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4255 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4256 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4257 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4258 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4259 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4260 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4261 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4262 | /* @ A B C D E F G H I J K L M N O */ |
| 4263 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4264 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4265 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4266 | /* ` a b c d e f g h i j k l m n o */ |
| 4267 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4268 | /* p q r s t u v w x y z { | } ~ del */ |
| 4269 | 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] | 4270 | }; |
| 4271 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4272 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4273 | * answer depends on whether we are encoding set O as itself, and also |
| 4274 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4275 | * clear that the answers to these questions vary between |
| 4276 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4277 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4278 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4279 | ((c) < 128 && (c) > 0 && \ |
| 4280 | ((utf7_category[(c)] == 0) || \ |
| 4281 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4282 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4284 | PyObject * |
| 4285 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4286 | Py_ssize_t size, |
| 4287 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4288 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4289 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4290 | } |
| 4291 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4292 | /* The decoder. The only state we preserve is our read position, |
| 4293 | * i.e. how many characters we have consumed. So if we end in the |
| 4294 | * middle of a shift sequence we have to back off the read position |
| 4295 | * and the output to the beginning of the sequence, otherwise we lose |
| 4296 | * all the shift state (seen bits, number of bits seen, high |
| 4297 | * surrogate). */ |
| 4298 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4299 | PyObject * |
| 4300 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4301 | Py_ssize_t size, |
| 4302 | const char *errors, |
| 4303 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4304 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4306 | Py_ssize_t startinpos; |
| 4307 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4308 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4309 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | const char *errmsg = ""; |
| 4311 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4312 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4313 | unsigned int base64bits = 0; |
| 4314 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4315 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4316 | PyObject *errorHandler = NULL; |
| 4317 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4319 | if (size == 0) { |
| 4320 | if (consumed) |
| 4321 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4322 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4323 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4324 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4325 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4326 | _PyUnicodeWriter_Init(&writer); |
| 4327 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4328 | |
| 4329 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | e = s + size; |
| 4331 | |
| 4332 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4333 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4334 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4335 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4337 | if (inShift) { /* in a base-64 section */ |
| 4338 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4339 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4340 | base64bits += 6; |
| 4341 | s++; |
| 4342 | if (base64bits >= 16) { |
| 4343 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4344 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | base64bits -= 16; |
| 4346 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4347 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4348 | if (surrogate) { |
| 4349 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4350 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4351 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4352 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4353 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4355 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4356 | } |
| 4357 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4358 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4359 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4363 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | /* first surrogate */ |
| 4365 | surrogate = outCh; |
| 4366 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4368 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4369 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | inShift = 0; |
| 4375 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4376 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4377 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4378 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4379 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4380 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4381 | if (base64bits > 0) { /* left-over bits */ |
| 4382 | if (base64bits >= 6) { |
| 4383 | /* We've seen at least one base-64 character */ |
| 4384 | errmsg = "partial character in shift sequence"; |
| 4385 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4386 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4387 | else { |
| 4388 | /* Some bits remain; they should be zero */ |
| 4389 | if (base64buffer != 0) { |
| 4390 | errmsg = "non-zero padding bits in shift sequence"; |
| 4391 | goto utf7Error; |
| 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | if (ch != '-') { |
| 4396 | /* '-' is absorbed; other terminating |
| 4397 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4398 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4399 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4404 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4405 | s++; /* consume '+' */ |
| 4406 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4407 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4408 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4409 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4410 | } |
| 4411 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4413 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4414 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4415 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4418 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4420 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4421 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | else { |
| 4424 | startinpos = s-starts; |
| 4425 | s++; |
| 4426 | errmsg = "unexpected special character"; |
| 4427 | goto utf7Error; |
| 4428 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4432 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4433 | errors, &errorHandler, |
| 4434 | "utf7", errmsg, |
| 4435 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4436 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4437 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | /* end of string */ |
| 4441 | |
| 4442 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4443 | /* if we're in an inconsistent state, that's an error */ |
| 4444 | if (surrogate || |
| 4445 | (base64bits >= 6) || |
| 4446 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4448 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4449 | errors, &errorHandler, |
| 4450 | "utf7", "unterminated shift sequence", |
| 4451 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4452 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4453 | goto onError; |
| 4454 | if (s < e) |
| 4455 | goto restart; |
| 4456 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4460 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4462 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | } |
| 4465 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4468 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4469 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4470 | Py_XDECREF(errorHandler); |
| 4471 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4472 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4474 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4475 | Py_XDECREF(errorHandler); |
| 4476 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4477 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4478 | return NULL; |
| 4479 | } |
| 4480 | |
| 4481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4482 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4483 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4484 | int base64SetO, |
| 4485 | int base64WhiteSpace, |
| 4486 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4487 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4488 | int kind; |
| 4489 | void *data; |
| 4490 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4491 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4492 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4493 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4494 | unsigned int base64bits = 0; |
| 4495 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4496 | char * out; |
| 4497 | char * start; |
| 4498 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4499 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4500 | return NULL; |
| 4501 | kind = PyUnicode_KIND(str); |
| 4502 | data = PyUnicode_DATA(str); |
| 4503 | len = PyUnicode_GET_LENGTH(str); |
| 4504 | |
| 4505 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4506 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4507 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4508 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4509 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4510 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4511 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4512 | if (v == NULL) |
| 4513 | return NULL; |
| 4514 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4515 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4516 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4517 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4518 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4519 | if (inShift) { |
| 4520 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4521 | /* shifting out */ |
| 4522 | if (base64bits) { /* output remaining bits */ |
| 4523 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4524 | base64buffer = 0; |
| 4525 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4526 | } |
| 4527 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4528 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4529 | so no '-' is required, except if the character is itself a '-' */ |
| 4530 | if (IS_BASE64(ch) || ch == '-') { |
| 4531 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4532 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4533 | *out++ = (char) ch; |
| 4534 | } |
| 4535 | else { |
| 4536 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4537 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4538 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | else { /* not in a shift sequence */ |
| 4540 | if (ch == '+') { |
| 4541 | *out++ = '+'; |
| 4542 | *out++ = '-'; |
| 4543 | } |
| 4544 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4545 | *out++ = (char) ch; |
| 4546 | } |
| 4547 | else { |
| 4548 | *out++ = '+'; |
| 4549 | inShift = 1; |
| 4550 | goto encode_char; |
| 4551 | } |
| 4552 | } |
| 4553 | continue; |
| 4554 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4555 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4556 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4557 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4558 | /* code first surrogate */ |
| 4559 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4560 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | while (base64bits >= 6) { |
| 4562 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4563 | base64bits -= 6; |
| 4564 | } |
| 4565 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4566 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4567 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4568 | base64bits += 16; |
| 4569 | base64buffer = (base64buffer << 16) | ch; |
| 4570 | while (base64bits >= 6) { |
| 4571 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4572 | base64bits -= 6; |
| 4573 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4574 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4575 | if (base64bits) |
| 4576 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4577 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4578 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4579 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4580 | return NULL; |
| 4581 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4582 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4583 | PyObject * |
| 4584 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4585 | Py_ssize_t size, |
| 4586 | int base64SetO, |
| 4587 | int base64WhiteSpace, |
| 4588 | const char *errors) |
| 4589 | { |
| 4590 | PyObject *result; |
| 4591 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4592 | if (tmp == NULL) |
| 4593 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4594 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4595 | base64WhiteSpace, errors); |
| 4596 | Py_DECREF(tmp); |
| 4597 | return result; |
| 4598 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4599 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4600 | #undef IS_BASE64 |
| 4601 | #undef FROM_BASE64 |
| 4602 | #undef TO_BASE64 |
| 4603 | #undef DECODE_DIRECT |
| 4604 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4606 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4607 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4608 | PyObject * |
| 4609 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4610 | Py_ssize_t size, |
| 4611 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4613 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4614 | } |
| 4615 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | #include "stringlib/asciilib.h" |
| 4617 | #include "stringlib/codecs.h" |
| 4618 | #include "stringlib/undef.h" |
| 4619 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4620 | #include "stringlib/ucs1lib.h" |
| 4621 | #include "stringlib/codecs.h" |
| 4622 | #include "stringlib/undef.h" |
| 4623 | |
| 4624 | #include "stringlib/ucs2lib.h" |
| 4625 | #include "stringlib/codecs.h" |
| 4626 | #include "stringlib/undef.h" |
| 4627 | |
| 4628 | #include "stringlib/ucs4lib.h" |
| 4629 | #include "stringlib/codecs.h" |
| 4630 | #include "stringlib/undef.h" |
| 4631 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4632 | /* Mask to quickly check whether a C 'long' contains a |
| 4633 | non-ASCII, UTF8-encoded char. */ |
| 4634 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4635 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4637 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4638 | #else |
| 4639 | # error C 'long' size should be either 4 or 8! |
| 4640 | #endif |
| 4641 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4642 | static Py_ssize_t |
| 4643 | 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] | 4644 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4646 | 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] | 4647 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4648 | /* |
| 4649 | * Issue #17237: m68k is a bit different from most architectures in |
| 4650 | * that objects do not use "natural alignment" - for example, int and |
| 4651 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4652 | * won't work; also, tests have shown that skipping the "optimised |
| 4653 | * version" will even speed up m68k. |
| 4654 | */ |
| 4655 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4656 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4657 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4658 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4659 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4660 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4661 | /* Help allocation */ |
| 4662 | const char *_p = p; |
| 4663 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4664 | while (_p < aligned_end) { |
| 4665 | unsigned long value = *(const unsigned long *) _p; |
| 4666 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4667 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4668 | *((unsigned long *)q) = value; |
| 4669 | _p += SIZEOF_LONG; |
| 4670 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4671 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4672 | p = _p; |
| 4673 | while (p < end) { |
| 4674 | if ((unsigned char)*p & 0x80) |
| 4675 | break; |
| 4676 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4677 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4679 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4681 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | while (p < end) { |
| 4683 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4684 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4685 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4686 | /* Help allocation */ |
| 4687 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | while (_p < aligned_end) { |
| 4689 | unsigned long value = *(unsigned long *) _p; |
| 4690 | if (value & ASCII_CHAR_MASK) |
| 4691 | break; |
| 4692 | _p += SIZEOF_LONG; |
| 4693 | } |
| 4694 | p = _p; |
| 4695 | if (_p == end) |
| 4696 | break; |
| 4697 | } |
| 4698 | if ((unsigned char)*p & 0x80) |
| 4699 | break; |
| 4700 | ++p; |
| 4701 | } |
| 4702 | memcpy(dest, start, p - start); |
| 4703 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4704 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4705 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4706 | PyObject * |
| 4707 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4708 | Py_ssize_t size, |
| 4709 | const char *errors, |
| 4710 | Py_ssize_t *consumed) |
| 4711 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4712 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4713 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4714 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4715 | |
| 4716 | Py_ssize_t startinpos; |
| 4717 | Py_ssize_t endinpos; |
| 4718 | const char *errmsg = ""; |
| 4719 | PyObject *errorHandler = NULL; |
| 4720 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4721 | |
| 4722 | if (size == 0) { |
| 4723 | if (consumed) |
| 4724 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4725 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4729 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4730 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | *consumed = 1; |
| 4732 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4733 | } |
| 4734 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4735 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4736 | writer.min_length = size; |
| 4737 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4738 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4739 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4740 | writer.pos = ascii_decode(s, end, writer.data); |
| 4741 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4742 | while (s < end) { |
| 4743 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4744 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4745 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4746 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4747 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4748 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4749 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4751 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4752 | } else { |
| 4753 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | switch (ch) { |
| 4758 | case 0: |
| 4759 | if (s == end || consumed) |
| 4760 | goto End; |
| 4761 | errmsg = "unexpected end of data"; |
| 4762 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4763 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4764 | break; |
| 4765 | case 1: |
| 4766 | errmsg = "invalid start byte"; |
| 4767 | startinpos = s - starts; |
| 4768 | endinpos = startinpos + 1; |
| 4769 | break; |
| 4770 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4771 | case 3: |
| 4772 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4773 | errmsg = "invalid continuation byte"; |
| 4774 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4775 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4776 | break; |
| 4777 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4778 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4779 | goto onError; |
| 4780 | continue; |
| 4781 | } |
| 4782 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4783 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4784 | errors, &errorHandler, |
| 4785 | "utf-8", errmsg, |
| 4786 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4787 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4788 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4789 | } |
| 4790 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4791 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4792 | if (consumed) |
| 4793 | *consumed = s - starts; |
| 4794 | |
| 4795 | Py_XDECREF(errorHandler); |
| 4796 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4797 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | |
| 4799 | onError: |
| 4800 | Py_XDECREF(errorHandler); |
| 4801 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4802 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4803 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4806 | #ifdef __APPLE__ |
| 4807 | |
| 4808 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4809 | used to decode the command line arguments on Mac OS X. |
| 4810 | |
| 4811 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4812 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4813 | |
| 4814 | wchar_t* |
| 4815 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4816 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4817 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4818 | wchar_t *unicode; |
| 4819 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4820 | |
| 4821 | /* Note: size will always be longer than the resulting Unicode |
| 4822 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4823 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4824 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4825 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4826 | if (!unicode) |
| 4827 | return NULL; |
| 4828 | |
| 4829 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4830 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4831 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4832 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4833 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4835 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4837 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4838 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4839 | if (ch > 0xFF) { |
| 4840 | #if SIZEOF_WCHAR_T == 4 |
| 4841 | assert(0); |
| 4842 | #else |
| 4843 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4844 | /* compute and append the two surrogates: */ |
| 4845 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4846 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4847 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4848 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4849 | else { |
| 4850 | if (!ch && s == e) |
| 4851 | break; |
| 4852 | /* surrogateescape */ |
| 4853 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4854 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4856 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4857 | return unicode; |
| 4858 | } |
| 4859 | |
| 4860 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4862 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4863 | |
| 4864 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4865 | and allocate exactly as much space needed at the end. Else allocate the |
| 4866 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4867 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4868 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4869 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4870 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4872 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4873 | void *data; |
| 4874 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4876 | if (!PyUnicode_Check(unicode)) { |
| 4877 | PyErr_BadArgument(); |
| 4878 | return NULL; |
| 4879 | } |
| 4880 | |
| 4881 | if (PyUnicode_READY(unicode) == -1) |
| 4882 | return NULL; |
| 4883 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4884 | if (PyUnicode_UTF8(unicode)) |
| 4885 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4886 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4887 | |
| 4888 | kind = PyUnicode_KIND(unicode); |
| 4889 | data = PyUnicode_DATA(unicode); |
| 4890 | size = PyUnicode_GET_LENGTH(unicode); |
| 4891 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4892 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4893 | default: |
| 4894 | assert(0); |
| 4895 | case PyUnicode_1BYTE_KIND: |
| 4896 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4897 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4898 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4899 | case PyUnicode_2BYTE_KIND: |
| 4900 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4901 | case PyUnicode_4BYTE_KIND: |
| 4902 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4903 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4906 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4907 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4908 | Py_ssize_t size, |
| 4909 | const char *errors) |
| 4910 | { |
| 4911 | PyObject *v, *unicode; |
| 4912 | |
| 4913 | unicode = PyUnicode_FromUnicode(s, size); |
| 4914 | if (unicode == NULL) |
| 4915 | return NULL; |
| 4916 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4917 | Py_DECREF(unicode); |
| 4918 | return v; |
| 4919 | } |
| 4920 | |
| 4921 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4922 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4924 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4927 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4928 | |
| 4929 | PyObject * |
| 4930 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4931 | Py_ssize_t size, |
| 4932 | const char *errors, |
| 4933 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4934 | { |
| 4935 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4936 | } |
| 4937 | |
| 4938 | PyObject * |
| 4939 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | Py_ssize_t size, |
| 4941 | const char *errors, |
| 4942 | int *byteorder, |
| 4943 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4944 | { |
| 4945 | const char *starts = s; |
| 4946 | Py_ssize_t startinpos; |
| 4947 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4948 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4949 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4950 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4951 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | PyObject *errorHandler = NULL; |
| 4953 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4954 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4955 | q = (unsigned char *)s; |
| 4956 | e = q + size; |
| 4957 | |
| 4958 | if (byteorder) |
| 4959 | bo = *byteorder; |
| 4960 | |
| 4961 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4962 | byte order setting accordingly. In native mode, the leading BOM |
| 4963 | mark is skipped, in all other modes, it is copied to the output |
| 4964 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4965 | if (bo == 0 && size >= 4) { |
| 4966 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4967 | if (bom == 0x0000FEFF) { |
| 4968 | bo = -1; |
| 4969 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4971 | else if (bom == 0xFFFE0000) { |
| 4972 | bo = 1; |
| 4973 | q += 4; |
| 4974 | } |
| 4975 | if (byteorder) |
| 4976 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4979 | if (q == e) { |
| 4980 | if (consumed) |
| 4981 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4982 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4985 | #ifdef WORDS_BIGENDIAN |
| 4986 | le = bo < 0; |
| 4987 | #else |
| 4988 | le = bo <= 0; |
| 4989 | #endif |
| 4990 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4991 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4992 | writer.min_length = (e - q + 3) / 4; |
| 4993 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4994 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4995 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4996 | while (1) { |
| 4997 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4998 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4999 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5000 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5001 | enum PyUnicode_Kind kind = writer.kind; |
| 5002 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5003 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5004 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5005 | if (le) { |
| 5006 | do { |
| 5007 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5008 | if (ch > maxch) |
| 5009 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5010 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5011 | q += 4; |
| 5012 | } while (q <= last); |
| 5013 | } |
| 5014 | else { |
| 5015 | do { |
| 5016 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5017 | if (ch > maxch) |
| 5018 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5019 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5020 | q += 4; |
| 5021 | } while (q <= last); |
| 5022 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5023 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | if (ch <= maxch) { |
| 5027 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5028 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5029 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5030 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5031 | startinpos = ((const char *)q) - starts; |
| 5032 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5033 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5034 | else { |
| 5035 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5036 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5037 | goto onError; |
| 5038 | q += 4; |
| 5039 | continue; |
| 5040 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5042 | startinpos = ((const char *)q) - starts; |
| 5043 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5044 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5045 | |
| 5046 | /* The remaining input chars are ignored if the callback |
| 5047 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5048 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | errors, &errorHandler, |
| 5050 | "utf32", errmsg, |
| 5051 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5052 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5058 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5059 | Py_XDECREF(errorHandler); |
| 5060 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5061 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5064 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5065 | Py_XDECREF(errorHandler); |
| 5066 | Py_XDECREF(exc); |
| 5067 | return NULL; |
| 5068 | } |
| 5069 | |
| 5070 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5071 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5072 | const char *errors, |
| 5073 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5074 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5075 | int kind; |
| 5076 | void *data; |
| 5077 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5078 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5079 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5080 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5082 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5083 | int iorder[] = {0, 1, 2, 3}; |
| 5084 | #else |
| 5085 | int iorder[] = {3, 2, 1, 0}; |
| 5086 | #endif |
| 5087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5088 | #define STORECHAR(CH) \ |
| 5089 | do { \ |
| 5090 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5091 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5092 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5093 | p[iorder[0]] = (CH) & 0xff; \ |
| 5094 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | } while(0) |
| 5096 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5097 | if (!PyUnicode_Check(str)) { |
| 5098 | PyErr_BadArgument(); |
| 5099 | return NULL; |
| 5100 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5101 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5102 | return NULL; |
| 5103 | kind = PyUnicode_KIND(str); |
| 5104 | data = PyUnicode_DATA(str); |
| 5105 | len = PyUnicode_GET_LENGTH(str); |
| 5106 | |
| 5107 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5108 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5109 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5110 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5111 | if (v == NULL) |
| 5112 | return NULL; |
| 5113 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5114 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5115 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5116 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5117 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5118 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5119 | |
| 5120 | if (byteorder == -1) { |
| 5121 | /* force LE */ |
| 5122 | iorder[0] = 0; |
| 5123 | iorder[1] = 1; |
| 5124 | iorder[2] = 2; |
| 5125 | iorder[3] = 3; |
| 5126 | } |
| 5127 | else if (byteorder == 1) { |
| 5128 | /* force BE */ |
| 5129 | iorder[0] = 3; |
| 5130 | iorder[1] = 2; |
| 5131 | iorder[2] = 1; |
| 5132 | iorder[3] = 0; |
| 5133 | } |
| 5134 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5135 | for (i = 0; i < len; i++) |
| 5136 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5137 | |
| 5138 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5139 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5140 | #undef STORECHAR |
| 5141 | } |
| 5142 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5143 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5144 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5145 | Py_ssize_t size, |
| 5146 | const char *errors, |
| 5147 | int byteorder) |
| 5148 | { |
| 5149 | PyObject *result; |
| 5150 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5151 | if (tmp == NULL) |
| 5152 | return NULL; |
| 5153 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5154 | Py_DECREF(tmp); |
| 5155 | return result; |
| 5156 | } |
| 5157 | |
| 5158 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5159 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5160 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5161 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5165 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5166 | PyObject * |
| 5167 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | Py_ssize_t size, |
| 5169 | const char *errors, |
| 5170 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5172 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5173 | } |
| 5174 | |
| 5175 | PyObject * |
| 5176 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5177 | Py_ssize_t size, |
| 5178 | const char *errors, |
| 5179 | int *byteorder, |
| 5180 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5181 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5182 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5183 | Py_ssize_t startinpos; |
| 5184 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5185 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5186 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5187 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5188 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5189 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 | PyObject *errorHandler = NULL; |
| 5191 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5193 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5194 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | |
| 5196 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5197 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5198 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5199 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5200 | byte order setting accordingly. In native mode, the leading BOM |
| 5201 | mark is skipped, in all other modes, it is copied to the output |
| 5202 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5203 | if (bo == 0 && size >= 2) { |
| 5204 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5205 | if (bom == 0xFEFF) { |
| 5206 | q += 2; |
| 5207 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5208 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5209 | else if (bom == 0xFFFE) { |
| 5210 | q += 2; |
| 5211 | bo = 1; |
| 5212 | } |
| 5213 | if (byteorder) |
| 5214 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5215 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5217 | if (q == e) { |
| 5218 | if (consumed) |
| 5219 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5220 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5221 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5222 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5223 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5224 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5225 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5226 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5227 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5228 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5229 | /* Note: size will always be longer than the resulting Unicode |
| 5230 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5231 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5232 | writer.min_length = (e - q + 1) / 2; |
| 5233 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5234 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5235 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5236 | while (1) { |
| 5237 | Py_UCS4 ch = 0; |
| 5238 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5239 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5240 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5241 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5242 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5243 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5244 | native_ordering); |
| 5245 | else |
| 5246 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5247 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5248 | native_ordering); |
| 5249 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5250 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5251 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5252 | native_ordering); |
| 5253 | } else { |
| 5254 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5255 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5256 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5257 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5258 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5259 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5261 | switch (ch) |
| 5262 | { |
| 5263 | case 0: |
| 5264 | /* remaining byte at the end? (size should be even) */ |
| 5265 | if (q == e || consumed) |
| 5266 | goto End; |
| 5267 | errmsg = "truncated data"; |
| 5268 | startinpos = ((const char *)q) - starts; |
| 5269 | endinpos = ((const char *)e) - starts; |
| 5270 | break; |
| 5271 | /* The remaining input chars are ignored if the callback |
| 5272 | chooses to skip the input */ |
| 5273 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5274 | q -= 2; |
| 5275 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5276 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5277 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5278 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5279 | endinpos = ((const char *)e) - starts; |
| 5280 | break; |
| 5281 | case 2: |
| 5282 | errmsg = "illegal encoding"; |
| 5283 | startinpos = ((const char *)q) - 2 - starts; |
| 5284 | endinpos = startinpos + 2; |
| 5285 | break; |
| 5286 | case 3: |
| 5287 | errmsg = "illegal UTF-16 surrogate"; |
| 5288 | startinpos = ((const char *)q) - 4 - starts; |
| 5289 | endinpos = startinpos + 2; |
| 5290 | break; |
| 5291 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5292 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5293 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5294 | continue; |
| 5295 | } |
| 5296 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5297 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5298 | errors, |
| 5299 | &errorHandler, |
| 5300 | "utf16", errmsg, |
| 5301 | &starts, |
| 5302 | (const char **)&e, |
| 5303 | &startinpos, |
| 5304 | &endinpos, |
| 5305 | &exc, |
| 5306 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5307 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5311 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5312 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5313 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5314 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5315 | Py_XDECREF(errorHandler); |
| 5316 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5317 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5320 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5321 | Py_XDECREF(errorHandler); |
| 5322 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | return NULL; |
| 5324 | } |
| 5325 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5326 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5327 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5328 | const char *errors, |
| 5329 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5331 | enum PyUnicode_Kind kind; |
| 5332 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5333 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5334 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5335 | unsigned short *out; |
| 5336 | Py_ssize_t bytesize; |
| 5337 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5338 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5339 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5340 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5341 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5342 | #endif |
| 5343 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5344 | if (!PyUnicode_Check(str)) { |
| 5345 | PyErr_BadArgument(); |
| 5346 | return NULL; |
| 5347 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5348 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5349 | return NULL; |
| 5350 | kind = PyUnicode_KIND(str); |
| 5351 | data = PyUnicode_DATA(str); |
| 5352 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5353 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5354 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5355 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5356 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5357 | const Py_UCS4 *end = in + len; |
| 5358 | while (in < end) |
| 5359 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5360 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5361 | } |
| 5362 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5363 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5364 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5365 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | if (v == NULL) |
| 5367 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5369 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5370 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5371 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5373 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5374 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5375 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5376 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5377 | switch (kind) { |
| 5378 | case PyUnicode_1BYTE_KIND: { |
| 5379 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5380 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5381 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5382 | case PyUnicode_2BYTE_KIND: { |
| 5383 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5384 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5385 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5386 | case PyUnicode_4BYTE_KIND: { |
| 5387 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5388 | break; |
| 5389 | } |
| 5390 | default: |
| 5391 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5392 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5393 | |
| 5394 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5395 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5398 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5399 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5400 | Py_ssize_t size, |
| 5401 | const char *errors, |
| 5402 | int byteorder) |
| 5403 | { |
| 5404 | PyObject *result; |
| 5405 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5406 | if (tmp == NULL) |
| 5407 | return NULL; |
| 5408 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5409 | Py_DECREF(tmp); |
| 5410 | return result; |
| 5411 | } |
| 5412 | |
| 5413 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5414 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5416 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5420 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5421 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5422 | if all the escapes in the string make it still a valid ASCII string. |
| 5423 | Returns -1 if any escapes were found which cause the string to |
| 5424 | pop out of ASCII range. Otherwise returns the length of the |
| 5425 | required buffer to hold the string. |
| 5426 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5427 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5428 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5429 | { |
| 5430 | const unsigned char *p = (const unsigned char *)s; |
| 5431 | const unsigned char *end = p + size; |
| 5432 | Py_ssize_t length = 0; |
| 5433 | |
| 5434 | if (size < 0) |
| 5435 | return -1; |
| 5436 | |
| 5437 | for (; p < end; ++p) { |
| 5438 | if (*p > 127) { |
| 5439 | /* Non-ASCII */ |
| 5440 | return -1; |
| 5441 | } |
| 5442 | else if (*p != '\\') { |
| 5443 | /* Normal character */ |
| 5444 | ++length; |
| 5445 | } |
| 5446 | else { |
| 5447 | /* Backslash-escape, check next char */ |
| 5448 | ++p; |
| 5449 | /* Escape sequence reaches till end of string or |
| 5450 | non-ASCII follow-up. */ |
| 5451 | if (p >= end || *p > 127) |
| 5452 | return -1; |
| 5453 | switch (*p) { |
| 5454 | case '\n': |
| 5455 | /* backslash + \n result in zero characters */ |
| 5456 | break; |
| 5457 | case '\\': case '\'': case '\"': |
| 5458 | case 'b': case 'f': case 't': |
| 5459 | case 'n': case 'r': case 'v': case 'a': |
| 5460 | ++length; |
| 5461 | break; |
| 5462 | case '0': case '1': case '2': case '3': |
| 5463 | case '4': case '5': case '6': case '7': |
| 5464 | case 'x': case 'u': case 'U': case 'N': |
| 5465 | /* these do not guarantee ASCII characters */ |
| 5466 | return -1; |
| 5467 | default: |
| 5468 | /* count the backslash + the other character */ |
| 5469 | length += 2; |
| 5470 | } |
| 5471 | } |
| 5472 | } |
| 5473 | return length; |
| 5474 | } |
| 5475 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5476 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5478 | PyObject * |
| 5479 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5480 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5481 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5483 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5484 | Py_ssize_t startinpos; |
| 5485 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5486 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5488 | char* message; |
| 5489 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5490 | PyObject *errorHandler = NULL; |
| 5491 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5492 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5493 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5494 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5495 | if (len == 0) |
| 5496 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5497 | |
| 5498 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5499 | either the string is pure ASCII with named escapes like \n, etc. |
| 5500 | and we determined it's exact size (common case) |
| 5501 | or it contains \x, \u, ... escape sequences. then we create a |
| 5502 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5503 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5504 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5505 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5506 | } |
| 5507 | else { |
| 5508 | /* Escaped strings will always be longer than the resulting |
| 5509 | Unicode string, so we start with size here and then reduce the |
| 5510 | length after conversion to the true value. |
| 5511 | (but if the error callback returns a long replacement string |
| 5512 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5513 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5514 | } |
| 5515 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5517 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | while (s < end) { |
| 5521 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5522 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5523 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | |
| 5525 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5526 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5527 | x = (unsigned char)*s; |
| 5528 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5529 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5530 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5531 | continue; |
| 5532 | } |
| 5533 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5534 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | /* \ - Escapes */ |
| 5536 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5537 | c = *s++; |
| 5538 | if (s > end) |
| 5539 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5540 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5541 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5543 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5544 | #define WRITECHAR(ch) \ |
| 5545 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5546 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5547 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5548 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5550 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5551 | case '\\': WRITECHAR('\\'); break; |
| 5552 | case '\'': WRITECHAR('\''); break; |
| 5553 | case '\"': WRITECHAR('\"'); break; |
| 5554 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5555 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5556 | case 'f': WRITECHAR('\014'); break; |
| 5557 | case 't': WRITECHAR('\t'); break; |
| 5558 | case 'n': WRITECHAR('\n'); break; |
| 5559 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5560 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5561 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5562 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5563 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5565 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | case '0': case '1': case '2': case '3': |
| 5567 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5568 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5569 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5570 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5571 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5572 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5574 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | break; |
| 5576 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5577 | /* hex escapes */ |
| 5578 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5580 | digits = 2; |
| 5581 | message = "truncated \\xXX escape"; |
| 5582 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5584 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5585 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5586 | digits = 4; |
| 5587 | message = "truncated \\uXXXX escape"; |
| 5588 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5590 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5591 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5592 | digits = 8; |
| 5593 | message = "truncated \\UXXXXXXXX escape"; |
| 5594 | hexescape: |
| 5595 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5596 | if (end - s < digits) { |
| 5597 | /* count only hex digits */ |
| 5598 | for (; s < end; ++s) { |
| 5599 | c = (unsigned char)*s; |
| 5600 | if (!Py_ISXDIGIT(c)) |
| 5601 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5602 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5603 | goto error; |
| 5604 | } |
| 5605 | for (; digits--; ++s) { |
| 5606 | c = (unsigned char)*s; |
| 5607 | if (!Py_ISXDIGIT(c)) |
| 5608 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5609 | chr = (chr<<4) & ~0xF; |
| 5610 | if (c >= '0' && c <= '9') |
| 5611 | chr += c - '0'; |
| 5612 | else if (c >= 'a' && c <= 'f') |
| 5613 | chr += 10 + c - 'a'; |
| 5614 | else |
| 5615 | chr += 10 + c - 'A'; |
| 5616 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5617 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5618 | /* _decoding_error will have already written into the |
| 5619 | target buffer. */ |
| 5620 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5621 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5622 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5623 | message = "illegal Unicode character"; |
| 5624 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5625 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5626 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5627 | break; |
| 5628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5629 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5630 | case 'N': |
| 5631 | message = "malformed \\N character escape"; |
| 5632 | if (ucnhash_CAPI == NULL) { |
| 5633 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5634 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5635 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5636 | if (ucnhash_CAPI == NULL) |
| 5637 | goto ucnhashError; |
| 5638 | } |
| 5639 | if (*s == '{') { |
| 5640 | const char *start = s+1; |
| 5641 | /* look for the closing brace */ |
| 5642 | while (*s != '}' && s < end) |
| 5643 | s++; |
| 5644 | if (s > start && s < end && *s == '}') { |
| 5645 | /* found a name. look it up in the unicode database */ |
| 5646 | message = "unknown Unicode character name"; |
| 5647 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5648 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5649 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5650 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5651 | goto store; |
| 5652 | } |
| 5653 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5654 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5655 | |
| 5656 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5657 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5658 | message = "\\ at end of string"; |
| 5659 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5660 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5661 | } |
| 5662 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5663 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5664 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5665 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5666 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5668 | continue; |
| 5669 | |
| 5670 | error: |
| 5671 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5672 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5673 | errors, &errorHandler, |
| 5674 | "unicodeescape", message, |
| 5675 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5676 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5677 | goto onError; |
| 5678 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5680 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5681 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5682 | Py_XDECREF(errorHandler); |
| 5683 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5684 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5686 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5687 | PyErr_SetString( |
| 5688 | PyExc_UnicodeError, |
| 5689 | "\\N escapes not supported (can't load unicodedata module)" |
| 5690 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5691 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5692 | Py_XDECREF(errorHandler); |
| 5693 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5694 | return NULL; |
| 5695 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5697 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5698 | Py_XDECREF(errorHandler); |
| 5699 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | return NULL; |
| 5701 | } |
| 5702 | |
| 5703 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5704 | |
| 5705 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5706 | appropriate. |
| 5707 | |
| 5708 | */ |
| 5709 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5710 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5711 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5713 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5714 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5715 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5716 | int kind; |
| 5717 | void *data; |
| 5718 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5720 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5721 | escape. |
| 5722 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5723 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5724 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5725 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5726 | */ |
| 5727 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5728 | if (!PyUnicode_Check(unicode)) { |
| 5729 | PyErr_BadArgument(); |
| 5730 | return NULL; |
| 5731 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5732 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5733 | return NULL; |
| 5734 | len = PyUnicode_GET_LENGTH(unicode); |
| 5735 | kind = PyUnicode_KIND(unicode); |
| 5736 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5737 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5738 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5739 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5740 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5741 | } |
| 5742 | |
| 5743 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5744 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5745 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5746 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5748 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5749 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5751 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5753 | if (repr == NULL) |
| 5754 | return NULL; |
| 5755 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5756 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5758 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5759 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5760 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5761 | /* Escape backslashes */ |
| 5762 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | *p++ = '\\'; |
| 5764 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5765 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5766 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5767 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5768 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5769 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5770 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5771 | *p++ = '\\'; |
| 5772 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5773 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5774 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5775 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5776 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5777 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5778 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5779 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5780 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5781 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5782 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5784 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5785 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | *p++ = '\\'; |
| 5787 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5788 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5789 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5790 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5791 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5793 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5794 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5795 | else if (ch == '\t') { |
| 5796 | *p++ = '\\'; |
| 5797 | *p++ = 't'; |
| 5798 | } |
| 5799 | else if (ch == '\n') { |
| 5800 | *p++ = '\\'; |
| 5801 | *p++ = 'n'; |
| 5802 | } |
| 5803 | else if (ch == '\r') { |
| 5804 | *p++ = '\\'; |
| 5805 | *p++ = 'r'; |
| 5806 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5807 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5808 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5809 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5811 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5812 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5813 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5814 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5816 | /* Copy everything else as-is */ |
| 5817 | else |
| 5818 | *p++ = (char) ch; |
| 5819 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5820 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5821 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5822 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5823 | return NULL; |
| 5824 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5827 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5828 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5829 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5831 | PyObject *result; |
| 5832 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5833 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5835 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5836 | Py_DECREF(tmp); |
| 5837 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5841 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5842 | PyObject * |
| 5843 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5844 | Py_ssize_t size, |
| 5845 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5847 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5848 | Py_ssize_t startinpos; |
| 5849 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5850 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | const char *end; |
| 5852 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | PyObject *errorHandler = NULL; |
| 5854 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5855 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5856 | if (size == 0) |
| 5857 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | /* Escaped strings will always be longer than the resulting |
| 5860 | 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] | 5861 | length after conversion to the true value. (But decoding error |
| 5862 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5863 | _PyUnicodeWriter_Init(&writer); |
| 5864 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | end = s + size; |
| 5867 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5868 | unsigned char c; |
| 5869 | Py_UCS4 x; |
| 5870 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5871 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5874 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5875 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5876 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5877 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5879 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | startinpos = s-starts; |
| 5881 | |
| 5882 | /* \u-escapes are only interpreted iff the number of leading |
| 5883 | backslashes if odd */ |
| 5884 | bs = s; |
| 5885 | for (;s < end;) { |
| 5886 | if (*s != '\\') |
| 5887 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5888 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5889 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5890 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5891 | } |
| 5892 | if (((s - bs) & 1) == 0 || |
| 5893 | s >= end || |
| 5894 | (*s != 'u' && *s != 'U')) { |
| 5895 | continue; |
| 5896 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5897 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5898 | count = *s=='u' ? 4 : 8; |
| 5899 | s++; |
| 5900 | |
| 5901 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5902 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5903 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5904 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5906 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5907 | errors, &errorHandler, |
| 5908 | "rawunicodeescape", "truncated \\uXXXX", |
| 5909 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5910 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5911 | goto onError; |
| 5912 | goto nextByte; |
| 5913 | } |
| 5914 | x = (x<<4) & ~0xF; |
| 5915 | if (c >= '0' && c <= '9') |
| 5916 | x += c - '0'; |
| 5917 | else if (c >= 'a' && c <= 'f') |
| 5918 | x += 10 + c - 'a'; |
| 5919 | else |
| 5920 | x += 10 + c - 'A'; |
| 5921 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5922 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5923 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5924 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5925 | } |
| 5926 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5927 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5928 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5929 | errors, &errorHandler, |
| 5930 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5932 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5934 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | nextByte: |
| 5936 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5938 | Py_XDECREF(errorHandler); |
| 5939 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5940 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5941 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5943 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5944 | Py_XDECREF(errorHandler); |
| 5945 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | return NULL; |
| 5947 | } |
| 5948 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5950 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5951 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5953 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | char *p; |
| 5955 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5956 | Py_ssize_t expandsize, pos; |
| 5957 | int kind; |
| 5958 | void *data; |
| 5959 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5961 | if (!PyUnicode_Check(unicode)) { |
| 5962 | PyErr_BadArgument(); |
| 5963 | return NULL; |
| 5964 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5965 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5966 | return NULL; |
| 5967 | kind = PyUnicode_KIND(unicode); |
| 5968 | data = PyUnicode_DATA(unicode); |
| 5969 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5970 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5971 | bytes, and 1 byte characters 4. */ |
| 5972 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5973 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5974 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5975 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5976 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5977 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | if (repr == NULL) |
| 5979 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5980 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5981 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5983 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5984 | for (pos = 0; pos < len; pos++) { |
| 5985 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5987 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5988 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5989 | *p++ = '\\'; |
| 5990 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5991 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5992 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5993 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5994 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5995 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5996 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5997 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5998 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5999 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6000 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6001 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 | *p++ = '\\'; |
| 6003 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6004 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6005 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6006 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6007 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | /* Copy everything else as-is */ |
| 6010 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | *p++ = (char) ch; |
| 6012 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6013 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6014 | assert(p > q); |
| 6015 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6016 | return NULL; |
| 6017 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6020 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6021 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6022 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6024 | PyObject *result; |
| 6025 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6026 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6027 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6028 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6029 | Py_DECREF(tmp); |
| 6030 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6033 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6034 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6035 | PyObject * |
| 6036 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6037 | Py_ssize_t size, |
| 6038 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6039 | { |
| 6040 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6041 | Py_ssize_t startinpos; |
| 6042 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6043 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6044 | const char *end; |
| 6045 | const char *reason; |
| 6046 | PyObject *errorHandler = NULL; |
| 6047 | PyObject *exc = NULL; |
| 6048 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6049 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6050 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6051 | 1)) |
| 6052 | return NULL; |
| 6053 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6054 | if (size == 0) |
| 6055 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6056 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6057 | _PyUnicodeWriter_Init(&writer); |
| 6058 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6059 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6061 | } |
| 6062 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6063 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6064 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6065 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6066 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6067 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6068 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6069 | endinpos = end-starts; |
| 6070 | reason = "truncated input"; |
| 6071 | goto error; |
| 6072 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6073 | /* We copy the raw representation one byte at a time because the |
| 6074 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6075 | ((char *) &uch)[0] = s[0]; |
| 6076 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6077 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6078 | ((char *) &uch)[2] = s[2]; |
| 6079 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6080 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6081 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6082 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6083 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6084 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6085 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6086 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6087 | reason = "illegal code point (> 0x10FFFF)"; |
| 6088 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6089 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6090 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6091 | s += Py_UNICODE_SIZE; |
| 6092 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6093 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6094 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6095 | Py_UNICODE uch2; |
| 6096 | ((char *) &uch2)[0] = s[0]; |
| 6097 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6098 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6099 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6100 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6101 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6102 | } |
| 6103 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6104 | #endif |
| 6105 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6106 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6107 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6108 | continue; |
| 6109 | |
| 6110 | error: |
| 6111 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6112 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6113 | errors, &errorHandler, |
| 6114 | "unicode_internal", reason, |
| 6115 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6116 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6117 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6120 | Py_XDECREF(errorHandler); |
| 6121 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6122 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6123 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6124 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6125 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6126 | Py_XDECREF(errorHandler); |
| 6127 | Py_XDECREF(exc); |
| 6128 | return NULL; |
| 6129 | } |
| 6130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6132 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6133 | PyObject * |
| 6134 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6135 | Py_ssize_t size, |
| 6136 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6139 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6142 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6143 | static void |
| 6144 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6145 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6146 | PyObject *unicode, |
| 6147 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6148 | const char *reason) |
| 6149 | { |
| 6150 | if (*exceptionObject == NULL) { |
| 6151 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6152 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6153 | encoding, unicode, startpos, endpos, reason); |
| 6154 | } |
| 6155 | else { |
| 6156 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6157 | goto onError; |
| 6158 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6159 | goto onError; |
| 6160 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6161 | goto onError; |
| 6162 | return; |
| 6163 | onError: |
| 6164 | Py_DECREF(*exceptionObject); |
| 6165 | *exceptionObject = NULL; |
| 6166 | } |
| 6167 | } |
| 6168 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6169 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6170 | static void |
| 6171 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6172 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6173 | PyObject *unicode, |
| 6174 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6175 | const char *reason) |
| 6176 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6177 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6178 | encoding, unicode, startpos, endpos, reason); |
| 6179 | if (*exceptionObject != NULL) |
| 6180 | PyCodec_StrictErrors(*exceptionObject); |
| 6181 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6182 | |
| 6183 | /* error handling callback helper: |
| 6184 | build arguments, call the callback and check the arguments, |
| 6185 | put the result into newpos and return the replacement string, which |
| 6186 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6187 | static PyObject * |
| 6188 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6189 | PyObject **errorHandler, |
| 6190 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6191 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6192 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6193 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6194 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6195 | 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] | 6196 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6197 | PyObject *restuple; |
| 6198 | PyObject *resunicode; |
| 6199 | |
| 6200 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6202 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6203 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6206 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6207 | return NULL; |
| 6208 | len = PyUnicode_GET_LENGTH(unicode); |
| 6209 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6210 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6211 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6212 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6213 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6214 | |
| 6215 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6217 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6218 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6219 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6220 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 | Py_DECREF(restuple); |
| 6222 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6223 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6224 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | &resunicode, newpos)) { |
| 6226 | Py_DECREF(restuple); |
| 6227 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6228 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6229 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6230 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6231 | Py_DECREF(restuple); |
| 6232 | return NULL; |
| 6233 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6234 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6235 | *newpos = len + *newpos; |
| 6236 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6237 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6238 | Py_DECREF(restuple); |
| 6239 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6240 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6241 | Py_INCREF(resunicode); |
| 6242 | Py_DECREF(restuple); |
| 6243 | return resunicode; |
| 6244 | } |
| 6245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6246 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6247 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6248 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6249 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6250 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6251 | /* input state */ |
| 6252 | Py_ssize_t pos=0, size; |
| 6253 | int kind; |
| 6254 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6255 | /* output object */ |
| 6256 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6257 | /* pointer into the output */ |
| 6258 | char *str; |
| 6259 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6260 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6261 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6262 | 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] | 6263 | PyObject *errorHandler = NULL; |
| 6264 | PyObject *exc = NULL; |
| 6265 | /* the following variable is used for caching string comparisons |
| 6266 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6267 | int known_errorHandler = -1; |
| 6268 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6269 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6270 | return NULL; |
| 6271 | size = PyUnicode_GET_LENGTH(unicode); |
| 6272 | kind = PyUnicode_KIND(unicode); |
| 6273 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6274 | /* allocate enough for a simple encoding without |
| 6275 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6276 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6277 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6278 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6279 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6280 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6281 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6282 | ressize = size; |
| 6283 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6284 | while (pos < size) { |
| 6285 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6286 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | /* can we encode this? */ |
| 6288 | if (c<limit) { |
| 6289 | /* no overflow check, because we know that the space is enough */ |
| 6290 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6291 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6292 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6294 | Py_ssize_t requiredsize; |
| 6295 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6296 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6298 | Py_ssize_t collstart = pos; |
| 6299 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6301 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6302 | ++collend; |
| 6303 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6304 | if (known_errorHandler==-1) { |
| 6305 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6306 | known_errorHandler = 1; |
| 6307 | else if (!strcmp(errors, "replace")) |
| 6308 | known_errorHandler = 2; |
| 6309 | else if (!strcmp(errors, "ignore")) |
| 6310 | known_errorHandler = 3; |
| 6311 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6312 | known_errorHandler = 4; |
| 6313 | else |
| 6314 | known_errorHandler = 0; |
| 6315 | } |
| 6316 | switch (known_errorHandler) { |
| 6317 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6318 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | goto onError; |
| 6320 | case 2: /* replace */ |
| 6321 | while (collstart++<collend) |
| 6322 | *str++ = '?'; /* fall through */ |
| 6323 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6324 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | break; |
| 6326 | case 4: /* xmlcharrefreplace */ |
| 6327 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6328 | /* determine replacement size */ |
| 6329 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6330 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6331 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6332 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6333 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6334 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6335 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6336 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6337 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6339 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6341 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6343 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6344 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6346 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6347 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6348 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | if (requiredsize > ressize) { |
| 6350 | if (requiredsize<2*ressize) |
| 6351 | requiredsize = 2*ressize; |
| 6352 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6353 | goto onError; |
| 6354 | str = PyBytes_AS_STRING(res) + respos; |
| 6355 | ressize = requiredsize; |
| 6356 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6357 | /* generate replacement */ |
| 6358 | for (i = collstart; i < collend; ++i) { |
| 6359 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6361 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6362 | break; |
| 6363 | default: |
| 6364 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6365 | encoding, reason, unicode, &exc, |
| 6366 | collstart, collend, &newpos); |
| 6367 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6368 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6370 | if (PyBytes_Check(repunicode)) { |
| 6371 | /* Directly copy bytes result to output. */ |
| 6372 | repsize = PyBytes_Size(repunicode); |
| 6373 | if (repsize > 1) { |
| 6374 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6375 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6376 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6377 | Py_DECREF(repunicode); |
| 6378 | goto onError; |
| 6379 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6380 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6381 | ressize += repsize-1; |
| 6382 | } |
| 6383 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6384 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6385 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6386 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6387 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6388 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6389 | /* need more space? (at least enough for what we |
| 6390 | have+the replacement+the rest of the string, so |
| 6391 | we won't have to check space for encodable characters) */ |
| 6392 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6393 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6394 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6395 | if (requiredsize > ressize) { |
| 6396 | if (requiredsize<2*ressize) |
| 6397 | requiredsize = 2*ressize; |
| 6398 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6399 | Py_DECREF(repunicode); |
| 6400 | goto onError; |
| 6401 | } |
| 6402 | str = PyBytes_AS_STRING(res) + respos; |
| 6403 | ressize = requiredsize; |
| 6404 | } |
| 6405 | /* check if there is anything unencodable in the replacement |
| 6406 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6407 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6408 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6410 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6411 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | Py_DECREF(repunicode); |
| 6413 | goto onError; |
| 6414 | } |
| 6415 | *str = (char)c; |
| 6416 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6417 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6418 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6419 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6420 | } |
| 6421 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6422 | /* Resize if we allocated to much */ |
| 6423 | size = str - PyBytes_AS_STRING(res); |
| 6424 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6425 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6426 | if (_PyBytes_Resize(&res, size) < 0) |
| 6427 | goto onError; |
| 6428 | } |
| 6429 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | Py_XDECREF(errorHandler); |
| 6431 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6432 | return res; |
| 6433 | |
| 6434 | onError: |
| 6435 | Py_XDECREF(res); |
| 6436 | Py_XDECREF(errorHandler); |
| 6437 | Py_XDECREF(exc); |
| 6438 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6441 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6442 | PyObject * |
| 6443 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6444 | Py_ssize_t size, |
| 6445 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6447 | PyObject *result; |
| 6448 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6449 | if (unicode == NULL) |
| 6450 | return NULL; |
| 6451 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6452 | Py_DECREF(unicode); |
| 6453 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6454 | } |
| 6455 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6456 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6457 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | { |
| 6459 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | PyErr_BadArgument(); |
| 6461 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6463 | if (PyUnicode_READY(unicode) == -1) |
| 6464 | return NULL; |
| 6465 | /* Fast path: if it is a one-byte string, construct |
| 6466 | bytes object directly. */ |
| 6467 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6468 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6469 | PyUnicode_GET_LENGTH(unicode)); |
| 6470 | /* Non-Latin-1 characters present. Defer to above function to |
| 6471 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6472 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6473 | } |
| 6474 | |
| 6475 | PyObject* |
| 6476 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6477 | { |
| 6478 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | } |
| 6480 | |
| 6481 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6482 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6483 | PyObject * |
| 6484 | PyUnicode_DecodeASCII(const char *s, |
| 6485 | Py_ssize_t size, |
| 6486 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6488 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6489 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6490 | int kind; |
| 6491 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6492 | Py_ssize_t startinpos; |
| 6493 | Py_ssize_t endinpos; |
| 6494 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6495 | const char *e; |
| 6496 | PyObject *errorHandler = NULL; |
| 6497 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6500 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6503 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6504 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6505 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6506 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6507 | writer.min_length = size; |
| 6508 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6509 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6510 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6511 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6512 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6513 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6514 | writer.pos = outpos; |
| 6515 | if (writer.pos == size) |
| 6516 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6517 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6518 | s += writer.pos; |
| 6519 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6520 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6521 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6523 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6524 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | ++s; |
| 6526 | } |
| 6527 | else { |
| 6528 | startinpos = s-starts; |
| 6529 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6530 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6531 | errors, &errorHandler, |
| 6532 | "ascii", "ordinal not in range(128)", |
| 6533 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6534 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6536 | kind = writer.kind; |
| 6537 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6540 | Py_XDECREF(errorHandler); |
| 6541 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6542 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6545 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6546 | Py_XDECREF(errorHandler); |
| 6547 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | return NULL; |
| 6549 | } |
| 6550 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6551 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6552 | PyObject * |
| 6553 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6554 | Py_ssize_t size, |
| 6555 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6557 | PyObject *result; |
| 6558 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6559 | if (unicode == NULL) |
| 6560 | return NULL; |
| 6561 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6562 | Py_DECREF(unicode); |
| 6563 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6566 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6567 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6568 | { |
| 6569 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | PyErr_BadArgument(); |
| 6571 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6573 | if (PyUnicode_READY(unicode) == -1) |
| 6574 | return NULL; |
| 6575 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6576 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6577 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6578 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6579 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6580 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | PyObject * |
| 6584 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6585 | { |
| 6586 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | } |
| 6588 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6589 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6590 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6591 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6592 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6593 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6594 | #define NEED_RETRY |
| 6595 | #endif |
| 6596 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6597 | #ifndef WC_ERR_INVALID_CHARS |
| 6598 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6599 | #endif |
| 6600 | |
| 6601 | static char* |
| 6602 | code_page_name(UINT code_page, PyObject **obj) |
| 6603 | { |
| 6604 | *obj = NULL; |
| 6605 | if (code_page == CP_ACP) |
| 6606 | return "mbcs"; |
| 6607 | if (code_page == CP_UTF7) |
| 6608 | return "CP_UTF7"; |
| 6609 | if (code_page == CP_UTF8) |
| 6610 | return "CP_UTF8"; |
| 6611 | |
| 6612 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6613 | if (*obj == NULL) |
| 6614 | return NULL; |
| 6615 | return PyBytes_AS_STRING(*obj); |
| 6616 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6618 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6619 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6620 | { |
| 6621 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6622 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6623 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6624 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6625 | return 0; |
| 6626 | |
| 6627 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6628 | if (prev == curr) |
| 6629 | return 1; |
| 6630 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6631 | as it assumes an incomplete character consists of a single |
| 6632 | byte. */ |
| 6633 | if (curr - prev == 2) |
| 6634 | return 1; |
| 6635 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6636 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6637 | return 0; |
| 6638 | } |
| 6639 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6640 | static DWORD |
| 6641 | decode_code_page_flags(UINT code_page) |
| 6642 | { |
| 6643 | if (code_page == CP_UTF7) { |
| 6644 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6645 | return 0; |
| 6646 | } |
| 6647 | else |
| 6648 | return MB_ERR_INVALID_CHARS; |
| 6649 | } |
| 6650 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6651 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6652 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6653 | * mode. |
| 6654 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6655 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6656 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6657 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6658 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6659 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6660 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6661 | const char *in, |
| 6662 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6663 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6664 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6665 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6666 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6667 | |
| 6668 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6669 | assert(insize > 0); |
| 6670 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6671 | if (outsize <= 0) |
| 6672 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6673 | |
| 6674 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6675 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6676 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6677 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | if (*v == NULL) |
| 6679 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6680 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6681 | } |
| 6682 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6684 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6685 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6686 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6687 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6691 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6692 | if (outsize <= 0) |
| 6693 | goto error; |
| 6694 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6695 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6696 | error: |
| 6697 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6698 | return -2; |
| 6699 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6700 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6701 | } |
| 6702 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6703 | /* |
| 6704 | * Decode a byte string from a code page into unicode object with an error |
| 6705 | * handler. |
| 6706 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6707 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6708 | * UnicodeDecodeError exception and returns -1 on error. |
| 6709 | */ |
| 6710 | static int |
| 6711 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6712 | PyObject **v, |
| 6713 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6714 | const char *errors) |
| 6715 | { |
| 6716 | const char *startin = in; |
| 6717 | const char *endin = in + size; |
| 6718 | const DWORD flags = decode_code_page_flags(code_page); |
| 6719 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6720 | 2000 English version of the message. */ |
| 6721 | const char *reason = "No mapping for the Unicode character exists " |
| 6722 | "in the target code page."; |
| 6723 | /* each step cannot decode more than 1 character, but a character can be |
| 6724 | represented as a surrogate pair */ |
| 6725 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6726 | int insize; |
| 6727 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6728 | PyObject *errorHandler = NULL; |
| 6729 | PyObject *exc = NULL; |
| 6730 | PyObject *encoding_obj = NULL; |
| 6731 | char *encoding; |
| 6732 | DWORD err; |
| 6733 | int ret = -1; |
| 6734 | |
| 6735 | assert(size > 0); |
| 6736 | |
| 6737 | encoding = code_page_name(code_page, &encoding_obj); |
| 6738 | if (encoding == NULL) |
| 6739 | return -1; |
| 6740 | |
| 6741 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6742 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6743 | UnicodeDecodeError. */ |
| 6744 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6745 | if (exc != NULL) { |
| 6746 | PyCodec_StrictErrors(exc); |
| 6747 | Py_CLEAR(exc); |
| 6748 | } |
| 6749 | goto error; |
| 6750 | } |
| 6751 | |
| 6752 | if (*v == NULL) { |
| 6753 | /* Create unicode object */ |
| 6754 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6755 | PyErr_NoMemory(); |
| 6756 | goto error; |
| 6757 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6758 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6759 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6760 | if (*v == NULL) |
| 6761 | goto error; |
| 6762 | startout = PyUnicode_AS_UNICODE(*v); |
| 6763 | } |
| 6764 | else { |
| 6765 | /* Extend unicode object */ |
| 6766 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6767 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6768 | PyErr_NoMemory(); |
| 6769 | goto error; |
| 6770 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6771 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6772 | goto error; |
| 6773 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6774 | } |
| 6775 | |
| 6776 | /* Decode the byte string character per character */ |
| 6777 | out = startout; |
| 6778 | while (in < endin) |
| 6779 | { |
| 6780 | /* Decode a character */ |
| 6781 | insize = 1; |
| 6782 | do |
| 6783 | { |
| 6784 | outsize = MultiByteToWideChar(code_page, flags, |
| 6785 | in, insize, |
| 6786 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6787 | if (outsize > 0) |
| 6788 | break; |
| 6789 | err = GetLastError(); |
| 6790 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6791 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6792 | { |
| 6793 | PyErr_SetFromWindowsErr(0); |
| 6794 | goto error; |
| 6795 | } |
| 6796 | insize++; |
| 6797 | } |
| 6798 | /* 4=maximum length of a UTF-8 sequence */ |
| 6799 | while (insize <= 4 && (in + insize) <= endin); |
| 6800 | |
| 6801 | if (outsize <= 0) { |
| 6802 | Py_ssize_t startinpos, endinpos, outpos; |
| 6803 | |
| 6804 | startinpos = in - startin; |
| 6805 | endinpos = startinpos + 1; |
| 6806 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6807 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6808 | errors, &errorHandler, |
| 6809 | encoding, reason, |
| 6810 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6811 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6812 | { |
| 6813 | goto error; |
| 6814 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6815 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6816 | } |
| 6817 | else { |
| 6818 | in += insize; |
| 6819 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6820 | out += outsize; |
| 6821 | } |
| 6822 | } |
| 6823 | |
| 6824 | /* write a NUL character at the end */ |
| 6825 | *out = 0; |
| 6826 | |
| 6827 | /* Extend unicode object */ |
| 6828 | outsize = out - startout; |
| 6829 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6830 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6831 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6832 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6833 | |
| 6834 | error: |
| 6835 | Py_XDECREF(encoding_obj); |
| 6836 | Py_XDECREF(errorHandler); |
| 6837 | Py_XDECREF(exc); |
| 6838 | return ret; |
| 6839 | } |
| 6840 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6841 | static PyObject * |
| 6842 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6843 | const char *s, Py_ssize_t size, |
| 6844 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6845 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6846 | PyObject *v = NULL; |
| 6847 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6849 | if (code_page < 0) { |
| 6850 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6851 | return NULL; |
| 6852 | } |
| 6853 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6855 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6856 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6857 | do |
| 6858 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6859 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6860 | if (size > INT_MAX) { |
| 6861 | chunk_size = INT_MAX; |
| 6862 | final = 0; |
| 6863 | done = 0; |
| 6864 | } |
| 6865 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6866 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6867 | { |
| 6868 | chunk_size = (int)size; |
| 6869 | final = (consumed == NULL); |
| 6870 | done = 1; |
| 6871 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6872 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6873 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6874 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6875 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6876 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6877 | if (chunk_size == 0 && done) { |
| 6878 | if (v != NULL) |
| 6879 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6880 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6881 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6882 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6883 | |
| 6884 | converted = decode_code_page_strict(code_page, &v, |
| 6885 | s, chunk_size); |
| 6886 | if (converted == -2) |
| 6887 | converted = decode_code_page_errors(code_page, &v, |
| 6888 | s, chunk_size, |
| 6889 | errors); |
| 6890 | assert(converted != 0); |
| 6891 | |
| 6892 | if (converted < 0) { |
| 6893 | Py_XDECREF(v); |
| 6894 | return NULL; |
| 6895 | } |
| 6896 | |
| 6897 | if (consumed) |
| 6898 | *consumed += converted; |
| 6899 | |
| 6900 | s += converted; |
| 6901 | size -= converted; |
| 6902 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6903 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6904 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6907 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6909 | const char *s, |
| 6910 | Py_ssize_t size, |
| 6911 | const char *errors, |
| 6912 | Py_ssize_t *consumed) |
| 6913 | { |
| 6914 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6915 | } |
| 6916 | |
| 6917 | PyObject * |
| 6918 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6919 | Py_ssize_t size, |
| 6920 | const char *errors, |
| 6921 | Py_ssize_t *consumed) |
| 6922 | { |
| 6923 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6924 | } |
| 6925 | |
| 6926 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6927 | PyUnicode_DecodeMBCS(const char *s, |
| 6928 | Py_ssize_t size, |
| 6929 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6930 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6931 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6932 | } |
| 6933 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6934 | static DWORD |
| 6935 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6936 | { |
| 6937 | if (code_page == CP_UTF8) { |
| 6938 | if (winver.dwMajorVersion >= 6) |
| 6939 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6940 | and later */ |
| 6941 | return WC_ERR_INVALID_CHARS; |
| 6942 | else |
| 6943 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6944 | return 0; |
| 6945 | } |
| 6946 | else if (code_page == CP_UTF7) { |
| 6947 | /* CP_UTF7 only supports flags=0 */ |
| 6948 | return 0; |
| 6949 | } |
| 6950 | else { |
| 6951 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6952 | return 0; |
| 6953 | else |
| 6954 | return WC_NO_BEST_FIT_CHARS; |
| 6955 | } |
| 6956 | } |
| 6957 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6958 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6959 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6960 | * mode. |
| 6961 | * |
| 6962 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6963 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6964 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6965 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6966 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6967 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6968 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6970 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6972 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6973 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6974 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6975 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6976 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6977 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6978 | /* Create a substring so that we can get the UTF-16 representation |
| 6979 | of just the slice under consideration. */ |
| 6980 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6981 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6982 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6983 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6985 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6986 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6987 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6988 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6989 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6990 | if (substring == NULL) |
| 6991 | return -1; |
| 6992 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6993 | if (p == NULL) { |
| 6994 | Py_DECREF(substring); |
| 6995 | return -1; |
| 6996 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6997 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6998 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6999 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7001 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7002 | NULL, 0, |
| 7003 | NULL, pusedDefaultChar); |
| 7004 | if (outsize <= 0) |
| 7005 | goto error; |
| 7006 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7007 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7008 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7009 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7010 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7011 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7012 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7013 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7014 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7015 | if (*outbytes == NULL) { |
| 7016 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7018 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7019 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | } |
| 7021 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7022 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7023 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7024 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7025 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7026 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7027 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7028 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7029 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7030 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7031 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7032 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7033 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7038 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7039 | out, outsize, |
| 7040 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7041 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | if (outsize <= 0) |
| 7043 | goto error; |
| 7044 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7045 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7046 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7047 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7049 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7050 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7051 | return -2; |
| 7052 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7053 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7054 | } |
| 7055 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7056 | /* |
| 7057 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7058 | * error handler. |
| 7059 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7060 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7061 | * -1 on other error. |
| 7062 | */ |
| 7063 | static int |
| 7064 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7065 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7066 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7067 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7068 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7069 | Py_ssize_t pos = unicode_offset; |
| 7070 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7072 | 2000 English version of the message. */ |
| 7073 | const char *reason = "invalid character"; |
| 7074 | /* 4=maximum length of a UTF-8 sequence */ |
| 7075 | char buffer[4]; |
| 7076 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7077 | Py_ssize_t outsize; |
| 7078 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7079 | PyObject *errorHandler = NULL; |
| 7080 | PyObject *exc = NULL; |
| 7081 | PyObject *encoding_obj = NULL; |
| 7082 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7083 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7084 | PyObject *rep; |
| 7085 | int ret = -1; |
| 7086 | |
| 7087 | assert(insize > 0); |
| 7088 | |
| 7089 | encoding = code_page_name(code_page, &encoding_obj); |
| 7090 | if (encoding == NULL) |
| 7091 | return -1; |
| 7092 | |
| 7093 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7094 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7095 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7096 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7097 | if (exc != NULL) { |
| 7098 | PyCodec_StrictErrors(exc); |
| 7099 | Py_DECREF(exc); |
| 7100 | } |
| 7101 | Py_XDECREF(encoding_obj); |
| 7102 | return -1; |
| 7103 | } |
| 7104 | |
| 7105 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7106 | pusedDefaultChar = &usedDefaultChar; |
| 7107 | else |
| 7108 | pusedDefaultChar = NULL; |
| 7109 | |
| 7110 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7111 | PyErr_NoMemory(); |
| 7112 | goto error; |
| 7113 | } |
| 7114 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7115 | |
| 7116 | if (*outbytes == NULL) { |
| 7117 | /* Create string object */ |
| 7118 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7119 | if (*outbytes == NULL) |
| 7120 | goto error; |
| 7121 | out = PyBytes_AS_STRING(*outbytes); |
| 7122 | } |
| 7123 | else { |
| 7124 | /* Extend string object */ |
| 7125 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7126 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7127 | PyErr_NoMemory(); |
| 7128 | goto error; |
| 7129 | } |
| 7130 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7131 | goto error; |
| 7132 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7133 | } |
| 7134 | |
| 7135 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7136 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7137 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7138 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7139 | wchar_t chars[2]; |
| 7140 | int charsize; |
| 7141 | if (ch < 0x10000) { |
| 7142 | chars[0] = (wchar_t)ch; |
| 7143 | charsize = 1; |
| 7144 | } |
| 7145 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7146 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7147 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7148 | charsize = 2; |
| 7149 | } |
| 7150 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7151 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7152 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7153 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7154 | NULL, pusedDefaultChar); |
| 7155 | if (outsize > 0) { |
| 7156 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7157 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7158 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7159 | memcpy(out, buffer, outsize); |
| 7160 | out += outsize; |
| 7161 | continue; |
| 7162 | } |
| 7163 | } |
| 7164 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7165 | PyErr_SetFromWindowsErr(0); |
| 7166 | goto error; |
| 7167 | } |
| 7168 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7169 | rep = unicode_encode_call_errorhandler( |
| 7170 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7171 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7172 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7173 | if (rep == NULL) |
| 7174 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7175 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | |
| 7177 | if (PyBytes_Check(rep)) { |
| 7178 | outsize = PyBytes_GET_SIZE(rep); |
| 7179 | if (outsize != 1) { |
| 7180 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7181 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7182 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7183 | Py_DECREF(rep); |
| 7184 | goto error; |
| 7185 | } |
| 7186 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7187 | } |
| 7188 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7189 | out += outsize; |
| 7190 | } |
| 7191 | else { |
| 7192 | Py_ssize_t i; |
| 7193 | enum PyUnicode_Kind kind; |
| 7194 | void *data; |
| 7195 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7196 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | Py_DECREF(rep); |
| 7198 | goto error; |
| 7199 | } |
| 7200 | |
| 7201 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7202 | if (outsize != 1) { |
| 7203 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7204 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7205 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7206 | Py_DECREF(rep); |
| 7207 | goto error; |
| 7208 | } |
| 7209 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7210 | } |
| 7211 | kind = PyUnicode_KIND(rep); |
| 7212 | data = PyUnicode_DATA(rep); |
| 7213 | for (i=0; i < outsize; i++) { |
| 7214 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7215 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7216 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7217 | encoding, unicode, |
| 7218 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7219 | "unable to encode error handler result to ASCII"); |
| 7220 | Py_DECREF(rep); |
| 7221 | goto error; |
| 7222 | } |
| 7223 | *out = (unsigned char)ch; |
| 7224 | out++; |
| 7225 | } |
| 7226 | } |
| 7227 | Py_DECREF(rep); |
| 7228 | } |
| 7229 | /* write a NUL byte */ |
| 7230 | *out = 0; |
| 7231 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7232 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7233 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7234 | goto error; |
| 7235 | ret = 0; |
| 7236 | |
| 7237 | error: |
| 7238 | Py_XDECREF(encoding_obj); |
| 7239 | Py_XDECREF(errorHandler); |
| 7240 | Py_XDECREF(exc); |
| 7241 | return ret; |
| 7242 | } |
| 7243 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7244 | static PyObject * |
| 7245 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7246 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | const char *errors) |
| 7248 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7249 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7251 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7252 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7253 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7254 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7255 | return NULL; |
| 7256 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7257 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7258 | if (code_page < 0) { |
| 7259 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7260 | return NULL; |
| 7261 | } |
| 7262 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7263 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7264 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7265 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7266 | offset = 0; |
| 7267 | do |
| 7268 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7269 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7270 | /* 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] | 7271 | chunks. */ |
| 7272 | if (len > INT_MAX/2) { |
| 7273 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7274 | done = 0; |
| 7275 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7276 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7277 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7278 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7279 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7280 | done = 1; |
| 7281 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7282 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7283 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7284 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7285 | errors); |
| 7286 | if (ret == -2) |
| 7287 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7288 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7289 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7290 | if (ret < 0) { |
| 7291 | Py_XDECREF(outbytes); |
| 7292 | return NULL; |
| 7293 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7294 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7295 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7296 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7297 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7298 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | return outbytes; |
| 7300 | } |
| 7301 | |
| 7302 | PyObject * |
| 7303 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7304 | Py_ssize_t size, |
| 7305 | const char *errors) |
| 7306 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7307 | PyObject *unicode, *res; |
| 7308 | unicode = PyUnicode_FromUnicode(p, size); |
| 7309 | if (unicode == NULL) |
| 7310 | return NULL; |
| 7311 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7312 | Py_DECREF(unicode); |
| 7313 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7314 | } |
| 7315 | |
| 7316 | PyObject * |
| 7317 | PyUnicode_EncodeCodePage(int code_page, |
| 7318 | PyObject *unicode, |
| 7319 | const char *errors) |
| 7320 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7321 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7322 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7323 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7324 | PyObject * |
| 7325 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7326 | { |
| 7327 | if (!PyUnicode_Check(unicode)) { |
| 7328 | PyErr_BadArgument(); |
| 7329 | return NULL; |
| 7330 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7331 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7334 | #undef NEED_RETRY |
| 7335 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7336 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7339 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7340 | static int |
| 7341 | charmap_decode_string(const char *s, |
| 7342 | Py_ssize_t size, |
| 7343 | PyObject *mapping, |
| 7344 | const char *errors, |
| 7345 | _PyUnicodeWriter *writer) |
| 7346 | { |
| 7347 | const char *starts = s; |
| 7348 | const char *e; |
| 7349 | Py_ssize_t startinpos, endinpos; |
| 7350 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7351 | Py_ssize_t maplen; |
| 7352 | enum PyUnicode_Kind mapkind; |
| 7353 | void *mapdata; |
| 7354 | Py_UCS4 x; |
| 7355 | unsigned char ch; |
| 7356 | |
| 7357 | if (PyUnicode_READY(mapping) == -1) |
| 7358 | return -1; |
| 7359 | |
| 7360 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7361 | mapdata = PyUnicode_DATA(mapping); |
| 7362 | mapkind = PyUnicode_KIND(mapping); |
| 7363 | |
| 7364 | e = s + size; |
| 7365 | |
| 7366 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7367 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7368 | * is disabled in encoding aliases, latin1 is preferred because |
| 7369 | * its implementation is faster. */ |
| 7370 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7371 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7372 | Py_UCS4 maxchar = writer->maxchar; |
| 7373 | |
| 7374 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7375 | while (s < e) { |
| 7376 | ch = *s; |
| 7377 | x = mapdata_ucs1[ch]; |
| 7378 | if (x > maxchar) { |
| 7379 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7380 | goto onError; |
| 7381 | maxchar = writer->maxchar; |
| 7382 | outdata = (Py_UCS1 *)writer->data; |
| 7383 | } |
| 7384 | outdata[writer->pos] = x; |
| 7385 | writer->pos++; |
| 7386 | ++s; |
| 7387 | } |
| 7388 | return 0; |
| 7389 | } |
| 7390 | |
| 7391 | while (s < e) { |
| 7392 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7393 | enum PyUnicode_Kind outkind = writer->kind; |
| 7394 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7395 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7396 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7397 | Py_UCS4 maxchar = writer->maxchar; |
| 7398 | while (s < e) { |
| 7399 | ch = *s; |
| 7400 | x = mapdata_ucs2[ch]; |
| 7401 | if (x > maxchar) |
| 7402 | goto Error; |
| 7403 | outdata[writer->pos] = x; |
| 7404 | writer->pos++; |
| 7405 | ++s; |
| 7406 | } |
| 7407 | break; |
| 7408 | } |
| 7409 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7410 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7411 | while (s < e) { |
| 7412 | ch = *s; |
| 7413 | x = mapdata_ucs2[ch]; |
| 7414 | if (x == 0xFFFE) |
| 7415 | goto Error; |
| 7416 | outdata[writer->pos] = x; |
| 7417 | writer->pos++; |
| 7418 | ++s; |
| 7419 | } |
| 7420 | break; |
| 7421 | } |
| 7422 | } |
| 7423 | ch = *s; |
| 7424 | |
| 7425 | if (ch < maplen) |
| 7426 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7427 | else |
| 7428 | x = 0xfffe; /* invalid value */ |
| 7429 | Error: |
| 7430 | if (x == 0xfffe) |
| 7431 | { |
| 7432 | /* undefined mapping */ |
| 7433 | startinpos = s-starts; |
| 7434 | endinpos = startinpos+1; |
| 7435 | if (unicode_decode_call_errorhandler_writer( |
| 7436 | errors, &errorHandler, |
| 7437 | "charmap", "character maps to <undefined>", |
| 7438 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7439 | writer)) { |
| 7440 | goto onError; |
| 7441 | } |
| 7442 | continue; |
| 7443 | } |
| 7444 | |
| 7445 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7446 | goto onError; |
| 7447 | ++s; |
| 7448 | } |
| 7449 | Py_XDECREF(errorHandler); |
| 7450 | Py_XDECREF(exc); |
| 7451 | return 0; |
| 7452 | |
| 7453 | onError: |
| 7454 | Py_XDECREF(errorHandler); |
| 7455 | Py_XDECREF(exc); |
| 7456 | return -1; |
| 7457 | } |
| 7458 | |
| 7459 | static int |
| 7460 | charmap_decode_mapping(const char *s, |
| 7461 | Py_ssize_t size, |
| 7462 | PyObject *mapping, |
| 7463 | const char *errors, |
| 7464 | _PyUnicodeWriter *writer) |
| 7465 | { |
| 7466 | const char *starts = s; |
| 7467 | const char *e; |
| 7468 | Py_ssize_t startinpos, endinpos; |
| 7469 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7470 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7471 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7472 | |
| 7473 | e = s + size; |
| 7474 | |
| 7475 | while (s < e) { |
| 7476 | ch = *s; |
| 7477 | |
| 7478 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7479 | key = PyLong_FromLong((long)ch); |
| 7480 | if (key == NULL) |
| 7481 | goto onError; |
| 7482 | |
| 7483 | item = PyObject_GetItem(mapping, key); |
| 7484 | Py_DECREF(key); |
| 7485 | if (item == NULL) { |
| 7486 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7487 | /* No mapping found means: mapping is undefined. */ |
| 7488 | PyErr_Clear(); |
| 7489 | goto Undefined; |
| 7490 | } else |
| 7491 | goto onError; |
| 7492 | } |
| 7493 | |
| 7494 | /* Apply mapping */ |
| 7495 | if (item == Py_None) |
| 7496 | goto Undefined; |
| 7497 | if (PyLong_Check(item)) { |
| 7498 | long value = PyLong_AS_LONG(item); |
| 7499 | if (value == 0xFFFE) |
| 7500 | goto Undefined; |
| 7501 | if (value < 0 || value > MAX_UNICODE) { |
| 7502 | PyErr_Format(PyExc_TypeError, |
| 7503 | "character mapping must be in range(0x%lx)", |
| 7504 | (unsigned long)MAX_UNICODE + 1); |
| 7505 | goto onError; |
| 7506 | } |
| 7507 | |
| 7508 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7509 | goto onError; |
| 7510 | } |
| 7511 | else if (PyUnicode_Check(item)) { |
| 7512 | if (PyUnicode_READY(item) == -1) |
| 7513 | goto onError; |
| 7514 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7515 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7516 | if (value == 0xFFFE) |
| 7517 | goto Undefined; |
| 7518 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7519 | goto onError; |
| 7520 | } |
| 7521 | else { |
| 7522 | writer->overallocate = 1; |
| 7523 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7524 | goto onError; |
| 7525 | } |
| 7526 | } |
| 7527 | else { |
| 7528 | /* wrong return value */ |
| 7529 | PyErr_SetString(PyExc_TypeError, |
| 7530 | "character mapping must return integer, None or str"); |
| 7531 | goto onError; |
| 7532 | } |
| 7533 | Py_CLEAR(item); |
| 7534 | ++s; |
| 7535 | continue; |
| 7536 | |
| 7537 | Undefined: |
| 7538 | /* undefined mapping */ |
| 7539 | Py_CLEAR(item); |
| 7540 | startinpos = s-starts; |
| 7541 | endinpos = startinpos+1; |
| 7542 | if (unicode_decode_call_errorhandler_writer( |
| 7543 | errors, &errorHandler, |
| 7544 | "charmap", "character maps to <undefined>", |
| 7545 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7546 | writer)) { |
| 7547 | goto onError; |
| 7548 | } |
| 7549 | } |
| 7550 | Py_XDECREF(errorHandler); |
| 7551 | Py_XDECREF(exc); |
| 7552 | return 0; |
| 7553 | |
| 7554 | onError: |
| 7555 | Py_XDECREF(item); |
| 7556 | Py_XDECREF(errorHandler); |
| 7557 | Py_XDECREF(exc); |
| 7558 | return -1; |
| 7559 | } |
| 7560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7561 | PyObject * |
| 7562 | PyUnicode_DecodeCharmap(const char *s, |
| 7563 | Py_ssize_t size, |
| 7564 | PyObject *mapping, |
| 7565 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7567 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | /* Default to Latin-1 */ |
| 7570 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7571 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7574 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7575 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7576 | writer.min_length = size; |
| 7577 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7578 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7579 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7580 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7581 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7582 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7583 | } |
| 7584 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7585 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7586 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7588 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7590 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7591 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | return NULL; |
| 7593 | } |
| 7594 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7595 | /* Charmap encoding: the lookup table */ |
| 7596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7597 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | PyObject_HEAD |
| 7599 | unsigned char level1[32]; |
| 7600 | int count2, count3; |
| 7601 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7602 | }; |
| 7603 | |
| 7604 | static PyObject* |
| 7605 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7606 | { |
| 7607 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7608 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
| 7612 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7613 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7615 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7616 | }; |
| 7617 | |
| 7618 | static void |
| 7619 | encoding_map_dealloc(PyObject* o) |
| 7620 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7621 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7622 | } |
| 7623 | |
| 7624 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7625 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | "EncodingMap", /*tp_name*/ |
| 7627 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7628 | 0, /*tp_itemsize*/ |
| 7629 | /* methods */ |
| 7630 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7631 | 0, /*tp_print*/ |
| 7632 | 0, /*tp_getattr*/ |
| 7633 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7634 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7635 | 0, /*tp_repr*/ |
| 7636 | 0, /*tp_as_number*/ |
| 7637 | 0, /*tp_as_sequence*/ |
| 7638 | 0, /*tp_as_mapping*/ |
| 7639 | 0, /*tp_hash*/ |
| 7640 | 0, /*tp_call*/ |
| 7641 | 0, /*tp_str*/ |
| 7642 | 0, /*tp_getattro*/ |
| 7643 | 0, /*tp_setattro*/ |
| 7644 | 0, /*tp_as_buffer*/ |
| 7645 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7646 | 0, /*tp_doc*/ |
| 7647 | 0, /*tp_traverse*/ |
| 7648 | 0, /*tp_clear*/ |
| 7649 | 0, /*tp_richcompare*/ |
| 7650 | 0, /*tp_weaklistoffset*/ |
| 7651 | 0, /*tp_iter*/ |
| 7652 | 0, /*tp_iternext*/ |
| 7653 | encoding_map_methods, /*tp_methods*/ |
| 7654 | 0, /*tp_members*/ |
| 7655 | 0, /*tp_getset*/ |
| 7656 | 0, /*tp_base*/ |
| 7657 | 0, /*tp_dict*/ |
| 7658 | 0, /*tp_descr_get*/ |
| 7659 | 0, /*tp_descr_set*/ |
| 7660 | 0, /*tp_dictoffset*/ |
| 7661 | 0, /*tp_init*/ |
| 7662 | 0, /*tp_alloc*/ |
| 7663 | 0, /*tp_new*/ |
| 7664 | 0, /*tp_free*/ |
| 7665 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7666 | }; |
| 7667 | |
| 7668 | PyObject* |
| 7669 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7670 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7671 | PyObject *result; |
| 7672 | struct encoding_map *mresult; |
| 7673 | int i; |
| 7674 | int need_dict = 0; |
| 7675 | unsigned char level1[32]; |
| 7676 | unsigned char level2[512]; |
| 7677 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7678 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7679 | int kind; |
| 7680 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7681 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7682 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7683 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7684 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7685 | PyErr_BadArgument(); |
| 7686 | return NULL; |
| 7687 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7688 | kind = PyUnicode_KIND(string); |
| 7689 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7690 | length = PyUnicode_GET_LENGTH(string); |
| 7691 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7692 | memset(level1, 0xFF, sizeof level1); |
| 7693 | memset(level2, 0xFF, sizeof level2); |
| 7694 | |
| 7695 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7696 | or if there are non-BMP characters, we need to use |
| 7697 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7698 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7699 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7700 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7701 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7702 | ch = PyUnicode_READ(kind, data, i); |
| 7703 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7704 | need_dict = 1; |
| 7705 | break; |
| 7706 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7707 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7708 | /* unmapped character */ |
| 7709 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7710 | l1 = ch >> 11; |
| 7711 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7712 | if (level1[l1] == 0xFF) |
| 7713 | level1[l1] = count2++; |
| 7714 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7715 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
| 7718 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7719 | need_dict = 1; |
| 7720 | |
| 7721 | if (need_dict) { |
| 7722 | PyObject *result = PyDict_New(); |
| 7723 | PyObject *key, *value; |
| 7724 | if (!result) |
| 7725 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7726 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7727 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7728 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7729 | if (!key || !value) |
| 7730 | goto failed1; |
| 7731 | if (PyDict_SetItem(result, key, value) == -1) |
| 7732 | goto failed1; |
| 7733 | Py_DECREF(key); |
| 7734 | Py_DECREF(value); |
| 7735 | } |
| 7736 | return result; |
| 7737 | failed1: |
| 7738 | Py_XDECREF(key); |
| 7739 | Py_XDECREF(value); |
| 7740 | Py_DECREF(result); |
| 7741 | return NULL; |
| 7742 | } |
| 7743 | |
| 7744 | /* Create a three-level trie */ |
| 7745 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7746 | 16*count2 + 128*count3 - 1); |
| 7747 | if (!result) |
| 7748 | return PyErr_NoMemory(); |
| 7749 | PyObject_Init(result, &EncodingMapType); |
| 7750 | mresult = (struct encoding_map*)result; |
| 7751 | mresult->count2 = count2; |
| 7752 | mresult->count3 = count3; |
| 7753 | mlevel1 = mresult->level1; |
| 7754 | mlevel2 = mresult->level23; |
| 7755 | mlevel3 = mresult->level23 + 16*count2; |
| 7756 | memcpy(mlevel1, level1, 32); |
| 7757 | memset(mlevel2, 0xFF, 16*count2); |
| 7758 | memset(mlevel3, 0, 128*count3); |
| 7759 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7760 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7761 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7762 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7763 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7764 | /* unmapped character */ |
| 7765 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7766 | o1 = ch>>11; |
| 7767 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7768 | i2 = 16*mlevel1[o1] + o2; |
| 7769 | if (mlevel2[i2] == 0xFF) |
| 7770 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7771 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7772 | i3 = 128*mlevel2[i2] + o3; |
| 7773 | mlevel3[i3] = i; |
| 7774 | } |
| 7775 | return result; |
| 7776 | } |
| 7777 | |
| 7778 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7779 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7780 | { |
| 7781 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7782 | int l1 = c>>11; |
| 7783 | int l2 = (c>>7) & 0xF; |
| 7784 | int l3 = c & 0x7F; |
| 7785 | int i; |
| 7786 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7787 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7788 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7789 | if (c == 0) |
| 7790 | return 0; |
| 7791 | /* level 1*/ |
| 7792 | i = map->level1[l1]; |
| 7793 | if (i == 0xFF) { |
| 7794 | return -1; |
| 7795 | } |
| 7796 | /* level 2*/ |
| 7797 | i = map->level23[16*i+l2]; |
| 7798 | if (i == 0xFF) { |
| 7799 | return -1; |
| 7800 | } |
| 7801 | /* level 3 */ |
| 7802 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7803 | if (i == 0) { |
| 7804 | return -1; |
| 7805 | } |
| 7806 | return i; |
| 7807 | } |
| 7808 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7809 | /* Lookup the character ch in the mapping. If the character |
| 7810 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7811 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7812 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7813 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7815 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 | PyObject *x; |
| 7817 | |
| 7818 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7820 | x = PyObject_GetItem(mapping, w); |
| 7821 | Py_DECREF(w); |
| 7822 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7824 | /* No mapping found means: mapping is undefined. */ |
| 7825 | PyErr_Clear(); |
| 7826 | x = Py_None; |
| 7827 | Py_INCREF(x); |
| 7828 | return x; |
| 7829 | } else |
| 7830 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7832 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7834 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7835 | long value = PyLong_AS_LONG(x); |
| 7836 | if (value < 0 || value > 255) { |
| 7837 | PyErr_SetString(PyExc_TypeError, |
| 7838 | "character mapping must be in range(256)"); |
| 7839 | Py_DECREF(x); |
| 7840 | return NULL; |
| 7841 | } |
| 7842 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7844 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7847 | /* wrong return value */ |
| 7848 | PyErr_Format(PyExc_TypeError, |
| 7849 | "character mapping must return integer, bytes or None, not %.400s", |
| 7850 | x->ob_type->tp_name); |
| 7851 | Py_DECREF(x); |
| 7852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | } |
| 7854 | } |
| 7855 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7856 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7857 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7858 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7859 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7860 | /* exponentially overallocate to minimize reallocations */ |
| 7861 | if (requiredsize < 2*outsize) |
| 7862 | requiredsize = 2*outsize; |
| 7863 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7864 | return -1; |
| 7865 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7868 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7869 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7870 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7871 | /* 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] | 7872 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7873 | space is available. Return a new reference to the object that |
| 7874 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7875 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7876 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7877 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7878 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7879 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7880 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7881 | PyObject *rep; |
| 7882 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7883 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7884 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7885 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7886 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7888 | if (res == -1) |
| 7889 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | if (outsize<requiredsize) |
| 7891 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7892 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7893 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7894 | outstart[(*outpos)++] = (char)res; |
| 7895 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7896 | } |
| 7897 | |
| 7898 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7899 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7901 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7902 | Py_DECREF(rep); |
| 7903 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | if (PyLong_Check(rep)) { |
| 7906 | Py_ssize_t requiredsize = *outpos+1; |
| 7907 | if (outsize<requiredsize) |
| 7908 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7909 | Py_DECREF(rep); |
| 7910 | return enc_EXCEPTION; |
| 7911 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7912 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7914 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7915 | else { |
| 7916 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7917 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7918 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7919 | if (outsize<requiredsize) |
| 7920 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7921 | Py_DECREF(rep); |
| 7922 | return enc_EXCEPTION; |
| 7923 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7924 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | memcpy(outstart + *outpos, repchars, repsize); |
| 7926 | *outpos += repsize; |
| 7927 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7928 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7929 | Py_DECREF(rep); |
| 7930 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
| 7933 | /* handle an error in PyUnicode_EncodeCharmap |
| 7934 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7935 | static int |
| 7936 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7937 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7938 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7939 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7940 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7941 | { |
| 7942 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7943 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7944 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7945 | enum PyUnicode_Kind kind; |
| 7946 | void *data; |
| 7947 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7948 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7949 | Py_ssize_t collstartpos = *inpos; |
| 7950 | Py_ssize_t collendpos = *inpos+1; |
| 7951 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7952 | char *encoding = "charmap"; |
| 7953 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7954 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7955 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7956 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7957 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7958 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7959 | return -1; |
| 7960 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7961 | /* find all unencodable characters */ |
| 7962 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7963 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7964 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7965 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7966 | val = encoding_map_lookup(ch, mapping); |
| 7967 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | break; |
| 7969 | ++collendpos; |
| 7970 | continue; |
| 7971 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7972 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7973 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7974 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7975 | if (rep==NULL) |
| 7976 | return -1; |
| 7977 | else if (rep!=Py_None) { |
| 7978 | Py_DECREF(rep); |
| 7979 | break; |
| 7980 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7981 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7982 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7983 | } |
| 7984 | /* cache callback name lookup |
| 7985 | * (if not done yet, i.e. it's the first error) */ |
| 7986 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7988 | *known_errorHandler = 1; |
| 7989 | else if (!strcmp(errors, "replace")) |
| 7990 | *known_errorHandler = 2; |
| 7991 | else if (!strcmp(errors, "ignore")) |
| 7992 | *known_errorHandler = 3; |
| 7993 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7994 | *known_errorHandler = 4; |
| 7995 | else |
| 7996 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7997 | } |
| 7998 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7999 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8000 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8001 | return -1; |
| 8002 | case 2: /* replace */ |
| 8003 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8004 | x = charmapencode_output('?', mapping, res, respos); |
| 8005 | if (x==enc_EXCEPTION) { |
| 8006 | return -1; |
| 8007 | } |
| 8008 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8009 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8010 | return -1; |
| 8011 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8012 | } |
| 8013 | /* fall through */ |
| 8014 | case 3: /* ignore */ |
| 8015 | *inpos = collendpos; |
| 8016 | break; |
| 8017 | case 4: /* xmlcharrefreplace */ |
| 8018 | /* generate replacement (temporarily (mis)uses p) */ |
| 8019 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8020 | char buffer[2+29+1+1]; |
| 8021 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8022 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8023 | for (cp = buffer; *cp; ++cp) { |
| 8024 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8025 | if (x==enc_EXCEPTION) |
| 8026 | return -1; |
| 8027 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8028 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8029 | return -1; |
| 8030 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8031 | } |
| 8032 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8033 | *inpos = collendpos; |
| 8034 | break; |
| 8035 | default: |
| 8036 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8037 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8039 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8041 | if (PyBytes_Check(repunicode)) { |
| 8042 | /* Directly copy bytes result to output. */ |
| 8043 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8044 | Py_ssize_t requiredsize; |
| 8045 | repsize = PyBytes_Size(repunicode); |
| 8046 | requiredsize = *respos + repsize; |
| 8047 | if (requiredsize > outsize) |
| 8048 | /* Make room for all additional bytes. */ |
| 8049 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8050 | Py_DECREF(repunicode); |
| 8051 | return -1; |
| 8052 | } |
| 8053 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8054 | PyBytes_AsString(repunicode), repsize); |
| 8055 | *respos += repsize; |
| 8056 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8057 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8058 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8059 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8060 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8061 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8062 | Py_DECREF(repunicode); |
| 8063 | return -1; |
| 8064 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8065 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8066 | data = PyUnicode_DATA(repunicode); |
| 8067 | kind = PyUnicode_KIND(repunicode); |
| 8068 | for (index = 0; index < repsize; index++) { |
| 8069 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8070 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8072 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8073 | return -1; |
| 8074 | } |
| 8075 | else if (x==enc_FAILED) { |
| 8076 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8077 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | return -1; |
| 8079 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8080 | } |
| 8081 | *inpos = newpos; |
| 8082 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 | } |
| 8084 | return 0; |
| 8085 | } |
| 8086 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8087 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8088 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8089 | PyObject *mapping, |
| 8090 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8091 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8092 | /* output object */ |
| 8093 | PyObject *res = NULL; |
| 8094 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8095 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8096 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8097 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8098 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8099 | PyObject *errorHandler = NULL; |
| 8100 | PyObject *exc = NULL; |
| 8101 | /* the following variable is used for caching string comparisons |
| 8102 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8103 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8104 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8105 | void *data; |
| 8106 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8108 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8109 | return NULL; |
| 8110 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8111 | data = PyUnicode_DATA(unicode); |
| 8112 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | /* Default to Latin-1 */ |
| 8115 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8116 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | /* allocate enough for a simple encoding without |
| 8119 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8120 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | if (res == NULL) |
| 8122 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8123 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8124 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8126 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8127 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8129 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | if (x==enc_EXCEPTION) /* error */ |
| 8131 | goto onError; |
| 8132 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8133 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | &exc, |
| 8135 | &known_errorHandler, &errorHandler, errors, |
| 8136 | &res, &respos)) { |
| 8137 | goto onError; |
| 8138 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8139 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8140 | else |
| 8141 | /* done with this character => adjust input position */ |
| 8142 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8144 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8145 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8146 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8147 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8148 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8150 | Py_XDECREF(exc); |
| 8151 | Py_XDECREF(errorHandler); |
| 8152 | return res; |
| 8153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8154 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8155 | Py_XDECREF(res); |
| 8156 | Py_XDECREF(exc); |
| 8157 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8158 | return NULL; |
| 8159 | } |
| 8160 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8161 | /* Deprecated */ |
| 8162 | PyObject * |
| 8163 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8164 | Py_ssize_t size, |
| 8165 | PyObject *mapping, |
| 8166 | const char *errors) |
| 8167 | { |
| 8168 | PyObject *result; |
| 8169 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8170 | if (unicode == NULL) |
| 8171 | return NULL; |
| 8172 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8173 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8174 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8175 | } |
| 8176 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8177 | PyObject * |
| 8178 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8179 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | { |
| 8181 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8182 | PyErr_BadArgument(); |
| 8183 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8185 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | } |
| 8187 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8189 | static void |
| 8190 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8191 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8192 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8193 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8195 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8196 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8197 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | } |
| 8199 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8200 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8201 | goto onError; |
| 8202 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8203 | goto onError; |
| 8204 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8205 | goto onError; |
| 8206 | return; |
| 8207 | onError: |
| 8208 | Py_DECREF(*exceptionObject); |
| 8209 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8210 | } |
| 8211 | } |
| 8212 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8213 | /* error handling callback helper: |
| 8214 | build arguments, call the callback and check the arguments, |
| 8215 | put the result into newpos and return the replacement string, which |
| 8216 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8217 | static PyObject * |
| 8218 | unicode_translate_call_errorhandler(const char *errors, |
| 8219 | PyObject **errorHandler, |
| 8220 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8221 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8222 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8223 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8224 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8225 | 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] | 8226 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8227 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8228 | PyObject *restuple; |
| 8229 | PyObject *resunicode; |
| 8230 | |
| 8231 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8238 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8239 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8240 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8241 | |
| 8242 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8246 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8247 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | Py_DECREF(restuple); |
| 8249 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8250 | } |
| 8251 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | &resunicode, &i_newpos)) { |
| 8253 | Py_DECREF(restuple); |
| 8254 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8255 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8256 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8257 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8258 | else |
| 8259 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8260 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8262 | Py_DECREF(restuple); |
| 8263 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8264 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8265 | Py_INCREF(resunicode); |
| 8266 | Py_DECREF(restuple); |
| 8267 | return resunicode; |
| 8268 | } |
| 8269 | |
| 8270 | /* Lookup the character ch in the mapping and put the result in result, |
| 8271 | which must be decrefed by the caller. |
| 8272 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8273 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8276 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8277 | PyObject *x; |
| 8278 | |
| 8279 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8280 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8281 | x = PyObject_GetItem(mapping, w); |
| 8282 | Py_DECREF(w); |
| 8283 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8284 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8285 | /* No mapping found means: use 1:1 mapping. */ |
| 8286 | PyErr_Clear(); |
| 8287 | *result = NULL; |
| 8288 | return 0; |
| 8289 | } else |
| 8290 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8291 | } |
| 8292 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8293 | *result = x; |
| 8294 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8295 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8296 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | long value = PyLong_AS_LONG(x); |
| 8298 | long max = PyUnicode_GetMax(); |
| 8299 | if (value < 0 || value > max) { |
| 8300 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8301 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | Py_DECREF(x); |
| 8303 | return -1; |
| 8304 | } |
| 8305 | *result = x; |
| 8306 | return 0; |
| 8307 | } |
| 8308 | else if (PyUnicode_Check(x)) { |
| 8309 | *result = x; |
| 8310 | return 0; |
| 8311 | } |
| 8312 | else { |
| 8313 | /* wrong return value */ |
| 8314 | PyErr_SetString(PyExc_TypeError, |
| 8315 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8316 | Py_DECREF(x); |
| 8317 | return -1; |
| 8318 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | } |
| 8320 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | if not reallocate and adjust various state variables. |
| 8322 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8323 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8324 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8325 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8326 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8328 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8329 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | /* exponentially overallocate to minimize reallocations */ |
| 8331 | if (requiredsize < 2 * oldsize) |
| 8332 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8333 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8334 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8336 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8337 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8338 | } |
| 8339 | return 0; |
| 8340 | } |
| 8341 | /* lookup the character, put the result in the output string and adjust |
| 8342 | various state variables. Return a new reference to the object that |
| 8343 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8344 | undefined (in which case no character was written). |
| 8345 | The called must decref result. |
| 8346 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8347 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8348 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8349 | PyObject *mapping, Py_UCS4 **output, |
| 8350 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8351 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8353 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8354 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8356 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8358 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8359 | } |
| 8360 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8362 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | /* 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] | 8364 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8365 | } |
| 8366 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8367 | Py_ssize_t repsize; |
| 8368 | if (PyUnicode_READY(*res) == -1) |
| 8369 | return -1; |
| 8370 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8371 | if (repsize==1) { |
| 8372 | /* 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] | 8373 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | } |
| 8375 | else if (repsize!=0) { |
| 8376 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8377 | Py_ssize_t requiredsize = *opos + |
| 8378 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | Py_ssize_t i; |
| 8381 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8382 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8383 | for(i = 0; i < repsize; i++) |
| 8384 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8385 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8386 | } |
| 8387 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | return 0; |
| 8390 | } |
| 8391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8392 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8393 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8394 | PyObject *mapping, |
| 8395 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8396 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8397 | /* input object */ |
| 8398 | char *idata; |
| 8399 | Py_ssize_t size, i; |
| 8400 | int kind; |
| 8401 | /* output buffer */ |
| 8402 | Py_UCS4 *output = NULL; |
| 8403 | Py_ssize_t osize; |
| 8404 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8406 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8407 | char *reason = "character maps to <undefined>"; |
| 8408 | PyObject *errorHandler = NULL; |
| 8409 | PyObject *exc = NULL; |
| 8410 | /* the following variable is used for caching string comparisons |
| 8411 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8412 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8413 | int known_errorHandler = -1; |
| 8414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8415 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | PyErr_BadArgument(); |
| 8417 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8418 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8420 | if (PyUnicode_READY(input) == -1) |
| 8421 | return NULL; |
| 8422 | idata = (char*)PyUnicode_DATA(input); |
| 8423 | kind = PyUnicode_KIND(input); |
| 8424 | size = PyUnicode_GET_LENGTH(input); |
| 8425 | i = 0; |
| 8426 | |
| 8427 | if (size == 0) { |
| 8428 | Py_INCREF(input); |
| 8429 | return input; |
| 8430 | } |
| 8431 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8432 | /* allocate enough for a simple 1:1 translation without |
| 8433 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | osize = size; |
| 8435 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8436 | opos = 0; |
| 8437 | if (output == NULL) { |
| 8438 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | /* try to encode it */ |
| 8444 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | if (charmaptranslate_output(input, i, mapping, |
| 8446 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8447 | Py_XDECREF(x); |
| 8448 | goto onError; |
| 8449 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8450 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8452 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | else { /* untranslatable character */ |
| 8454 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8455 | Py_ssize_t repsize; |
| 8456 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8457 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8459 | Py_ssize_t collstart = i; |
| 8460 | Py_ssize_t collend = i+1; |
| 8461 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | while (collend < size) { |
| 8465 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | goto onError; |
| 8467 | Py_XDECREF(x); |
| 8468 | if (x!=Py_None) |
| 8469 | break; |
| 8470 | ++collend; |
| 8471 | } |
| 8472 | /* cache callback name lookup |
| 8473 | * (if not done yet, i.e. it's the first error) */ |
| 8474 | if (known_errorHandler==-1) { |
| 8475 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8476 | known_errorHandler = 1; |
| 8477 | else if (!strcmp(errors, "replace")) |
| 8478 | known_errorHandler = 2; |
| 8479 | else if (!strcmp(errors, "ignore")) |
| 8480 | known_errorHandler = 3; |
| 8481 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8482 | known_errorHandler = 4; |
| 8483 | else |
| 8484 | known_errorHandler = 0; |
| 8485 | } |
| 8486 | switch (known_errorHandler) { |
| 8487 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8488 | make_translate_exception(&exc, |
| 8489 | input, collstart, collend, reason); |
| 8490 | if (exc != NULL) |
| 8491 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8492 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8493 | case 2: /* replace */ |
| 8494 | /* 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] | 8495 | for (coll = collstart; coll<collend; coll++) |
| 8496 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | /* fall through */ |
| 8498 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8499 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | break; |
| 8501 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | /* generate replacement (temporarily (mis)uses i) */ |
| 8503 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8504 | char buffer[2+29+1+1]; |
| 8505 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8506 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8507 | if (charmaptranslate_makespace(&output, &osize, |
| 8508 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | goto onError; |
| 8510 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8513 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | break; |
| 8515 | default: |
| 8516 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | reason, input, &exc, |
| 8518 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8519 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8521 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8522 | Py_DECREF(repunicode); |
| 8523 | goto onError; |
| 8524 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8527 | if (charmaptranslate_makespace(&output, &osize, |
| 8528 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | Py_DECREF(repunicode); |
| 8530 | goto onError; |
| 8531 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8533 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8534 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8536 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8537 | } |
| 8538 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8540 | if (!res) |
| 8541 | goto onError; |
| 8542 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8543 | Py_XDECREF(exc); |
| 8544 | Py_XDECREF(errorHandler); |
| 8545 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8547 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8548 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8549 | Py_XDECREF(exc); |
| 8550 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8551 | return NULL; |
| 8552 | } |
| 8553 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8554 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8555 | PyObject * |
| 8556 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8557 | Py_ssize_t size, |
| 8558 | PyObject *mapping, |
| 8559 | const char *errors) |
| 8560 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8561 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8562 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8563 | if (!unicode) |
| 8564 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8565 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8566 | Py_DECREF(unicode); |
| 8567 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8568 | } |
| 8569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8570 | PyObject * |
| 8571 | PyUnicode_Translate(PyObject *str, |
| 8572 | PyObject *mapping, |
| 8573 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | { |
| 8575 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | str = PyUnicode_FromObject(str); |
| 8578 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8579 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8580 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | Py_DECREF(str); |
| 8582 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8584 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8586 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8587 | { |
| 8588 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8589 | called as a callback from fixup() which does it already. */ |
| 8590 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8591 | const int kind = PyUnicode_KIND(self); |
| 8592 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8593 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8594 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8595 | Py_ssize_t i; |
| 8596 | |
| 8597 | for (i = 0; i < len; ++i) { |
| 8598 | ch = PyUnicode_READ(kind, data, i); |
| 8599 | fixed = 0; |
| 8600 | if (ch > 127) { |
| 8601 | if (Py_UNICODE_ISSPACE(ch)) |
| 8602 | fixed = ' '; |
| 8603 | else { |
| 8604 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8605 | if (decimal >= 0) |
| 8606 | fixed = '0' + decimal; |
| 8607 | } |
| 8608 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8609 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8610 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8612 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8613 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8614 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | } |
| 8617 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8618 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | } |
| 8620 | |
| 8621 | PyObject * |
| 8622 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8623 | { |
| 8624 | if (!PyUnicode_Check(unicode)) { |
| 8625 | PyErr_BadInternalCall(); |
| 8626 | return NULL; |
| 8627 | } |
| 8628 | if (PyUnicode_READY(unicode) == -1) |
| 8629 | return NULL; |
| 8630 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8631 | /* If the string is already ASCII, just return the same string */ |
| 8632 | Py_INCREF(unicode); |
| 8633 | return unicode; |
| 8634 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8635 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 | } |
| 8637 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8638 | PyObject * |
| 8639 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8640 | Py_ssize_t length) |
| 8641 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8642 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8643 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8644 | Py_UCS4 maxchar; |
| 8645 | enum PyUnicode_Kind kind; |
| 8646 | void *data; |
| 8647 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8648 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8649 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8650 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8651 | if (ch > 127) { |
| 8652 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8653 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8654 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8655 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8656 | } |
| 8657 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8658 | |
| 8659 | /* Copy to a new string */ |
| 8660 | decimal = PyUnicode_New(length, maxchar); |
| 8661 | if (decimal == NULL) |
| 8662 | return decimal; |
| 8663 | kind = PyUnicode_KIND(decimal); |
| 8664 | data = PyUnicode_DATA(decimal); |
| 8665 | /* Iterate over code points */ |
| 8666 | for (i = 0; i < length; i++) { |
| 8667 | Py_UNICODE ch = s[i]; |
| 8668 | if (ch > 127) { |
| 8669 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8670 | if (decimal >= 0) |
| 8671 | ch = '0' + decimal; |
| 8672 | } |
| 8673 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8675 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8676 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8677 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8679 | int |
| 8680 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8681 | Py_ssize_t length, |
| 8682 | char *output, |
| 8683 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8684 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8685 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8686 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8687 | enum PyUnicode_Kind kind; |
| 8688 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8689 | |
| 8690 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8691 | PyErr_BadArgument(); |
| 8692 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8693 | } |
| 8694 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8695 | unicode = PyUnicode_FromUnicode(s, length); |
| 8696 | if (unicode == NULL) |
| 8697 | return -1; |
| 8698 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8699 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8700 | Py_DECREF(unicode); |
| 8701 | return -1; |
| 8702 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8703 | kind = PyUnicode_KIND(unicode); |
| 8704 | data = PyUnicode_DATA(unicode); |
| 8705 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8706 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8707 | PyObject *exc; |
| 8708 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8709 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8710 | Py_ssize_t startpos; |
| 8711 | |
| 8712 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8713 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8715 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8716 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8718 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8720 | if (decimal >= 0) { |
| 8721 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8722 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 | continue; |
| 8724 | } |
| 8725 | if (0 < ch && ch < 256) { |
| 8726 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8727 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8728 | continue; |
| 8729 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8730 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8731 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8732 | exc = NULL; |
| 8733 | raise_encode_exception(&exc, "decimal", unicode, |
| 8734 | startpos, startpos+1, |
| 8735 | "invalid decimal Unicode string"); |
| 8736 | Py_XDECREF(exc); |
| 8737 | Py_DECREF(unicode); |
| 8738 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8739 | } |
| 8740 | /* 0-terminate the output string */ |
| 8741 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8742 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8743 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | /* --- Helpers ------------------------------------------------------------ */ |
| 8747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8749 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8750 | Py_ssize_t start, |
| 8751 | Py_ssize_t end) |
| 8752 | { |
| 8753 | int kind1, kind2, kind; |
| 8754 | void *buf1, *buf2; |
| 8755 | Py_ssize_t len1, len2, result; |
| 8756 | |
| 8757 | kind1 = PyUnicode_KIND(s1); |
| 8758 | kind2 = PyUnicode_KIND(s2); |
| 8759 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8760 | buf1 = PyUnicode_DATA(s1); |
| 8761 | buf2 = PyUnicode_DATA(s2); |
| 8762 | if (kind1 != kind) |
| 8763 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8764 | if (!buf1) |
| 8765 | return -2; |
| 8766 | if (kind2 != kind) |
| 8767 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8768 | if (!buf2) { |
| 8769 | if (kind1 != kind) PyMem_Free(buf1); |
| 8770 | return -2; |
| 8771 | } |
| 8772 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8773 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8774 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8775 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8776 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8777 | case PyUnicode_1BYTE_KIND: |
| 8778 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8779 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8780 | else |
| 8781 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8782 | break; |
| 8783 | case PyUnicode_2BYTE_KIND: |
| 8784 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8785 | break; |
| 8786 | case PyUnicode_4BYTE_KIND: |
| 8787 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8788 | break; |
| 8789 | default: |
| 8790 | assert(0); result = -2; |
| 8791 | } |
| 8792 | } |
| 8793 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8794 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8795 | case PyUnicode_1BYTE_KIND: |
| 8796 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8797 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8798 | else |
| 8799 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8800 | break; |
| 8801 | case PyUnicode_2BYTE_KIND: |
| 8802 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8803 | break; |
| 8804 | case PyUnicode_4BYTE_KIND: |
| 8805 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8806 | break; |
| 8807 | default: |
| 8808 | assert(0); result = -2; |
| 8809 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | if (kind1 != kind) |
| 8813 | PyMem_Free(buf1); |
| 8814 | if (kind2 != kind) |
| 8815 | PyMem_Free(buf2); |
| 8816 | |
| 8817 | return result; |
| 8818 | } |
| 8819 | |
| 8820 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8821 | _PyUnicode_InsertThousandsGrouping( |
| 8822 | PyObject *unicode, Py_ssize_t index, |
| 8823 | Py_ssize_t n_buffer, |
| 8824 | void *digits, Py_ssize_t n_digits, |
| 8825 | Py_ssize_t min_width, |
| 8826 | const char *grouping, PyObject *thousands_sep, |
| 8827 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8828 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8829 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8830 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8831 | Py_ssize_t thousands_sep_len; |
| 8832 | Py_ssize_t len; |
| 8833 | |
| 8834 | if (unicode != NULL) { |
| 8835 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8836 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8837 | } |
| 8838 | else { |
| 8839 | kind = PyUnicode_1BYTE_KIND; |
| 8840 | data = NULL; |
| 8841 | } |
| 8842 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8843 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8844 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8845 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8846 | if (thousands_sep_kind < kind) { |
| 8847 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8848 | if (!thousands_sep_data) |
| 8849 | return -1; |
| 8850 | } |
| 8851 | else { |
| 8852 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8853 | if (!data) |
| 8854 | return -1; |
| 8855 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8856 | } |
| 8857 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8858 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8859 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8860 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8861 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8862 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8863 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8864 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8865 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8866 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8867 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8868 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8869 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8870 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8872 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8873 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8874 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8875 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8876 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8877 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8878 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8879 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8880 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8881 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8882 | break; |
| 8883 | default: |
| 8884 | assert(0); |
| 8885 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8886 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8887 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8888 | if (thousands_sep_kind < kind) |
| 8889 | PyMem_Free(thousands_sep_data); |
| 8890 | else |
| 8891 | PyMem_Free(data); |
| 8892 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8893 | if (unicode == NULL) { |
| 8894 | *maxchar = 127; |
| 8895 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8896 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8897 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8898 | } |
| 8899 | } |
| 8900 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | } |
| 8902 | |
| 8903 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8904 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8905 | #define ADJUST_INDICES(start, end, len) \ |
| 8906 | if (end > len) \ |
| 8907 | end = len; \ |
| 8908 | else if (end < 0) { \ |
| 8909 | end += len; \ |
| 8910 | if (end < 0) \ |
| 8911 | end = 0; \ |
| 8912 | } \ |
| 8913 | if (start < 0) { \ |
| 8914 | start += len; \ |
| 8915 | if (start < 0) \ |
| 8916 | start = 0; \ |
| 8917 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8919 | Py_ssize_t |
| 8920 | PyUnicode_Count(PyObject *str, |
| 8921 | PyObject *substr, |
| 8922 | Py_ssize_t start, |
| 8923 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8924 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8925 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8926 | PyObject* str_obj; |
| 8927 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | int kind1, kind2, kind; |
| 8929 | void *buf1 = NULL, *buf2 = NULL; |
| 8930 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8931 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8932 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8933 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8935 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8936 | if (!sub_obj) { |
| 8937 | Py_DECREF(str_obj); |
| 8938 | return -1; |
| 8939 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8940 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8941 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8942 | Py_DECREF(str_obj); |
| 8943 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8944 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8945 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8946 | kind1 = PyUnicode_KIND(str_obj); |
| 8947 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8948 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8951 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8952 | if (kind2 > kind) { |
| 8953 | Py_DECREF(sub_obj); |
| 8954 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8955 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8956 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8957 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8958 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8959 | if (!buf2) |
| 8960 | goto onError; |
| 8961 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8962 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8963 | |
| 8964 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8965 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8967 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8968 | result = asciilib_count( |
| 8969 | ((Py_UCS1*)buf1) + start, end - start, |
| 8970 | buf2, len2, PY_SSIZE_T_MAX |
| 8971 | ); |
| 8972 | else |
| 8973 | result = ucs1lib_count( |
| 8974 | ((Py_UCS1*)buf1) + start, end - start, |
| 8975 | buf2, len2, PY_SSIZE_T_MAX |
| 8976 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8977 | break; |
| 8978 | case PyUnicode_2BYTE_KIND: |
| 8979 | result = ucs2lib_count( |
| 8980 | ((Py_UCS2*)buf1) + start, end - start, |
| 8981 | buf2, len2, PY_SSIZE_T_MAX |
| 8982 | ); |
| 8983 | break; |
| 8984 | case PyUnicode_4BYTE_KIND: |
| 8985 | result = ucs4lib_count( |
| 8986 | ((Py_UCS4*)buf1) + start, end - start, |
| 8987 | buf2, len2, PY_SSIZE_T_MAX |
| 8988 | ); |
| 8989 | break; |
| 8990 | default: |
| 8991 | assert(0); result = 0; |
| 8992 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8993 | |
| 8994 | Py_DECREF(sub_obj); |
| 8995 | Py_DECREF(str_obj); |
| 8996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8997 | if (kind2 != kind) |
| 8998 | PyMem_Free(buf2); |
| 8999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | onError: |
| 9002 | Py_DECREF(sub_obj); |
| 9003 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | if (kind2 != kind && buf2) |
| 9005 | PyMem_Free(buf2); |
| 9006 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | } |
| 9008 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9009 | Py_ssize_t |
| 9010 | PyUnicode_Find(PyObject *str, |
| 9011 | PyObject *sub, |
| 9012 | Py_ssize_t start, |
| 9013 | Py_ssize_t end, |
| 9014 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9016 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9017 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9019 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9020 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9021 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9022 | if (!sub) { |
| 9023 | Py_DECREF(str); |
| 9024 | return -2; |
| 9025 | } |
| 9026 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9027 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9028 | Py_DECREF(str); |
| 9029 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9031 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9032 | result = any_find_slice(direction, |
| 9033 | str, sub, start, end |
| 9034 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9035 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9037 | Py_DECREF(sub); |
| 9038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | return result; |
| 9040 | } |
| 9041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | Py_ssize_t |
| 9043 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9044 | Py_ssize_t start, Py_ssize_t end, |
| 9045 | int direction) |
| 9046 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9048 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | if (PyUnicode_READY(str) == -1) |
| 9050 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9051 | if (start < 0 || end < 0) { |
| 9052 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9053 | return -2; |
| 9054 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9055 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9056 | end = PyUnicode_GET_LENGTH(str); |
| 9057 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9058 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9059 | kind, end-start, ch, direction); |
| 9060 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9062 | else |
| 9063 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | } |
| 9065 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9066 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9067 | tailmatch(PyObject *self, |
| 9068 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9069 | Py_ssize_t start, |
| 9070 | Py_ssize_t end, |
| 9071 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9072 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9073 | int kind_self; |
| 9074 | int kind_sub; |
| 9075 | void *data_self; |
| 9076 | void *data_sub; |
| 9077 | Py_ssize_t offset; |
| 9078 | Py_ssize_t i; |
| 9079 | Py_ssize_t end_sub; |
| 9080 | |
| 9081 | if (PyUnicode_READY(self) == -1 || |
| 9082 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9083 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9084 | |
| 9085 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 | return 1; |
| 9087 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9088 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9089 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9091 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | kind_self = PyUnicode_KIND(self); |
| 9094 | data_self = PyUnicode_DATA(self); |
| 9095 | kind_sub = PyUnicode_KIND(substring); |
| 9096 | data_sub = PyUnicode_DATA(substring); |
| 9097 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9098 | |
| 9099 | if (direction > 0) |
| 9100 | offset = end; |
| 9101 | else |
| 9102 | offset = start; |
| 9103 | |
| 9104 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9105 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9106 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9107 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9108 | /* If both are of the same kind, memcmp is sufficient */ |
| 9109 | if (kind_self == kind_sub) { |
| 9110 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9111 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | data_sub, |
| 9113 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9114 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9115 | } |
| 9116 | /* otherwise we have to compare each character by first accesing it */ |
| 9117 | else { |
| 9118 | /* We do not need to compare 0 and len(substring)-1 because |
| 9119 | the if statement above ensured already that they are equal |
| 9120 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | for (i = 1; i < end_sub; ++i) { |
| 9122 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9123 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9124 | return 0; |
| 9125 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9126 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9127 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9128 | } |
| 9129 | |
| 9130 | return 0; |
| 9131 | } |
| 9132 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9133 | Py_ssize_t |
| 9134 | PyUnicode_Tailmatch(PyObject *str, |
| 9135 | PyObject *substr, |
| 9136 | Py_ssize_t start, |
| 9137 | Py_ssize_t end, |
| 9138 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9139 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9140 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | str = PyUnicode_FromObject(str); |
| 9143 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | substr = PyUnicode_FromObject(substr); |
| 9146 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | Py_DECREF(str); |
| 9148 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9149 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9150 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9151 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | Py_DECREF(str); |
| 9154 | Py_DECREF(substr); |
| 9155 | return result; |
| 9156 | } |
| 9157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | /* Apply fixfct filter to the Unicode object self and return a |
| 9159 | reference to the modified object */ |
| 9160 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9161 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9162 | fixup(PyObject *self, |
| 9163 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9164 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9165 | PyObject *u; |
| 9166 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9167 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9168 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9169 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9170 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9171 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9172 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9174 | /* fix functions return the new maximum character in a string, |
| 9175 | if the kind of the resulting unicode object does not change, |
| 9176 | everything is fine. Otherwise we need to change the string kind |
| 9177 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9178 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9179 | |
| 9180 | if (maxchar_new == 0) { |
| 9181 | /* no changes */; |
| 9182 | if (PyUnicode_CheckExact(self)) { |
| 9183 | Py_DECREF(u); |
| 9184 | Py_INCREF(self); |
| 9185 | return self; |
| 9186 | } |
| 9187 | else |
| 9188 | return u; |
| 9189 | } |
| 9190 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9191 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9192 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9193 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9195 | |
| 9196 | /* In case the maximum character changed, we need to |
| 9197 | convert the string to the new category. */ |
| 9198 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9199 | if (v == NULL) { |
| 9200 | Py_DECREF(u); |
| 9201 | return NULL; |
| 9202 | } |
| 9203 | if (maxchar_new > maxchar_old) { |
| 9204 | /* If the maxchar increased so that the kind changed, not all |
| 9205 | characters are representable anymore and we need to fix the |
| 9206 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9207 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9208 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9209 | maxchar_old = fixfct(v); |
| 9210 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9211 | } |
| 9212 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9213 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9214 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9215 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9216 | Py_DECREF(u); |
| 9217 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9218 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9219 | } |
| 9220 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9221 | static PyObject * |
| 9222 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9223 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9224 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9225 | char *resdata, *data = PyUnicode_DATA(self); |
| 9226 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9227 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9228 | res = PyUnicode_New(len, 127); |
| 9229 | if (res == NULL) |
| 9230 | return NULL; |
| 9231 | resdata = PyUnicode_DATA(res); |
| 9232 | if (lower) |
| 9233 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9234 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9235 | _Py_bytes_upper(resdata, data, len); |
| 9236 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9237 | } |
| 9238 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9239 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9240 | 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] | 9241 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9242 | Py_ssize_t j; |
| 9243 | int final_sigma; |
| 9244 | Py_UCS4 c; |
| 9245 | /* 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] | 9246 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9247 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9248 | |
| 9249 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9250 | */ |
| 9251 | for (j = i - 1; j >= 0; j--) { |
| 9252 | c = PyUnicode_READ(kind, data, j); |
| 9253 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9254 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9255 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9256 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9257 | if (final_sigma) { |
| 9258 | for (j = i + 1; j < length; j++) { |
| 9259 | c = PyUnicode_READ(kind, data, j); |
| 9260 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9261 | break; |
| 9262 | } |
| 9263 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9264 | } |
| 9265 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9266 | } |
| 9267 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9268 | static int |
| 9269 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9270 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9272 | /* Obscure special case. */ |
| 9273 | if (c == 0x3A3) { |
| 9274 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9275 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9277 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9278 | } |
| 9279 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9280 | static Py_ssize_t |
| 9281 | 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] | 9282 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9283 | Py_ssize_t i, k = 0; |
| 9284 | int n_res, j; |
| 9285 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9286 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9287 | c = PyUnicode_READ(kind, data, 0); |
| 9288 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9289 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9290 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9291 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9293 | for (i = 1; i < length; i++) { |
| 9294 | c = PyUnicode_READ(kind, data, i); |
| 9295 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9296 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9297 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9298 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9299 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9300 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9301 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | } |
| 9303 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9304 | static Py_ssize_t |
| 9305 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9306 | Py_ssize_t i, k = 0; |
| 9307 | |
| 9308 | for (i = 0; i < length; i++) { |
| 9309 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9310 | int n_res, j; |
| 9311 | if (Py_UNICODE_ISUPPER(c)) { |
| 9312 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9313 | } |
| 9314 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9315 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9316 | } |
| 9317 | else { |
| 9318 | n_res = 1; |
| 9319 | mapped[0] = c; |
| 9320 | } |
| 9321 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9322 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9323 | res[k++] = mapped[j]; |
| 9324 | } |
| 9325 | } |
| 9326 | return k; |
| 9327 | } |
| 9328 | |
| 9329 | static Py_ssize_t |
| 9330 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9331 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9332 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9333 | Py_ssize_t i, k = 0; |
| 9334 | |
| 9335 | for (i = 0; i < length; i++) { |
| 9336 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9337 | int n_res, j; |
| 9338 | if (lower) |
| 9339 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9340 | else |
| 9341 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9342 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9343 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9344 | res[k++] = mapped[j]; |
| 9345 | } |
| 9346 | } |
| 9347 | return k; |
| 9348 | } |
| 9349 | |
| 9350 | static Py_ssize_t |
| 9351 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9352 | { |
| 9353 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9354 | } |
| 9355 | |
| 9356 | static Py_ssize_t |
| 9357 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9358 | { |
| 9359 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9360 | } |
| 9361 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9362 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9363 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9364 | { |
| 9365 | Py_ssize_t i, k = 0; |
| 9366 | |
| 9367 | for (i = 0; i < length; i++) { |
| 9368 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9369 | Py_UCS4 mapped[3]; |
| 9370 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9371 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9372 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9373 | res[k++] = mapped[j]; |
| 9374 | } |
| 9375 | } |
| 9376 | return k; |
| 9377 | } |
| 9378 | |
| 9379 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9380 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9381 | { |
| 9382 | Py_ssize_t i, k = 0; |
| 9383 | int previous_is_cased; |
| 9384 | |
| 9385 | previous_is_cased = 0; |
| 9386 | for (i = 0; i < length; i++) { |
| 9387 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9388 | Py_UCS4 mapped[3]; |
| 9389 | int n_res, j; |
| 9390 | |
| 9391 | if (previous_is_cased) |
| 9392 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9393 | else |
| 9394 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9395 | |
| 9396 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9397 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9398 | res[k++] = mapped[j]; |
| 9399 | } |
| 9400 | |
| 9401 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9402 | } |
| 9403 | return k; |
| 9404 | } |
| 9405 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9406 | static PyObject * |
| 9407 | case_operation(PyObject *self, |
| 9408 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9409 | { |
| 9410 | PyObject *res = NULL; |
| 9411 | Py_ssize_t length, newlength = 0; |
| 9412 | int kind, outkind; |
| 9413 | void *data, *outdata; |
| 9414 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9415 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9416 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9417 | |
| 9418 | kind = PyUnicode_KIND(self); |
| 9419 | data = PyUnicode_DATA(self); |
| 9420 | length = PyUnicode_GET_LENGTH(self); |
| 9421 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9422 | if (tmp == NULL) |
| 9423 | return PyErr_NoMemory(); |
| 9424 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9425 | res = PyUnicode_New(newlength, maxchar); |
| 9426 | if (res == NULL) |
| 9427 | goto leave; |
| 9428 | tmpend = tmp + newlength; |
| 9429 | outdata = PyUnicode_DATA(res); |
| 9430 | outkind = PyUnicode_KIND(res); |
| 9431 | switch (outkind) { |
| 9432 | case PyUnicode_1BYTE_KIND: |
| 9433 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9434 | break; |
| 9435 | case PyUnicode_2BYTE_KIND: |
| 9436 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9437 | break; |
| 9438 | case PyUnicode_4BYTE_KIND: |
| 9439 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9440 | break; |
| 9441 | default: |
| 9442 | assert(0); |
| 9443 | break; |
| 9444 | } |
| 9445 | leave: |
| 9446 | PyMem_FREE(tmp); |
| 9447 | return res; |
| 9448 | } |
| 9449 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9450 | PyObject * |
| 9451 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9452 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9454 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9456 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9457 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9458 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9459 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9461 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9462 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9463 | int use_memcpy; |
| 9464 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9465 | PyObject *last_obj; |
| 9466 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9467 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9468 | fseq = PySequence_Fast(seq, ""); |
| 9469 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9470 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9471 | } |
| 9472 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9473 | /* NOTE: the following code can't call back into Python code, |
| 9474 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9475 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9476 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9477 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9478 | /* If empty sequence, return u"". */ |
| 9479 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9480 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9481 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9482 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9483 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9484 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9485 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9486 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9487 | if (seqlen == 1) { |
| 9488 | if (PyUnicode_CheckExact(items[0])) { |
| 9489 | res = items[0]; |
| 9490 | Py_INCREF(res); |
| 9491 | Py_DECREF(fseq); |
| 9492 | return res; |
| 9493 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9494 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9495 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9496 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9497 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9498 | /* Set up sep and seplen */ |
| 9499 | if (separator == NULL) { |
| 9500 | /* fall back to a blank space separator */ |
| 9501 | sep = PyUnicode_FromOrdinal(' '); |
| 9502 | if (!sep) |
| 9503 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9504 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9505 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9506 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9507 | else { |
| 9508 | if (!PyUnicode_Check(separator)) { |
| 9509 | PyErr_Format(PyExc_TypeError, |
| 9510 | "separator: expected str instance," |
| 9511 | " %.80s found", |
| 9512 | Py_TYPE(separator)->tp_name); |
| 9513 | goto onError; |
| 9514 | } |
| 9515 | if (PyUnicode_READY(separator)) |
| 9516 | goto onError; |
| 9517 | sep = separator; |
| 9518 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9519 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9520 | /* inc refcount to keep this code path symmetric with the |
| 9521 | above case of a blank separator */ |
| 9522 | Py_INCREF(sep); |
| 9523 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9524 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9525 | } |
| 9526 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9527 | /* There are at least two things to join, or else we have a subclass |
| 9528 | * of str in the sequence. |
| 9529 | * Do a pre-pass to figure out the total amount of space we'll |
| 9530 | * need (sz), and see whether all argument are strings. |
| 9531 | */ |
| 9532 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9533 | #ifdef Py_DEBUG |
| 9534 | use_memcpy = 0; |
| 9535 | #else |
| 9536 | use_memcpy = 1; |
| 9537 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9538 | for (i = 0; i < seqlen; i++) { |
| 9539 | const Py_ssize_t old_sz = sz; |
| 9540 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9541 | if (!PyUnicode_Check(item)) { |
| 9542 | PyErr_Format(PyExc_TypeError, |
| 9543 | "sequence item %zd: expected str instance," |
| 9544 | " %.80s found", |
| 9545 | i, Py_TYPE(item)->tp_name); |
| 9546 | goto onError; |
| 9547 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9548 | if (PyUnicode_READY(item) == -1) |
| 9549 | goto onError; |
| 9550 | sz += PyUnicode_GET_LENGTH(item); |
| 9551 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9552 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9553 | if (i != 0) |
| 9554 | sz += seplen; |
| 9555 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9556 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9557 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9558 | goto onError; |
| 9559 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9560 | if (use_memcpy && last_obj != NULL) { |
| 9561 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9562 | use_memcpy = 0; |
| 9563 | } |
| 9564 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9565 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9566 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9567 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9568 | if (res == NULL) |
| 9569 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9570 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9571 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9572 | #ifdef Py_DEBUG |
| 9573 | use_memcpy = 0; |
| 9574 | #else |
| 9575 | if (use_memcpy) { |
| 9576 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9577 | kind = PyUnicode_KIND(res); |
| 9578 | if (seplen != 0) |
| 9579 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9580 | } |
| 9581 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9582 | if (use_memcpy) { |
| 9583 | for (i = 0; i < seqlen; ++i) { |
| 9584 | Py_ssize_t itemlen; |
| 9585 | item = items[i]; |
| 9586 | |
| 9587 | /* Copy item, and maybe the separator. */ |
| 9588 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9589 | Py_MEMCPY(res_data, |
| 9590 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9591 | kind * seplen); |
| 9592 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9593 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9594 | |
| 9595 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9596 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9597 | Py_MEMCPY(res_data, |
| 9598 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9599 | kind * itemlen); |
| 9600 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9601 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9602 | } |
| 9603 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9604 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9605 | } |
| 9606 | else { |
| 9607 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9608 | Py_ssize_t itemlen; |
| 9609 | item = items[i]; |
| 9610 | |
| 9611 | /* Copy item, and maybe the separator. */ |
| 9612 | if (i && seplen != 0) { |
| 9613 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9614 | res_offset += seplen; |
| 9615 | } |
| 9616 | |
| 9617 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9618 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9619 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9620 | res_offset += itemlen; |
| 9621 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9622 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9623 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9624 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9625 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9626 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9627 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9628 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9629 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9630 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9631 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9632 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9634 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | return NULL; |
| 9636 | } |
| 9637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9638 | #define FILL(kind, data, value, start, length) \ |
| 9639 | do { \ |
| 9640 | Py_ssize_t i_ = 0; \ |
| 9641 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9642 | switch ((kind)) { \ |
| 9643 | case PyUnicode_1BYTE_KIND: { \ |
| 9644 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9645 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9646 | break; \ |
| 9647 | } \ |
| 9648 | case PyUnicode_2BYTE_KIND: { \ |
| 9649 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9650 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9651 | break; \ |
| 9652 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9653 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9654 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9655 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9656 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9657 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9658 | } \ |
| 9659 | } \ |
| 9660 | } while (0) |
| 9661 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9662 | void |
| 9663 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9664 | Py_UCS4 fill_char) |
| 9665 | { |
| 9666 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9667 | const void *data = PyUnicode_DATA(unicode); |
| 9668 | assert(PyUnicode_IS_READY(unicode)); |
| 9669 | assert(unicode_modifiable(unicode)); |
| 9670 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9671 | assert(start >= 0); |
| 9672 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9673 | FILL(kind, data, fill_char, start, length); |
| 9674 | } |
| 9675 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9676 | Py_ssize_t |
| 9677 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9678 | Py_UCS4 fill_char) |
| 9679 | { |
| 9680 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9681 | |
| 9682 | if (!PyUnicode_Check(unicode)) { |
| 9683 | PyErr_BadInternalCall(); |
| 9684 | return -1; |
| 9685 | } |
| 9686 | if (PyUnicode_READY(unicode) == -1) |
| 9687 | return -1; |
| 9688 | if (unicode_check_modifiable(unicode)) |
| 9689 | return -1; |
| 9690 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9691 | if (start < 0) { |
| 9692 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9693 | return -1; |
| 9694 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9695 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9696 | PyErr_SetString(PyExc_ValueError, |
| 9697 | "fill character is bigger than " |
| 9698 | "the string maximum character"); |
| 9699 | return -1; |
| 9700 | } |
| 9701 | |
| 9702 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9703 | length = Py_MIN(maxlen, length); |
| 9704 | if (length <= 0) |
| 9705 | return 0; |
| 9706 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9707 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9708 | return length; |
| 9709 | } |
| 9710 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9711 | static PyObject * |
| 9712 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9713 | Py_ssize_t left, |
| 9714 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9715 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9716 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9717 | PyObject *u; |
| 9718 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9719 | int kind; |
| 9720 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9721 | |
| 9722 | if (left < 0) |
| 9723 | left = 0; |
| 9724 | if (right < 0) |
| 9725 | right = 0; |
| 9726 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9727 | if (left == 0 && right == 0) |
| 9728 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9730 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9731 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9732 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9733 | return NULL; |
| 9734 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9735 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9736 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9737 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9738 | if (!u) |
| 9739 | return NULL; |
| 9740 | |
| 9741 | kind = PyUnicode_KIND(u); |
| 9742 | data = PyUnicode_DATA(u); |
| 9743 | if (left) |
| 9744 | FILL(kind, data, fill, 0, left); |
| 9745 | if (right) |
| 9746 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9747 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9748 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9749 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9750 | } |
| 9751 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9752 | PyObject * |
| 9753 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9755 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9756 | |
| 9757 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9758 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9759 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9760 | if (PyUnicode_READY(string) == -1) { |
| 9761 | Py_DECREF(string); |
| 9762 | return NULL; |
| 9763 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9764 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9765 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9766 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9767 | if (PyUnicode_IS_ASCII(string)) |
| 9768 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9769 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9770 | PyUnicode_GET_LENGTH(string), keepends); |
| 9771 | else |
| 9772 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9773 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9774 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9775 | break; |
| 9776 | case PyUnicode_2BYTE_KIND: |
| 9777 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9778 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 | PyUnicode_GET_LENGTH(string), keepends); |
| 9780 | break; |
| 9781 | case PyUnicode_4BYTE_KIND: |
| 9782 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9783 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9784 | PyUnicode_GET_LENGTH(string), keepends); |
| 9785 | break; |
| 9786 | default: |
| 9787 | assert(0); |
| 9788 | list = 0; |
| 9789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9790 | Py_DECREF(string); |
| 9791 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9792 | } |
| 9793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9794 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9795 | split(PyObject *self, |
| 9796 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9797 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9798 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9799 | int kind1, kind2, kind; |
| 9800 | void *buf1, *buf2; |
| 9801 | Py_ssize_t len1, len2; |
| 9802 | PyObject* out; |
| 9803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9804 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9805 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9807 | if (PyUnicode_READY(self) == -1) |
| 9808 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9811 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9813 | if (PyUnicode_IS_ASCII(self)) |
| 9814 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9815 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9816 | PyUnicode_GET_LENGTH(self), maxcount |
| 9817 | ); |
| 9818 | else |
| 9819 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9820 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9821 | PyUnicode_GET_LENGTH(self), maxcount |
| 9822 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9823 | case PyUnicode_2BYTE_KIND: |
| 9824 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9825 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | PyUnicode_GET_LENGTH(self), maxcount |
| 9827 | ); |
| 9828 | case PyUnicode_4BYTE_KIND: |
| 9829 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9830 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | PyUnicode_GET_LENGTH(self), maxcount |
| 9832 | ); |
| 9833 | default: |
| 9834 | assert(0); |
| 9835 | return NULL; |
| 9836 | } |
| 9837 | |
| 9838 | if (PyUnicode_READY(substring) == -1) |
| 9839 | return NULL; |
| 9840 | |
| 9841 | kind1 = PyUnicode_KIND(self); |
| 9842 | kind2 = PyUnicode_KIND(substring); |
| 9843 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9844 | buf1 = PyUnicode_DATA(self); |
| 9845 | buf2 = PyUnicode_DATA(substring); |
| 9846 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9847 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | if (!buf1) |
| 9849 | return NULL; |
| 9850 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9851 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | if (!buf2) { |
| 9853 | if (kind1 != kind) PyMem_Free(buf1); |
| 9854 | return NULL; |
| 9855 | } |
| 9856 | len1 = PyUnicode_GET_LENGTH(self); |
| 9857 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9858 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9859 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9860 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9861 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9862 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9863 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9864 | else |
| 9865 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9866 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | break; |
| 9868 | case PyUnicode_2BYTE_KIND: |
| 9869 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9870 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9871 | break; |
| 9872 | case PyUnicode_4BYTE_KIND: |
| 9873 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9874 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | break; |
| 9876 | default: |
| 9877 | out = NULL; |
| 9878 | } |
| 9879 | if (kind1 != kind) |
| 9880 | PyMem_Free(buf1); |
| 9881 | if (kind2 != kind) |
| 9882 | PyMem_Free(buf2); |
| 9883 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9884 | } |
| 9885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9886 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9887 | rsplit(PyObject *self, |
| 9888 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9889 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | int kind1, kind2, kind; |
| 9892 | void *buf1, *buf2; |
| 9893 | Py_ssize_t len1, len2; |
| 9894 | PyObject* out; |
| 9895 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9896 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9897 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | if (PyUnicode_READY(self) == -1) |
| 9900 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9901 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9902 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9903 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9905 | if (PyUnicode_IS_ASCII(self)) |
| 9906 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9907 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9908 | PyUnicode_GET_LENGTH(self), maxcount |
| 9909 | ); |
| 9910 | else |
| 9911 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9912 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9913 | PyUnicode_GET_LENGTH(self), maxcount |
| 9914 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | case PyUnicode_2BYTE_KIND: |
| 9916 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9917 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9918 | PyUnicode_GET_LENGTH(self), maxcount |
| 9919 | ); |
| 9920 | case PyUnicode_4BYTE_KIND: |
| 9921 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9922 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9923 | PyUnicode_GET_LENGTH(self), maxcount |
| 9924 | ); |
| 9925 | default: |
| 9926 | assert(0); |
| 9927 | return NULL; |
| 9928 | } |
| 9929 | |
| 9930 | if (PyUnicode_READY(substring) == -1) |
| 9931 | return NULL; |
| 9932 | |
| 9933 | kind1 = PyUnicode_KIND(self); |
| 9934 | kind2 = PyUnicode_KIND(substring); |
| 9935 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9936 | buf1 = PyUnicode_DATA(self); |
| 9937 | buf2 = PyUnicode_DATA(substring); |
| 9938 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9939 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9940 | if (!buf1) |
| 9941 | return NULL; |
| 9942 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9943 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9944 | if (!buf2) { |
| 9945 | if (kind1 != kind) PyMem_Free(buf1); |
| 9946 | return NULL; |
| 9947 | } |
| 9948 | len1 = PyUnicode_GET_LENGTH(self); |
| 9949 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9950 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9951 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9952 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9953 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9954 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9955 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9956 | else |
| 9957 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9958 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | break; |
| 9960 | case PyUnicode_2BYTE_KIND: |
| 9961 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9962 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | break; |
| 9964 | case PyUnicode_4BYTE_KIND: |
| 9965 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9966 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9967 | break; |
| 9968 | default: |
| 9969 | out = NULL; |
| 9970 | } |
| 9971 | if (kind1 != kind) |
| 9972 | PyMem_Free(buf1); |
| 9973 | if (kind2 != kind) |
| 9974 | PyMem_Free(buf2); |
| 9975 | return out; |
| 9976 | } |
| 9977 | |
| 9978 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9979 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9980 | 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] | 9981 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9982 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9984 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9985 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9986 | else |
| 9987 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9988 | case PyUnicode_2BYTE_KIND: |
| 9989 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9990 | case PyUnicode_4BYTE_KIND: |
| 9991 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9992 | } |
| 9993 | assert(0); |
| 9994 | return -1; |
| 9995 | } |
| 9996 | |
| 9997 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9998 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9999 | 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] | 10000 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10001 | switch (kind) { |
| 10002 | case PyUnicode_1BYTE_KIND: |
| 10003 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10004 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10005 | else |
| 10006 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10007 | case PyUnicode_2BYTE_KIND: |
| 10008 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10009 | case PyUnicode_4BYTE_KIND: |
| 10010 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10011 | } |
| 10012 | assert(0); |
| 10013 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10014 | } |
| 10015 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10016 | static void |
| 10017 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10018 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10019 | { |
| 10020 | int kind = PyUnicode_KIND(u); |
| 10021 | void *data = PyUnicode_DATA(u); |
| 10022 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10023 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10024 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10025 | (Py_UCS1 *)data + len, |
| 10026 | u1, u2, maxcount); |
| 10027 | } |
| 10028 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10029 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10030 | (Py_UCS2 *)data + len, |
| 10031 | u1, u2, maxcount); |
| 10032 | } |
| 10033 | else { |
| 10034 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10035 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10036 | (Py_UCS4 *)data + len, |
| 10037 | u1, u2, maxcount); |
| 10038 | } |
| 10039 | } |
| 10040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10041 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10042 | replace(PyObject *self, PyObject *str1, |
| 10043 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10044 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | PyObject *u; |
| 10046 | char *sbuf = PyUnicode_DATA(self); |
| 10047 | char *buf1 = PyUnicode_DATA(str1); |
| 10048 | char *buf2 = PyUnicode_DATA(str2); |
| 10049 | int srelease = 0, release1 = 0, release2 = 0; |
| 10050 | int skind = PyUnicode_KIND(self); |
| 10051 | int kind1 = PyUnicode_KIND(str1); |
| 10052 | int kind2 = PyUnicode_KIND(str2); |
| 10053 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10054 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10055 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10056 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10057 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10058 | |
| 10059 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10060 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10062 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10063 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10064 | if (str1 == str2) |
| 10065 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10067 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10068 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10069 | if (maxchar < maxchar_str1) |
| 10070 | /* substring too wide to be present */ |
| 10071 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10072 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10073 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10074 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10075 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10076 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10079 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10081 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10083 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10084 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10085 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10086 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10087 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10088 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10089 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10090 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10091 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10092 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10093 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10094 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10095 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10096 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10097 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10098 | } |
| 10099 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | int rkind = skind; |
| 10101 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10102 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | if (kind1 < rkind) { |
| 10105 | /* widen substring */ |
| 10106 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10107 | if (!buf1) goto error; |
| 10108 | release1 = 1; |
| 10109 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10110 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10111 | if (i < 0) |
| 10112 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10113 | if (rkind > kind2) { |
| 10114 | /* widen replacement */ |
| 10115 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10116 | if (!buf2) goto error; |
| 10117 | release2 = 1; |
| 10118 | } |
| 10119 | else if (rkind < kind2) { |
| 10120 | /* widen self and buf1 */ |
| 10121 | rkind = kind2; |
| 10122 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10123 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10124 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10125 | if (!sbuf) goto error; |
| 10126 | srelease = 1; |
| 10127 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10128 | if (!buf1) goto error; |
| 10129 | release1 = 1; |
| 10130 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10131 | u = PyUnicode_New(slen, maxchar); |
| 10132 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10133 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10134 | assert(PyUnicode_KIND(u) == rkind); |
| 10135 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10136 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10137 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10138 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10139 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10140 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10141 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10143 | |
| 10144 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10145 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10146 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10147 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10148 | if (i == -1) |
| 10149 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10150 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10152 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10154 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10155 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10156 | } |
| 10157 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10158 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10159 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | int rkind = skind; |
| 10161 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10162 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10164 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10166 | if (!buf1) goto error; |
| 10167 | release1 = 1; |
| 10168 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10169 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10170 | if (n == 0) |
| 10171 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10173 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10175 | if (!buf2) goto error; |
| 10176 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10177 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10179 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | rkind = kind2; |
| 10181 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10182 | if (!sbuf) goto error; |
| 10183 | srelease = 1; |
| 10184 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10185 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10187 | if (!buf1) goto error; |
| 10188 | release1 = 1; |
| 10189 | } |
| 10190 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10191 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10192 | 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] | 10193 | PyErr_SetString(PyExc_OverflowError, |
| 10194 | "replace string is too long"); |
| 10195 | goto error; |
| 10196 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10197 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10198 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10199 | _Py_INCREF_UNICODE_EMPTY(); |
| 10200 | if (!unicode_empty) |
| 10201 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10202 | u = unicode_empty; |
| 10203 | goto done; |
| 10204 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10205 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | PyErr_SetString(PyExc_OverflowError, |
| 10207 | "replace string is too long"); |
| 10208 | goto error; |
| 10209 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10210 | u = PyUnicode_New(new_size, maxchar); |
| 10211 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10212 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10213 | assert(PyUnicode_KIND(u) == rkind); |
| 10214 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | ires = i = 0; |
| 10216 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10217 | while (n-- > 0) { |
| 10218 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10219 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10220 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10221 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10222 | if (j == -1) |
| 10223 | break; |
| 10224 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10225 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10226 | memcpy(res + rkind * ires, |
| 10227 | sbuf + rkind * i, |
| 10228 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10230 | } |
| 10231 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10233 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10235 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10237 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10239 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10240 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10241 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10242 | memcpy(res + rkind * ires, |
| 10243 | sbuf + rkind * i, |
| 10244 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10245 | } |
| 10246 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10247 | /* interleave */ |
| 10248 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10249 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10251 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10252 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10253 | if (--n <= 0) |
| 10254 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10255 | memcpy(res + rkind * ires, |
| 10256 | sbuf + rkind * i, |
| 10257 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | ires++; |
| 10259 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10260 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10261 | memcpy(res + rkind * ires, |
| 10262 | sbuf + rkind * i, |
| 10263 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10264 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10265 | } |
| 10266 | |
| 10267 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10268 | unicode_adjust_maxchar(&u); |
| 10269 | if (u == NULL) |
| 10270 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10271 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10272 | |
| 10273 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10274 | if (srelease) |
| 10275 | PyMem_FREE(sbuf); |
| 10276 | if (release1) |
| 10277 | PyMem_FREE(buf1); |
| 10278 | if (release2) |
| 10279 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10280 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10282 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10283 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10284 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (srelease) |
| 10286 | PyMem_FREE(sbuf); |
| 10287 | if (release1) |
| 10288 | PyMem_FREE(buf1); |
| 10289 | if (release2) |
| 10290 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10291 | return unicode_result_unchanged(self); |
| 10292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | error: |
| 10294 | if (srelease && sbuf) |
| 10295 | PyMem_FREE(sbuf); |
| 10296 | if (release1 && buf1) |
| 10297 | PyMem_FREE(buf1); |
| 10298 | if (release2 && buf2) |
| 10299 | PyMem_FREE(buf2); |
| 10300 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10301 | } |
| 10302 | |
| 10303 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10305 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10306 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10307 | \n\ |
| 10308 | 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] | 10309 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10310 | |
| 10311 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10312 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10314 | if (PyUnicode_READY(self) == -1) |
| 10315 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10316 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10317 | } |
| 10318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10319 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10320 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10321 | \n\ |
| 10322 | 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] | 10323 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10324 | |
| 10325 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10326 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10327 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10328 | if (PyUnicode_READY(self) == -1) |
| 10329 | return NULL; |
| 10330 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10331 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10332 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10333 | } |
| 10334 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10335 | PyDoc_STRVAR(casefold__doc__, |
| 10336 | "S.casefold() -> str\n\ |
| 10337 | \n\ |
| 10338 | Return a version of S suitable for caseless comparisons."); |
| 10339 | |
| 10340 | static PyObject * |
| 10341 | unicode_casefold(PyObject *self) |
| 10342 | { |
| 10343 | if (PyUnicode_READY(self) == -1) |
| 10344 | return NULL; |
| 10345 | if (PyUnicode_IS_ASCII(self)) |
| 10346 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10347 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10351 | /* Argument converter. Coerces to a single unicode character */ |
| 10352 | |
| 10353 | static int |
| 10354 | convert_uc(PyObject *obj, void *addr) |
| 10355 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10356 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10357 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10358 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10359 | uniobj = PyUnicode_FromObject(obj); |
| 10360 | if (uniobj == NULL) { |
| 10361 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10362 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10363 | return 0; |
| 10364 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10365 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10366 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10367 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10368 | Py_DECREF(uniobj); |
| 10369 | return 0; |
| 10370 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10372 | Py_DECREF(uniobj); |
| 10373 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10374 | } |
| 10375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10376 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10377 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10378 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10379 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10380 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | |
| 10382 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10383 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10385 | Py_ssize_t marg, left; |
| 10386 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10387 | Py_UCS4 fillchar = ' '; |
| 10388 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10389 | 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] | 10390 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10391 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10392 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | return NULL; |
| 10394 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10395 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10396 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10397 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10398 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10399 | left = marg / 2 + (marg & width & 1); |
| 10400 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10401 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10402 | } |
| 10403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10404 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10405 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10406 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10407 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10408 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10409 | #define COMPARE(TYPE1, TYPE2) \ |
| 10410 | do { \ |
| 10411 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10412 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10413 | TYPE1* end = p1 + len; \ |
| 10414 | Py_UCS4 c1, c2; \ |
| 10415 | for (; p1 != end; p1++, p2++) { \ |
| 10416 | c1 = *p1; \ |
| 10417 | c2 = *p2; \ |
| 10418 | if (c1 != c2) \ |
| 10419 | return (c1 < c2) ? -1 : 1; \ |
| 10420 | } \ |
| 10421 | } \ |
| 10422 | while (0) |
| 10423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10424 | int kind1, kind2; |
| 10425 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10426 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10427 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10428 | /* a string is equal to itself */ |
| 10429 | if (str1 == str2) |
| 10430 | return 0; |
| 10431 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | kind1 = PyUnicode_KIND(str1); |
| 10433 | kind2 = PyUnicode_KIND(str2); |
| 10434 | data1 = PyUnicode_DATA(str1); |
| 10435 | data2 = PyUnicode_DATA(str2); |
| 10436 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10437 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10438 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10439 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10440 | switch(kind1) { |
| 10441 | case PyUnicode_1BYTE_KIND: |
| 10442 | { |
| 10443 | switch(kind2) { |
| 10444 | case PyUnicode_1BYTE_KIND: |
| 10445 | { |
| 10446 | int cmp = memcmp(data1, data2, len); |
| 10447 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10448 | if (cmp < 0) |
| 10449 | return -1; |
| 10450 | if (cmp > 0) |
| 10451 | return 1; |
| 10452 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10453 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10454 | case PyUnicode_2BYTE_KIND: |
| 10455 | COMPARE(Py_UCS1, Py_UCS2); |
| 10456 | break; |
| 10457 | case PyUnicode_4BYTE_KIND: |
| 10458 | COMPARE(Py_UCS1, Py_UCS4); |
| 10459 | break; |
| 10460 | default: |
| 10461 | assert(0); |
| 10462 | } |
| 10463 | break; |
| 10464 | } |
| 10465 | case PyUnicode_2BYTE_KIND: |
| 10466 | { |
| 10467 | switch(kind2) { |
| 10468 | case PyUnicode_1BYTE_KIND: |
| 10469 | COMPARE(Py_UCS2, Py_UCS1); |
| 10470 | break; |
| 10471 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10472 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10473 | COMPARE(Py_UCS2, Py_UCS2); |
| 10474 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10475 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10476 | case PyUnicode_4BYTE_KIND: |
| 10477 | COMPARE(Py_UCS2, Py_UCS4); |
| 10478 | break; |
| 10479 | default: |
| 10480 | assert(0); |
| 10481 | } |
| 10482 | break; |
| 10483 | } |
| 10484 | case PyUnicode_4BYTE_KIND: |
| 10485 | { |
| 10486 | switch(kind2) { |
| 10487 | case PyUnicode_1BYTE_KIND: |
| 10488 | COMPARE(Py_UCS4, Py_UCS1); |
| 10489 | break; |
| 10490 | case PyUnicode_2BYTE_KIND: |
| 10491 | COMPARE(Py_UCS4, Py_UCS2); |
| 10492 | break; |
| 10493 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10494 | { |
| 10495 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10496 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10497 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10498 | if (cmp < 0) |
| 10499 | return -1; |
| 10500 | if (cmp > 0) |
| 10501 | return 1; |
| 10502 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10503 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10504 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10505 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10506 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10507 | default: |
| 10508 | assert(0); |
| 10509 | } |
| 10510 | break; |
| 10511 | } |
| 10512 | default: |
| 10513 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10516 | if (len1 == len2) |
| 10517 | return 0; |
| 10518 | if (len1 < len2) |
| 10519 | return -1; |
| 10520 | else |
| 10521 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10522 | |
| 10523 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10524 | } |
| 10525 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10526 | static int |
| 10527 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10528 | { |
| 10529 | int kind; |
| 10530 | void *data1, *data2; |
| 10531 | Py_ssize_t len; |
| 10532 | int cmp; |
| 10533 | |
| 10534 | /* a string is equal to itself */ |
| 10535 | if (str1 == str2) |
| 10536 | return 1; |
| 10537 | |
| 10538 | len = PyUnicode_GET_LENGTH(str1); |
| 10539 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10540 | return 0; |
| 10541 | kind = PyUnicode_KIND(str1); |
| 10542 | if (PyUnicode_KIND(str2) != kind) |
| 10543 | return 0; |
| 10544 | data1 = PyUnicode_DATA(str1); |
| 10545 | data2 = PyUnicode_DATA(str2); |
| 10546 | |
| 10547 | cmp = memcmp(data1, data2, len * kind); |
| 10548 | return (cmp == 0); |
| 10549 | } |
| 10550 | |
| 10551 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10552 | int |
| 10553 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10554 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10556 | if (PyUnicode_READY(left) == -1 || |
| 10557 | PyUnicode_READY(right) == -1) |
| 10558 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10559 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10560 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10561 | PyErr_Format(PyExc_TypeError, |
| 10562 | "Can't compare %.100s and %.100s", |
| 10563 | left->ob_type->tp_name, |
| 10564 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10565 | return -1; |
| 10566 | } |
| 10567 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10568 | int |
| 10569 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10570 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | Py_ssize_t i; |
| 10572 | int kind; |
| 10573 | void *data; |
| 10574 | Py_UCS4 chr; |
| 10575 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10576 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10577 | if (PyUnicode_READY(uni) == -1) |
| 10578 | return -1; |
| 10579 | kind = PyUnicode_KIND(uni); |
| 10580 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10581 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10582 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10583 | if (chr != str[i]) |
| 10584 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10585 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10586 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10587 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10588 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10589 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10591 | return 0; |
| 10592 | } |
| 10593 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10595 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10596 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10597 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10598 | PyObject * |
| 10599 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10600 | { |
| 10601 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10602 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10603 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10604 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10605 | Py_RETURN_NOTIMPLEMENTED; |
| 10606 | |
| 10607 | if (PyUnicode_READY(left) == -1 || |
| 10608 | PyUnicode_READY(right) == -1) |
| 10609 | return NULL; |
| 10610 | |
| 10611 | if (op == Py_EQ || op == Py_NE) { |
| 10612 | result = unicode_compare_eq(left, right); |
| 10613 | if (op == Py_EQ) |
| 10614 | v = TEST_COND(result); |
| 10615 | else |
| 10616 | v = TEST_COND(!result); |
| 10617 | } |
| 10618 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10619 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10620 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10621 | /* Convert the return value to a Boolean */ |
| 10622 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10623 | case Py_LE: |
| 10624 | v = TEST_COND(result <= 0); |
| 10625 | break; |
| 10626 | case Py_GE: |
| 10627 | v = TEST_COND(result >= 0); |
| 10628 | break; |
| 10629 | case Py_LT: |
| 10630 | v = TEST_COND(result == -1); |
| 10631 | break; |
| 10632 | case Py_GT: |
| 10633 | v = TEST_COND(result == 1); |
| 10634 | break; |
| 10635 | default: |
| 10636 | PyErr_BadArgument(); |
| 10637 | return NULL; |
| 10638 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10639 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10640 | Py_INCREF(v); |
| 10641 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10642 | } |
| 10643 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10644 | int |
| 10645 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10646 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10647 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10648 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10649 | void *buf1, *buf2; |
| 10650 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10651 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10652 | |
| 10653 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | sub = PyUnicode_FromObject(element); |
| 10655 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10656 | PyErr_Format(PyExc_TypeError, |
| 10657 | "'in <string>' requires string as left operand, not %s", |
| 10658 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10659 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10660 | } |
| 10661 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10662 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10663 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10664 | Py_DECREF(sub); |
| 10665 | return -1; |
| 10666 | } |
| 10667 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10668 | kind1 = PyUnicode_KIND(str); |
| 10669 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10670 | buf1 = PyUnicode_DATA(str); |
| 10671 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10672 | if (kind2 != kind1) { |
| 10673 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10674 | Py_DECREF(sub); |
| 10675 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10676 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10677 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10678 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10679 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (!buf2) { |
| 10681 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10682 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10683 | return -1; |
| 10684 | } |
| 10685 | len1 = PyUnicode_GET_LENGTH(str); |
| 10686 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10687 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10688 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | case PyUnicode_1BYTE_KIND: |
| 10690 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10691 | break; |
| 10692 | case PyUnicode_2BYTE_KIND: |
| 10693 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10694 | break; |
| 10695 | case PyUnicode_4BYTE_KIND: |
| 10696 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10697 | break; |
| 10698 | default: |
| 10699 | result = -1; |
| 10700 | assert(0); |
| 10701 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10702 | |
| 10703 | Py_DECREF(str); |
| 10704 | Py_DECREF(sub); |
| 10705 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10706 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10707 | PyMem_Free(buf2); |
| 10708 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10709 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10713 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10714 | PyObject * |
| 10715 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10718 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10719 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | |
| 10721 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10724 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10725 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10726 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10727 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | |
| 10729 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10730 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10731 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10732 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10733 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10734 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10735 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10736 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10739 | u_len = PyUnicode_GET_LENGTH(u); |
| 10740 | v_len = PyUnicode_GET_LENGTH(v); |
| 10741 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10742 | PyErr_SetString(PyExc_OverflowError, |
| 10743 | "strings are too large to concat"); |
| 10744 | goto onError; |
| 10745 | } |
| 10746 | new_len = u_len + v_len; |
| 10747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10748 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10749 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10750 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10751 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10752 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10753 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10754 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10755 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10756 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10757 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10758 | Py_DECREF(u); |
| 10759 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10760 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10761 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10763 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10764 | Py_XDECREF(u); |
| 10765 | Py_XDECREF(v); |
| 10766 | return NULL; |
| 10767 | } |
| 10768 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10769 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10770 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10771 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10772 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10773 | Py_UCS4 maxchar, maxchar2; |
| 10774 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10775 | |
| 10776 | if (p_left == NULL) { |
| 10777 | if (!PyErr_Occurred()) |
| 10778 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10779 | return; |
| 10780 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10781 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10782 | if (right == NULL || left == NULL |
| 10783 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10784 | if (!PyErr_Occurred()) |
| 10785 | PyErr_BadInternalCall(); |
| 10786 | goto error; |
| 10787 | } |
| 10788 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10789 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10790 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10791 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10792 | goto error; |
| 10793 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10794 | /* Shortcuts */ |
| 10795 | if (left == unicode_empty) { |
| 10796 | Py_DECREF(left); |
| 10797 | Py_INCREF(right); |
| 10798 | *p_left = right; |
| 10799 | return; |
| 10800 | } |
| 10801 | if (right == unicode_empty) |
| 10802 | return; |
| 10803 | |
| 10804 | left_len = PyUnicode_GET_LENGTH(left); |
| 10805 | right_len = PyUnicode_GET_LENGTH(right); |
| 10806 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10807 | PyErr_SetString(PyExc_OverflowError, |
| 10808 | "strings are too large to concat"); |
| 10809 | goto error; |
| 10810 | } |
| 10811 | new_len = left_len + right_len; |
| 10812 | |
| 10813 | if (unicode_modifiable(left) |
| 10814 | && PyUnicode_CheckExact(right) |
| 10815 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10816 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10817 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10818 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10819 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10820 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10821 | { |
| 10822 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10823 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10824 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10825 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10826 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10827 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10828 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10829 | else { |
| 10830 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10831 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10832 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10833 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10834 | /* Concat the two Unicode strings */ |
| 10835 | res = PyUnicode_New(new_len, maxchar); |
| 10836 | if (res == NULL) |
| 10837 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10838 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10839 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10840 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10841 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10842 | } |
| 10843 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10844 | return; |
| 10845 | |
| 10846 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10847 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10848 | } |
| 10849 | |
| 10850 | void |
| 10851 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10852 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10853 | PyUnicode_Append(pleft, right); |
| 10854 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10855 | } |
| 10856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10857 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10858 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10859 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10860 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10861 | 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] | 10862 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10863 | |
| 10864 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10865 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10866 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10867 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10868 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10869 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10870 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10871 | int kind1, kind2, kind; |
| 10872 | void *buf1, *buf2; |
| 10873 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10874 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10875 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10876 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10877 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10878 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10879 | kind1 = PyUnicode_KIND(self); |
| 10880 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10881 | if (kind2 > kind1) { |
| 10882 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10883 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10884 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10885 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10886 | buf1 = PyUnicode_DATA(self); |
| 10887 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10888 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10889 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10890 | if (!buf2) { |
| 10891 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | return NULL; |
| 10893 | } |
| 10894 | len1 = PyUnicode_GET_LENGTH(self); |
| 10895 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10896 | |
| 10897 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10898 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10899 | case PyUnicode_1BYTE_KIND: |
| 10900 | iresult = ucs1lib_count( |
| 10901 | ((Py_UCS1*)buf1) + start, end - start, |
| 10902 | buf2, len2, PY_SSIZE_T_MAX |
| 10903 | ); |
| 10904 | break; |
| 10905 | case PyUnicode_2BYTE_KIND: |
| 10906 | iresult = ucs2lib_count( |
| 10907 | ((Py_UCS2*)buf1) + start, end - start, |
| 10908 | buf2, len2, PY_SSIZE_T_MAX |
| 10909 | ); |
| 10910 | break; |
| 10911 | case PyUnicode_4BYTE_KIND: |
| 10912 | iresult = ucs4lib_count( |
| 10913 | ((Py_UCS4*)buf1) + start, end - start, |
| 10914 | buf2, len2, PY_SSIZE_T_MAX |
| 10915 | ); |
| 10916 | break; |
| 10917 | default: |
| 10918 | assert(0); iresult = 0; |
| 10919 | } |
| 10920 | |
| 10921 | result = PyLong_FromSsize_t(iresult); |
| 10922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10923 | if (kind2 != kind) |
| 10924 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | |
| 10926 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10928 | return result; |
| 10929 | } |
| 10930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10931 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10932 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10934 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10935 | 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] | 10936 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10937 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10938 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10939 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | |
| 10941 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10942 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10943 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10944 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10945 | char *encoding = NULL; |
| 10946 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10947 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10948 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10949 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10951 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10952 | } |
| 10953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10954 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10955 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | \n\ |
| 10957 | 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] | 10958 | 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] | 10959 | |
| 10960 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10961 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10963 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10964 | Py_UCS4 ch; |
| 10965 | PyObject *u; |
| 10966 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10967 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10968 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10969 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | |
| 10971 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10972 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10974 | if (PyUnicode_READY(self) == -1) |
| 10975 | return NULL; |
| 10976 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10977 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10978 | src_len = PyUnicode_GET_LENGTH(self); |
| 10979 | i = j = line_pos = 0; |
| 10980 | kind = PyUnicode_KIND(self); |
| 10981 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10982 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10983 | for (; i < src_len; i++) { |
| 10984 | ch = PyUnicode_READ(kind, src_data, i); |
| 10985 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10986 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10987 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10988 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10989 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10990 | goto overflow; |
| 10991 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10992 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10993 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10994 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10996 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10997 | goto overflow; |
| 10998 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10999 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11000 | if (ch == '\n' || ch == '\r') |
| 11001 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11003 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11004 | if (!found) |
| 11005 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11007 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11008 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | if (!u) |
| 11010 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11011 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11013 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11015 | for (; i < src_len; i++) { |
| 11016 | ch = PyUnicode_READ(kind, src_data, i); |
| 11017 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11018 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11019 | incr = tabsize - (line_pos % tabsize); |
| 11020 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11021 | FILL(kind, dest_data, ' ', j, incr); |
| 11022 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11023 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11024 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11025 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11026 | line_pos++; |
| 11027 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11028 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11029 | if (ch == '\n' || ch == '\r') |
| 11030 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11032 | } |
| 11033 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11034 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11035 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11036 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11037 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11038 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | } |
| 11040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11041 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11042 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | \n\ |
| 11044 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11045 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | arguments start and end are interpreted as in slice notation.\n\ |
| 11047 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11048 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | |
| 11050 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11053 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11054 | Py_ssize_t start; |
| 11055 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11056 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11058 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11059 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11061 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11062 | if (PyUnicode_READY(self) == -1) { |
| 11063 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11064 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11065 | } |
| 11066 | if (PyUnicode_READY(substring) == -1) { |
| 11067 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11069 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11070 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11071 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | |
| 11073 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11075 | if (result == -2) |
| 11076 | return NULL; |
| 11077 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11078 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | } |
| 11080 | |
| 11081 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11082 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11084 | void *data; |
| 11085 | enum PyUnicode_Kind kind; |
| 11086 | Py_UCS4 ch; |
| 11087 | PyObject *res; |
| 11088 | |
| 11089 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11090 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11091 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11092 | } |
| 11093 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11094 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11095 | return NULL; |
| 11096 | } |
| 11097 | kind = PyUnicode_KIND(self); |
| 11098 | data = PyUnicode_DATA(self); |
| 11099 | ch = PyUnicode_READ(kind, data, index); |
| 11100 | if (ch < 256) |
| 11101 | return get_latin1_char(ch); |
| 11102 | |
| 11103 | res = PyUnicode_New(1, ch); |
| 11104 | if (res == NULL) |
| 11105 | return NULL; |
| 11106 | kind = PyUnicode_KIND(res); |
| 11107 | data = PyUnicode_DATA(res); |
| 11108 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11109 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11110 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11111 | } |
| 11112 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11113 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11114 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11115 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11116 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11117 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11118 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11119 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11120 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11121 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11122 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11123 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | if (_PyUnicode_HASH(self) != -1) |
| 11125 | return _PyUnicode_HASH(self); |
| 11126 | if (PyUnicode_READY(self) == -1) |
| 11127 | return -1; |
| 11128 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11129 | /* |
| 11130 | We make the hash of the empty string be 0, rather than using |
| 11131 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11132 | */ |
| 11133 | if (len == 0) { |
| 11134 | _PyUnicode_HASH(self) = 0; |
| 11135 | return 0; |
| 11136 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11137 | |
| 11138 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11139 | #define HASH(P) \ |
| 11140 | x ^= (Py_uhash_t) *P << 7; \ |
| 11141 | while (--len >= 0) \ |
| 11142 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11144 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 | switch (PyUnicode_KIND(self)) { |
| 11146 | case PyUnicode_1BYTE_KIND: { |
| 11147 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11148 | HASH(c); |
| 11149 | break; |
| 11150 | } |
| 11151 | case PyUnicode_2BYTE_KIND: { |
| 11152 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11153 | HASH(s); |
| 11154 | break; |
| 11155 | } |
| 11156 | default: { |
| 11157 | Py_UCS4 *l; |
| 11158 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11159 | "Impossible switch case in unicode_hash"); |
| 11160 | l = PyUnicode_4BYTE_DATA(self); |
| 11161 | HASH(l); |
| 11162 | break; |
| 11163 | } |
| 11164 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11165 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11166 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11167 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11168 | if (x == -1) |
| 11169 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11171 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11173 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11175 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11176 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11178 | 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] | 11179 | |
| 11180 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11181 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11182 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11183 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11184 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11185 | Py_ssize_t start; |
| 11186 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11188 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11189 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11192 | if (PyUnicode_READY(self) == -1) { |
| 11193 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11195 | } |
| 11196 | if (PyUnicode_READY(substring) == -1) { |
| 11197 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11199 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11200 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11201 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | |
| 11203 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | if (result == -2) |
| 11206 | return NULL; |
| 11207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | if (result < 0) { |
| 11209 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11210 | return NULL; |
| 11211 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11212 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11213 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11214 | } |
| 11215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11216 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11217 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11219 | 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] | 11220 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
| 11222 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11223 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11225 | Py_ssize_t i, length; |
| 11226 | int kind; |
| 11227 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | int cased; |
| 11229 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | if (PyUnicode_READY(self) == -1) |
| 11231 | return NULL; |
| 11232 | length = PyUnicode_GET_LENGTH(self); |
| 11233 | kind = PyUnicode_KIND(self); |
| 11234 | data = PyUnicode_DATA(self); |
| 11235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11237 | if (length == 1) |
| 11238 | return PyBool_FromLong( |
| 11239 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11241 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11242 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11243 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | for (i = 0; i < length; i++) { |
| 11247 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11248 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11249 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11250 | return PyBool_FromLong(0); |
| 11251 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11252 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11254 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | } |
| 11256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11257 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11258 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11260 | Return True if 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] | 11261 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | |
| 11263 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11264 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11266 | Py_ssize_t i, length; |
| 11267 | int kind; |
| 11268 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | int cased; |
| 11270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11271 | if (PyUnicode_READY(self) == -1) |
| 11272 | return NULL; |
| 11273 | length = PyUnicode_GET_LENGTH(self); |
| 11274 | kind = PyUnicode_KIND(self); |
| 11275 | data = PyUnicode_DATA(self); |
| 11276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11277 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | if (length == 1) |
| 11279 | return PyBool_FromLong( |
| 11280 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11282 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11283 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11284 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11287 | for (i = 0; i < length; i++) { |
| 11288 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11289 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11290 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11291 | return PyBool_FromLong(0); |
| 11292 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11293 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11295 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | } |
| 11297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11301 | Return True if S is a titlecased string and there is at least one\n\ |
| 11302 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11303 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11304 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11305 | |
| 11306 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11307 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11308 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | Py_ssize_t i, length; |
| 11310 | int kind; |
| 11311 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11312 | int cased, previous_is_cased; |
| 11313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | if (PyUnicode_READY(self) == -1) |
| 11315 | return NULL; |
| 11316 | length = PyUnicode_GET_LENGTH(self); |
| 11317 | kind = PyUnicode_KIND(self); |
| 11318 | data = PyUnicode_DATA(self); |
| 11319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11321 | if (length == 1) { |
| 11322 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11323 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11324 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11325 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11327 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11328 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11329 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | cased = 0; |
| 11332 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | for (i = 0; i < length; i++) { |
| 11334 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11335 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11336 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11337 | if (previous_is_cased) |
| 11338 | return PyBool_FromLong(0); |
| 11339 | previous_is_cased = 1; |
| 11340 | cased = 1; |
| 11341 | } |
| 11342 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11343 | if (!previous_is_cased) |
| 11344 | return PyBool_FromLong(0); |
| 11345 | previous_is_cased = 1; |
| 11346 | cased = 1; |
| 11347 | } |
| 11348 | else |
| 11349 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11351 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11352 | } |
| 11353 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11354 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11355 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11357 | Return True if all characters in S are whitespace\n\ |
| 11358 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11359 | |
| 11360 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11361 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11362 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | Py_ssize_t i, length; |
| 11364 | int kind; |
| 11365 | void *data; |
| 11366 | |
| 11367 | if (PyUnicode_READY(self) == -1) |
| 11368 | return NULL; |
| 11369 | length = PyUnicode_GET_LENGTH(self); |
| 11370 | kind = PyUnicode_KIND(self); |
| 11371 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | if (length == 1) |
| 11375 | return PyBool_FromLong( |
| 11376 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11377 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11378 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11379 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11380 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11382 | for (i = 0; i < length; i++) { |
| 11383 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11384 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11385 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11387 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11388 | } |
| 11389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11390 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11391 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11392 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11393 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11394 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11395 | |
| 11396 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11397 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11398 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | Py_ssize_t i, length; |
| 11400 | int kind; |
| 11401 | void *data; |
| 11402 | |
| 11403 | if (PyUnicode_READY(self) == -1) |
| 11404 | return NULL; |
| 11405 | length = PyUnicode_GET_LENGTH(self); |
| 11406 | kind = PyUnicode_KIND(self); |
| 11407 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11408 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11409 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11410 | if (length == 1) |
| 11411 | return PyBool_FromLong( |
| 11412 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11413 | |
| 11414 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11416 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11418 | for (i = 0; i < length; i++) { |
| 11419 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11420 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11421 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11422 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11423 | } |
| 11424 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11425 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11426 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11427 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11428 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11429 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11430 | |
| 11431 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11432 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11434 | int kind; |
| 11435 | void *data; |
| 11436 | Py_ssize_t len, i; |
| 11437 | |
| 11438 | if (PyUnicode_READY(self) == -1) |
| 11439 | return NULL; |
| 11440 | |
| 11441 | kind = PyUnicode_KIND(self); |
| 11442 | data = PyUnicode_DATA(self); |
| 11443 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11444 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11445 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | if (len == 1) { |
| 11447 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11448 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11449 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11450 | |
| 11451 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11452 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11453 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11455 | for (i = 0; i < len; i++) { |
| 11456 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11457 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11458 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11459 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11460 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11461 | } |
| 11462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11463 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11464 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11466 | 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] | 11467 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11468 | |
| 11469 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11470 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11472 | Py_ssize_t i, length; |
| 11473 | int kind; |
| 11474 | void *data; |
| 11475 | |
| 11476 | if (PyUnicode_READY(self) == -1) |
| 11477 | return NULL; |
| 11478 | length = PyUnicode_GET_LENGTH(self); |
| 11479 | kind = PyUnicode_KIND(self); |
| 11480 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | if (length == 1) |
| 11484 | return PyBool_FromLong( |
| 11485 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11487 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11488 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11489 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11490 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11491 | for (i = 0; i < length; i++) { |
| 11492 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11493 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11495 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11496 | } |
| 11497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11498 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11501 | Return True if all characters in S are digits\n\ |
| 11502 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | |
| 11504 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11505 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | Py_ssize_t i, length; |
| 11508 | int kind; |
| 11509 | void *data; |
| 11510 | |
| 11511 | if (PyUnicode_READY(self) == -1) |
| 11512 | return NULL; |
| 11513 | length = PyUnicode_GET_LENGTH(self); |
| 11514 | kind = PyUnicode_KIND(self); |
| 11515 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11518 | if (length == 1) { |
| 11519 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11520 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11521 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11522 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11523 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11525 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | for (i = 0; i < length; i++) { |
| 11528 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11529 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11530 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11531 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | } |
| 11533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11534 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11535 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11537 | 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] | 11538 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | |
| 11540 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11541 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11542 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | Py_ssize_t i, length; |
| 11544 | int kind; |
| 11545 | void *data; |
| 11546 | |
| 11547 | if (PyUnicode_READY(self) == -1) |
| 11548 | return NULL; |
| 11549 | length = PyUnicode_GET_LENGTH(self); |
| 11550 | kind = PyUnicode_KIND(self); |
| 11551 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11554 | if (length == 1) |
| 11555 | return PyBool_FromLong( |
| 11556 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11558 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11559 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11560 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11561 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | for (i = 0; i < length; i++) { |
| 11563 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11564 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11566 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | } |
| 11568 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11569 | int |
| 11570 | PyUnicode_IsIdentifier(PyObject *self) |
| 11571 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11572 | int kind; |
| 11573 | void *data; |
| 11574 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11575 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11576 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11577 | if (PyUnicode_READY(self) == -1) { |
| 11578 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11579 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11580 | } |
| 11581 | |
| 11582 | /* Special case for empty strings */ |
| 11583 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11584 | return 0; |
| 11585 | kind = PyUnicode_KIND(self); |
| 11586 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11587 | |
| 11588 | /* PEP 3131 says that the first character must be in |
| 11589 | XID_Start and subsequent characters in XID_Continue, |
| 11590 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11591 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11592 | letters, digits, underscore). However, given the current |
| 11593 | definition of XID_Start and XID_Continue, it is sufficient |
| 11594 | to check just for these, except that _ must be allowed |
| 11595 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11596 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11597 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11598 | return 0; |
| 11599 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11600 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11601 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11602 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11603 | return 1; |
| 11604 | } |
| 11605 | |
| 11606 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11607 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11608 | \n\ |
| 11609 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11610 | to the language definition.\n\ |
| 11611 | \n\ |
| 11612 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11613 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11614 | |
| 11615 | static PyObject* |
| 11616 | unicode_isidentifier(PyObject *self) |
| 11617 | { |
| 11618 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11619 | } |
| 11620 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11621 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11622 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11623 | \n\ |
| 11624 | Return True if all characters in S are considered\n\ |
| 11625 | printable in repr() or S is empty, False otherwise."); |
| 11626 | |
| 11627 | static PyObject* |
| 11628 | unicode_isprintable(PyObject *self) |
| 11629 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11630 | Py_ssize_t i, length; |
| 11631 | int kind; |
| 11632 | void *data; |
| 11633 | |
| 11634 | if (PyUnicode_READY(self) == -1) |
| 11635 | return NULL; |
| 11636 | length = PyUnicode_GET_LENGTH(self); |
| 11637 | kind = PyUnicode_KIND(self); |
| 11638 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11639 | |
| 11640 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | if (length == 1) |
| 11642 | return PyBool_FromLong( |
| 11643 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11645 | for (i = 0; i < length; i++) { |
| 11646 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11647 | Py_RETURN_FALSE; |
| 11648 | } |
| 11649 | } |
| 11650 | Py_RETURN_TRUE; |
| 11651 | } |
| 11652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11654 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11655 | \n\ |
| 11656 | 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] | 11657 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11658 | |
| 11659 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11660 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11662 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11663 | } |
| 11664 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11665 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11666 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11667 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11668 | if (PyUnicode_READY(self) == -1) |
| 11669 | return -1; |
| 11670 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | } |
| 11672 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11673 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11674 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11676 | 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] | 11677 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11678 | |
| 11679 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11680 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11681 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11682 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11683 | Py_UCS4 fillchar = ' '; |
| 11684 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11685 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | return NULL; |
| 11687 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11688 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11689 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11691 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11692 | return unicode_result_unchanged(self); |
| 11693 | |
| 11694 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | } |
| 11696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11697 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11699 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11700 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11701 | |
| 11702 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11703 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11704 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11705 | if (PyUnicode_READY(self) == -1) |
| 11706 | return NULL; |
| 11707 | if (PyUnicode_IS_ASCII(self)) |
| 11708 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11709 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11710 | } |
| 11711 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11712 | #define LEFTSTRIP 0 |
| 11713 | #define RIGHTSTRIP 1 |
| 11714 | #define BOTHSTRIP 2 |
| 11715 | |
| 11716 | /* Arrays indexed by above */ |
| 11717 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11718 | |
| 11719 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11720 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11721 | /* externally visible for str.strip(unicode) */ |
| 11722 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11723 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11724 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11725 | void *data; |
| 11726 | int kind; |
| 11727 | Py_ssize_t i, j, len; |
| 11728 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11729 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11730 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11731 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11732 | return NULL; |
| 11733 | |
| 11734 | kind = PyUnicode_KIND(self); |
| 11735 | data = PyUnicode_DATA(self); |
| 11736 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11737 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11739 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11740 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11741 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11742 | i = 0; |
| 11743 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11744 | while (i < len) { |
| 11745 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11746 | if (!BLOOM(sepmask, ch)) |
| 11747 | break; |
| 11748 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11749 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | i++; |
| 11751 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11753 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11754 | j = len; |
| 11755 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11756 | j--; |
| 11757 | while (j >= i) { |
| 11758 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11759 | if (!BLOOM(sepmask, ch)) |
| 11760 | break; |
| 11761 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11762 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11763 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11764 | } |
| 11765 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11767 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11768 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11769 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11770 | } |
| 11771 | |
| 11772 | PyObject* |
| 11773 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11774 | { |
| 11775 | unsigned char *data; |
| 11776 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11777 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11779 | if (PyUnicode_READY(self) == -1) |
| 11780 | return NULL; |
| 11781 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11782 | length = PyUnicode_GET_LENGTH(self); |
| 11783 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11784 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11785 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11786 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11787 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11788 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11789 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11790 | return NULL; |
| 11791 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11792 | if (start >= length || end < start) |
| 11793 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11794 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11795 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11796 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11797 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11798 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11799 | } |
| 11800 | else { |
| 11801 | kind = PyUnicode_KIND(self); |
| 11802 | data = PyUnicode_1BYTE_DATA(self); |
| 11803 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11804 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11805 | length); |
| 11806 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11808 | |
| 11809 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11810 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11811 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11812 | Py_ssize_t len, i, j; |
| 11813 | |
| 11814 | if (PyUnicode_READY(self) == -1) |
| 11815 | return NULL; |
| 11816 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11817 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11818 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11819 | if (PyUnicode_IS_ASCII(self)) { |
| 11820 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 11821 | |
| 11822 | i = 0; |
| 11823 | if (striptype != RIGHTSTRIP) { |
| 11824 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11825 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11826 | if (!_Py_ascii_whitespace[ch]) |
| 11827 | break; |
| 11828 | i++; |
| 11829 | } |
| 11830 | } |
| 11831 | |
| 11832 | j = len; |
| 11833 | if (striptype != LEFTSTRIP) { |
| 11834 | j--; |
| 11835 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11836 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11837 | if (!_Py_ascii_whitespace[ch]) |
| 11838 | break; |
| 11839 | j--; |
| 11840 | } |
| 11841 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11842 | } |
| 11843 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11844 | else { |
| 11845 | int kind = PyUnicode_KIND(self); |
| 11846 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11847 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11848 | i = 0; |
| 11849 | if (striptype != RIGHTSTRIP) { |
| 11850 | while (i < len) { |
| 11851 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11852 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11853 | break; |
| 11854 | i++; |
| 11855 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 11856 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11857 | |
| 11858 | j = len; |
| 11859 | if (striptype != LEFTSTRIP) { |
| 11860 | j--; |
| 11861 | while (j >= i) { |
| 11862 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11863 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11864 | break; |
| 11865 | j--; |
| 11866 | } |
| 11867 | j++; |
| 11868 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11869 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11870 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11871 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | } |
| 11873 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11874 | |
| 11875 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11876 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11877 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11878 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11879 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 11880 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11881 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11882 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11883 | if (sep != NULL && sep != Py_None) { |
| 11884 | if (PyUnicode_Check(sep)) |
| 11885 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11886 | else { |
| 11887 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11888 | "%s arg must be None or str", |
| 11889 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11890 | return NULL; |
| 11891 | } |
| 11892 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11893 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11894 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11895 | } |
| 11896 | |
| 11897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11898 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11899 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11900 | \n\ |
| 11901 | Return a copy of the string S with leading and trailing\n\ |
| 11902 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11903 | 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] | 11904 | |
| 11905 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11906 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11907 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11908 | if (PyTuple_GET_SIZE(args) == 0) |
| 11909 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11910 | else |
| 11911 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11912 | } |
| 11913 | |
| 11914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11915 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11916 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11917 | \n\ |
| 11918 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11919 | 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] | 11920 | |
| 11921 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11922 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11923 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11924 | if (PyTuple_GET_SIZE(args) == 0) |
| 11925 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11926 | else |
| 11927 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11928 | } |
| 11929 | |
| 11930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11931 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11932 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11933 | \n\ |
| 11934 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11935 | 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] | 11936 | |
| 11937 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11938 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11939 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11940 | if (PyTuple_GET_SIZE(args) == 0) |
| 11941 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11942 | else |
| 11943 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11944 | } |
| 11945 | |
| 11946 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11948 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11950 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11952 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11953 | if (len < 1) |
| 11954 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11956 | /* no repeat, return original string */ |
| 11957 | if (len == 1) |
| 11958 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11959 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11960 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11961 | return NULL; |
| 11962 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11963 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11964 | PyErr_SetString(PyExc_OverflowError, |
| 11965 | "repeated string is too long"); |
| 11966 | return NULL; |
| 11967 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11969 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11970 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11971 | if (!u) |
| 11972 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11973 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11975 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11976 | const int kind = PyUnicode_KIND(str); |
| 11977 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11978 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11979 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11980 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11981 | } |
| 11982 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11983 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11984 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11985 | ucs2[n] = fill_char; |
| 11986 | } else { |
| 11987 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11988 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11989 | for (n = 0; n < len; ++n) |
| 11990 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11991 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11992 | } |
| 11993 | else { |
| 11994 | /* number of characters copied this far */ |
| 11995 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11996 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11997 | char *to = (char *) PyUnicode_DATA(u); |
| 11998 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11999 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12000 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12001 | n = (done <= nchars-done) ? done : nchars-done; |
| 12002 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12003 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12005 | } |
| 12006 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12007 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12008 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12009 | } |
| 12010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12011 | PyObject * |
| 12012 | PyUnicode_Replace(PyObject *obj, |
| 12013 | PyObject *subobj, |
| 12014 | PyObject *replobj, |
| 12015 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12016 | { |
| 12017 | PyObject *self; |
| 12018 | PyObject *str1; |
| 12019 | PyObject *str2; |
| 12020 | PyObject *result; |
| 12021 | |
| 12022 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12023 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12025 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12026 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12027 | Py_DECREF(self); |
| 12028 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12029 | } |
| 12030 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12031 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12032 | Py_DECREF(self); |
| 12033 | Py_DECREF(str1); |
| 12034 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12035 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12036 | if (PyUnicode_READY(self) == -1 || |
| 12037 | PyUnicode_READY(str1) == -1 || |
| 12038 | PyUnicode_READY(str2) == -1) |
| 12039 | result = NULL; |
| 12040 | else |
| 12041 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12042 | Py_DECREF(self); |
| 12043 | Py_DECREF(str1); |
| 12044 | Py_DECREF(str2); |
| 12045 | return result; |
| 12046 | } |
| 12047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12048 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12049 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12050 | \n\ |
| 12051 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12052 | old replaced by new. If the optional argument count is\n\ |
| 12053 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12054 | |
| 12055 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12057 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12058 | PyObject *str1; |
| 12059 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12060 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12061 | PyObject *result; |
| 12062 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12063 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12064 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12065 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12066 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12067 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12068 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | return NULL; |
| 12070 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12071 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12072 | Py_DECREF(str1); |
| 12073 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12074 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12075 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12076 | result = NULL; |
| 12077 | else |
| 12078 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12079 | |
| 12080 | Py_DECREF(str1); |
| 12081 | Py_DECREF(str2); |
| 12082 | return result; |
| 12083 | } |
| 12084 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12085 | static PyObject * |
| 12086 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12088 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12089 | Py_ssize_t isize; |
| 12090 | Py_ssize_t osize, squote, dquote, i, o; |
| 12091 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12092 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12094 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12095 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12096 | return NULL; |
| 12097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12098 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12099 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | /* Compute length of output, quote characters, and |
| 12102 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12103 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12104 | max = 127; |
| 12105 | squote = dquote = 0; |
| 12106 | ikind = PyUnicode_KIND(unicode); |
| 12107 | for (i = 0; i < isize; i++) { |
| 12108 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12109 | switch (ch) { |
| 12110 | case '\'': squote++; osize++; break; |
| 12111 | case '"': dquote++; osize++; break; |
| 12112 | case '\\': case '\t': case '\r': case '\n': |
| 12113 | osize += 2; break; |
| 12114 | default: |
| 12115 | /* Fast-path ASCII */ |
| 12116 | if (ch < ' ' || ch == 0x7f) |
| 12117 | osize += 4; /* \xHH */ |
| 12118 | else if (ch < 0x7f) |
| 12119 | osize++; |
| 12120 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12121 | osize++; |
| 12122 | max = ch > max ? ch : max; |
| 12123 | } |
| 12124 | else if (ch < 0x100) |
| 12125 | osize += 4; /* \xHH */ |
| 12126 | else if (ch < 0x10000) |
| 12127 | osize += 6; /* \uHHHH */ |
| 12128 | else |
| 12129 | osize += 10; /* \uHHHHHHHH */ |
| 12130 | } |
| 12131 | } |
| 12132 | |
| 12133 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12134 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12135 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12136 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12137 | if (dquote) |
| 12138 | /* Both squote and dquote present. Use squote, |
| 12139 | and escape them */ |
| 12140 | osize += squote; |
| 12141 | else |
| 12142 | quote = '"'; |
| 12143 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12144 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12145 | |
| 12146 | repr = PyUnicode_New(osize, max); |
| 12147 | if (repr == NULL) |
| 12148 | return NULL; |
| 12149 | okind = PyUnicode_KIND(repr); |
| 12150 | odata = PyUnicode_DATA(repr); |
| 12151 | |
| 12152 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12153 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12154 | if (unchanged) { |
| 12155 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12156 | unicode, 0, |
| 12157 | isize); |
| 12158 | } |
| 12159 | else { |
| 12160 | for (i = 0, o = 1; i < isize; i++) { |
| 12161 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12162 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12163 | /* Escape quotes and backslashes */ |
| 12164 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12165 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12166 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12167 | continue; |
| 12168 | } |
| 12169 | |
| 12170 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12171 | if (ch == '\t') { |
| 12172 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12173 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12174 | } |
| 12175 | else if (ch == '\n') { |
| 12176 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12177 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12178 | } |
| 12179 | else if (ch == '\r') { |
| 12180 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12181 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12182 | } |
| 12183 | |
| 12184 | /* Map non-printable US ASCII to '\xhh' */ |
| 12185 | else if (ch < ' ' || ch == 0x7F) { |
| 12186 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12187 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12188 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12189 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12190 | } |
| 12191 | |
| 12192 | /* Copy ASCII characters as-is */ |
| 12193 | else if (ch < 0x7F) { |
| 12194 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12195 | } |
| 12196 | |
| 12197 | /* Non-ASCII characters */ |
| 12198 | else { |
| 12199 | /* Map Unicode whitespace and control characters |
| 12200 | (categories Z* and C* except ASCII space) |
| 12201 | */ |
| 12202 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12203 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12204 | /* Map 8-bit characters to '\xhh' */ |
| 12205 | if (ch <= 0xff) { |
| 12206 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12207 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12208 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12209 | } |
| 12210 | /* Map 16-bit characters to '\uxxxx' */ |
| 12211 | else if (ch <= 0xffff) { |
| 12212 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12213 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12214 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12215 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12216 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12217 | } |
| 12218 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12219 | else { |
| 12220 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12221 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12222 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12223 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12224 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12225 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12226 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12227 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12228 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12229 | } |
| 12230 | } |
| 12231 | /* Copy characters as-is */ |
| 12232 | else { |
| 12233 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12234 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12235 | } |
| 12236 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12237 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12238 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12239 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12240 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12241 | } |
| 12242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12243 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12244 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12245 | \n\ |
| 12246 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12247 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | arguments start and end are interpreted as in slice notation.\n\ |
| 12249 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12250 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | |
| 12252 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12253 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12254 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12255 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12256 | Py_ssize_t start; |
| 12257 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12258 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12259 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12260 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12261 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12262 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12264 | if (PyUnicode_READY(self) == -1) { |
| 12265 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12266 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12267 | } |
| 12268 | if (PyUnicode_READY(substring) == -1) { |
| 12269 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12270 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12271 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12273 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | |
| 12275 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12277 | if (result == -2) |
| 12278 | return NULL; |
| 12279 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12280 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | } |
| 12282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12283 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12286 | 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] | 12287 | |
| 12288 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12289 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12290 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12291 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12292 | Py_ssize_t start; |
| 12293 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12294 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12296 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12297 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12298 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12300 | if (PyUnicode_READY(self) == -1) { |
| 12301 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12303 | } |
| 12304 | if (PyUnicode_READY(substring) == -1) { |
| 12305 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12306 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12307 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12309 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12310 | |
| 12311 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12313 | if (result == -2) |
| 12314 | return NULL; |
| 12315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12316 | if (result < 0) { |
| 12317 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12318 | return NULL; |
| 12319 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12320 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12321 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | } |
| 12323 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12324 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12325 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12326 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12327 | 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] | 12328 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12329 | |
| 12330 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12331 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12332 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12333 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | Py_UCS4 fillchar = ' '; |
| 12335 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12336 | 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] | 12337 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12338 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12339 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12340 | return NULL; |
| 12341 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12342 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12343 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12344 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12345 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12346 | } |
| 12347 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12348 | PyObject * |
| 12349 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12350 | { |
| 12351 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12353 | s = PyUnicode_FromObject(s); |
| 12354 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12355 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12356 | if (sep != NULL) { |
| 12357 | sep = PyUnicode_FromObject(sep); |
| 12358 | if (sep == NULL) { |
| 12359 | Py_DECREF(s); |
| 12360 | return NULL; |
| 12361 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | } |
| 12363 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12364 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | |
| 12366 | Py_DECREF(s); |
| 12367 | Py_XDECREF(sep); |
| 12368 | return result; |
| 12369 | } |
| 12370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12371 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12372 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12373 | \n\ |
| 12374 | Return a list of the words in S, using sep as the\n\ |
| 12375 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12376 | 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] | 12377 | whitespace string is a separator and empty strings are\n\ |
| 12378 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12379 | |
| 12380 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12381 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12383 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12385 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12387 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12388 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | return NULL; |
| 12390 | |
| 12391 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12392 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12393 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12394 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12395 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12396 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12397 | } |
| 12398 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12399 | PyObject * |
| 12400 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12401 | { |
| 12402 | PyObject* str_obj; |
| 12403 | PyObject* sep_obj; |
| 12404 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | int kind1, kind2, kind; |
| 12406 | void *buf1 = NULL, *buf2 = NULL; |
| 12407 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12408 | |
| 12409 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12410 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12412 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12413 | if (!sep_obj) { |
| 12414 | Py_DECREF(str_obj); |
| 12415 | return NULL; |
| 12416 | } |
| 12417 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12418 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12419 | Py_DECREF(str_obj); |
| 12420 | return NULL; |
| 12421 | } |
| 12422 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12423 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12424 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12425 | kind = Py_MAX(kind1, kind2); |
| 12426 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12428 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12429 | if (!buf1) |
| 12430 | goto onError; |
| 12431 | buf2 = PyUnicode_DATA(sep_obj); |
| 12432 | if (kind2 != kind) |
| 12433 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12434 | if (!buf2) |
| 12435 | goto onError; |
| 12436 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12437 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12438 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12439 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12440 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12441 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12442 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12443 | else |
| 12444 | 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] | 12445 | break; |
| 12446 | case PyUnicode_2BYTE_KIND: |
| 12447 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12448 | break; |
| 12449 | case PyUnicode_4BYTE_KIND: |
| 12450 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12451 | break; |
| 12452 | default: |
| 12453 | assert(0); |
| 12454 | out = 0; |
| 12455 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12456 | |
| 12457 | Py_DECREF(sep_obj); |
| 12458 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 | if (kind1 != kind) |
| 12460 | PyMem_Free(buf1); |
| 12461 | if (kind2 != kind) |
| 12462 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12463 | |
| 12464 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12465 | onError: |
| 12466 | Py_DECREF(sep_obj); |
| 12467 | Py_DECREF(str_obj); |
| 12468 | if (kind1 != kind && buf1) |
| 12469 | PyMem_Free(buf1); |
| 12470 | if (kind2 != kind && buf2) |
| 12471 | PyMem_Free(buf2); |
| 12472 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12473 | } |
| 12474 | |
| 12475 | |
| 12476 | PyObject * |
| 12477 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12478 | { |
| 12479 | PyObject* str_obj; |
| 12480 | PyObject* sep_obj; |
| 12481 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12482 | int kind1, kind2, kind; |
| 12483 | void *buf1 = NULL, *buf2 = NULL; |
| 12484 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12485 | |
| 12486 | str_obj = PyUnicode_FromObject(str_in); |
| 12487 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12488 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12489 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12490 | if (!sep_obj) { |
| 12491 | Py_DECREF(str_obj); |
| 12492 | return NULL; |
| 12493 | } |
| 12494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12495 | kind1 = PyUnicode_KIND(str_in); |
| 12496 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12497 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | buf1 = PyUnicode_DATA(str_in); |
| 12499 | if (kind1 != kind) |
| 12500 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12501 | if (!buf1) |
| 12502 | goto onError; |
| 12503 | buf2 = PyUnicode_DATA(sep_obj); |
| 12504 | if (kind2 != kind) |
| 12505 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12506 | if (!buf2) |
| 12507 | goto onError; |
| 12508 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12509 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12510 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12511 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12513 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12514 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12515 | else |
| 12516 | 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] | 12517 | break; |
| 12518 | case PyUnicode_2BYTE_KIND: |
| 12519 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12520 | break; |
| 12521 | case PyUnicode_4BYTE_KIND: |
| 12522 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12523 | break; |
| 12524 | default: |
| 12525 | assert(0); |
| 12526 | out = 0; |
| 12527 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12528 | |
| 12529 | Py_DECREF(sep_obj); |
| 12530 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12531 | if (kind1 != kind) |
| 12532 | PyMem_Free(buf1); |
| 12533 | if (kind2 != kind) |
| 12534 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12535 | |
| 12536 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12537 | onError: |
| 12538 | Py_DECREF(sep_obj); |
| 12539 | Py_DECREF(str_obj); |
| 12540 | if (kind1 != kind && buf1) |
| 12541 | PyMem_Free(buf1); |
| 12542 | if (kind2 != kind && buf2) |
| 12543 | PyMem_Free(buf2); |
| 12544 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12545 | } |
| 12546 | |
| 12547 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12548 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12549 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12550 | 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] | 12551 | 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] | 12552 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12553 | |
| 12554 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12555 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12556 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12557 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12558 | } |
| 12559 | |
| 12560 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12561 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12562 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12563 | 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] | 12564 | 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] | 12565 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12566 | |
| 12567 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12568 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12569 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12570 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12571 | } |
| 12572 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12573 | PyObject * |
| 12574 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12575 | { |
| 12576 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12577 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12578 | s = PyUnicode_FromObject(s); |
| 12579 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12580 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12581 | if (sep != NULL) { |
| 12582 | sep = PyUnicode_FromObject(sep); |
| 12583 | if (sep == NULL) { |
| 12584 | Py_DECREF(s); |
| 12585 | return NULL; |
| 12586 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12587 | } |
| 12588 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12589 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12590 | |
| 12591 | Py_DECREF(s); |
| 12592 | Py_XDECREF(sep); |
| 12593 | return result; |
| 12594 | } |
| 12595 | |
| 12596 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12597 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12598 | \n\ |
| 12599 | Return a list of the words in S, using sep as the\n\ |
| 12600 | delimiter string, starting at the end of the string and\n\ |
| 12601 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12602 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12603 | is a separator."); |
| 12604 | |
| 12605 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12606 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12607 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12608 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12609 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12610 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12611 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12612 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12613 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12614 | return NULL; |
| 12615 | |
| 12616 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12617 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12618 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12619 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12620 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12621 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12622 | } |
| 12623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12624 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12625 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | \n\ |
| 12627 | 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] | 12628 | 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] | 12629 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | |
| 12631 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12632 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12634 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12635 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12637 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12638 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12639 | return NULL; |
| 12640 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12641 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | } |
| 12643 | |
| 12644 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12645 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12646 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12647 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12648 | } |
| 12649 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12650 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12651 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12652 | \n\ |
| 12653 | 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] | 12654 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12655 | |
| 12656 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12657 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12658 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12659 | if (PyUnicode_READY(self) == -1) |
| 12660 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12661 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12662 | } |
| 12663 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12664 | /*[clinic] |
| 12665 | module str |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12666 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12667 | @staticmethod |
| 12668 | str.maketrans as unicode_maketrans |
| 12669 | |
| 12670 | x: object |
| 12671 | |
| 12672 | y: unicode=NULL |
| 12673 | |
| 12674 | z: unicode=NULL |
| 12675 | |
| 12676 | / |
| 12677 | |
| 12678 | Return a translation table usable for str.translate(). |
| 12679 | |
| 12680 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12681 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12682 | Character keys will be then converted to ordinals. |
| 12683 | If there are two arguments, they must be strings of equal length, and |
| 12684 | in the resulting dictionary, each character in x will be mapped to the |
| 12685 | character at the same position in y. If there is a third argument, it |
| 12686 | must be a string, whose characters will be mapped to None in the result. |
| 12687 | [clinic]*/ |
| 12688 | |
| 12689 | PyDoc_STRVAR(unicode_maketrans__doc__, |
| 12690 | "Return a translation table usable for str.translate().\n" |
| 12691 | "\n" |
| 12692 | "str.maketrans(x, y=None, z=None)\n" |
| 12693 | "\n" |
| 12694 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12695 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12696 | "Character keys will be then converted to ordinals.\n" |
| 12697 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12698 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12699 | "character at the same position in y. If there is a third argument, it\n" |
| 12700 | "must be a string, whose characters will be mapped to None in the result."); |
| 12701 | |
| 12702 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12703 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12704 | |
| 12705 | static PyObject * |
| 12706 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
| 12707 | |
| 12708 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12709 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12710 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12711 | PyObject *return_value = NULL; |
| 12712 | PyObject *x; |
| 12713 | PyObject *y = NULL; |
| 12714 | PyObject *z = NULL; |
| 12715 | |
| 12716 | if (!PyArg_ParseTuple(args, |
| 12717 | "O|UU:maketrans", |
| 12718 | &x, &y, &z)) |
| 12719 | goto exit; |
| 12720 | return_value = unicode_maketrans_impl(x, y, z); |
| 12721 | |
| 12722 | exit: |
| 12723 | return return_value; |
| 12724 | } |
| 12725 | |
| 12726 | static PyObject * |
| 12727 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
| 12728 | /*[clinic checksum: 137db9c3199e7906b7967009f511c24fa3235b5f]*/ |
| 12729 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12730 | PyObject *new = NULL, *key, *value; |
| 12731 | Py_ssize_t i = 0; |
| 12732 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12733 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12734 | new = PyDict_New(); |
| 12735 | if (!new) |
| 12736 | return NULL; |
| 12737 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | int x_kind, y_kind, z_kind; |
| 12739 | void *x_data, *y_data, *z_data; |
| 12740 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12741 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12742 | if (!PyUnicode_Check(x)) { |
| 12743 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12744 | "be a string if there is a second argument"); |
| 12745 | goto err; |
| 12746 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12747 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12748 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12749 | "arguments must have equal length"); |
| 12750 | goto err; |
| 12751 | } |
| 12752 | /* 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] | 12753 | x_kind = PyUnicode_KIND(x); |
| 12754 | y_kind = PyUnicode_KIND(y); |
| 12755 | x_data = PyUnicode_DATA(x); |
| 12756 | y_data = PyUnicode_DATA(y); |
| 12757 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12758 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12759 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12760 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12761 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12762 | if (!value) { |
| 12763 | Py_DECREF(key); |
| 12764 | goto err; |
| 12765 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12766 | res = PyDict_SetItem(new, key, value); |
| 12767 | Py_DECREF(key); |
| 12768 | Py_DECREF(value); |
| 12769 | if (res < 0) |
| 12770 | goto err; |
| 12771 | } |
| 12772 | /* create entries for deleting chars in z */ |
| 12773 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12774 | z_kind = PyUnicode_KIND(z); |
| 12775 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12776 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12777 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12778 | if (!key) |
| 12779 | goto err; |
| 12780 | res = PyDict_SetItem(new, key, Py_None); |
| 12781 | Py_DECREF(key); |
| 12782 | if (res < 0) |
| 12783 | goto err; |
| 12784 | } |
| 12785 | } |
| 12786 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12787 | int kind; |
| 12788 | void *data; |
| 12789 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12790 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12791 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12792 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12793 | "to maketrans it must be a dict"); |
| 12794 | goto err; |
| 12795 | } |
| 12796 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12797 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12798 | if (PyUnicode_Check(key)) { |
| 12799 | /* convert string keys to integer keys */ |
| 12800 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12801 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12802 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12803 | "table must be of length 1"); |
| 12804 | goto err; |
| 12805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12806 | kind = PyUnicode_KIND(key); |
| 12807 | data = PyUnicode_DATA(key); |
| 12808 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12809 | if (!newkey) |
| 12810 | goto err; |
| 12811 | res = PyDict_SetItem(new, newkey, value); |
| 12812 | Py_DECREF(newkey); |
| 12813 | if (res < 0) |
| 12814 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12815 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12816 | /* just keep integer keys */ |
| 12817 | if (PyDict_SetItem(new, key, value) < 0) |
| 12818 | goto err; |
| 12819 | } else { |
| 12820 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12821 | "be strings or integers"); |
| 12822 | goto err; |
| 12823 | } |
| 12824 | } |
| 12825 | } |
| 12826 | return new; |
| 12827 | err: |
| 12828 | Py_DECREF(new); |
| 12829 | return NULL; |
| 12830 | } |
| 12831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12832 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12833 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12834 | \n\ |
| 12835 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12836 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12837 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12838 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12839 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | |
| 12841 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12842 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12844 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | } |
| 12846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12847 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12848 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12849 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12850 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12851 | |
| 12852 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12853 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12854 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12855 | if (PyUnicode_READY(self) == -1) |
| 12856 | return NULL; |
| 12857 | if (PyUnicode_IS_ASCII(self)) |
| 12858 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12859 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12860 | } |
| 12861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12862 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12863 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12864 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12865 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12866 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | |
| 12868 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12869 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12871 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12872 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12873 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12874 | int kind; |
| 12875 | void *data; |
| 12876 | Py_UCS4 chr; |
| 12877 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12878 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12879 | return NULL; |
| 12880 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12881 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12882 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12884 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12885 | return unicode_result_unchanged(self); |
| 12886 | |
| 12887 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12888 | |
| 12889 | u = pad(self, fill, 0, '0'); |
| 12890 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12891 | if (u == NULL) |
| 12892 | return NULL; |
| 12893 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12894 | kind = PyUnicode_KIND(u); |
| 12895 | data = PyUnicode_DATA(u); |
| 12896 | chr = PyUnicode_READ(kind, data, fill); |
| 12897 | |
| 12898 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12900 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12901 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12902 | } |
| 12903 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12904 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12905 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12906 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12907 | |
| 12908 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12909 | static PyObject * |
| 12910 | unicode__decimal2ascii(PyObject *self) |
| 12911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12912 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12913 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12914 | #endif |
| 12915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12916 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12917 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12918 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12919 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12920 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12921 | With optional end, stop comparing S at that position.\n\ |
| 12922 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12923 | |
| 12924 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12925 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12926 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12927 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12928 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12929 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12930 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12931 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12932 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12933 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12934 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12935 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12936 | if (PyTuple_Check(subobj)) { |
| 12937 | Py_ssize_t i; |
| 12938 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12939 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12940 | if (substring == NULL) |
| 12941 | return NULL; |
| 12942 | result = tailmatch(self, substring, start, end, -1); |
| 12943 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12944 | if (result == -1) |
| 12945 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12946 | if (result) { |
| 12947 | Py_RETURN_TRUE; |
| 12948 | } |
| 12949 | } |
| 12950 | /* nothing matched */ |
| 12951 | Py_RETURN_FALSE; |
| 12952 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12953 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12954 | if (substring == NULL) { |
| 12955 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12956 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12957 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12958 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12959 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12960 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12961 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12962 | if (result == -1) |
| 12963 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12964 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12965 | } |
| 12966 | |
| 12967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12968 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12969 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12970 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12971 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12972 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12973 | With optional end, stop comparing S at that position.\n\ |
| 12974 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12975 | |
| 12976 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12977 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12978 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12979 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12980 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12981 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12982 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12983 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12984 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12985 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12986 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12987 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12988 | if (PyTuple_Check(subobj)) { |
| 12989 | Py_ssize_t i; |
| 12990 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12991 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12993 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12994 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12995 | result = tailmatch(self, substring, start, end, +1); |
| 12996 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12997 | if (result == -1) |
| 12998 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12999 | if (result) { |
| 13000 | Py_RETURN_TRUE; |
| 13001 | } |
| 13002 | } |
| 13003 | Py_RETURN_FALSE; |
| 13004 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13005 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13006 | if (substring == NULL) { |
| 13007 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13008 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13009 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13010 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13011 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13012 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13013 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13014 | if (result == -1) |
| 13015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13016 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13017 | } |
| 13018 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13019 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13020 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13021 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13022 | if (!writer->readonly) |
| 13023 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13024 | else { |
| 13025 | /* Copy-on-write mode: set buffer size to 0 so |
| 13026 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13027 | * next write. */ |
| 13028 | writer->size = 0; |
| 13029 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13030 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13031 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13032 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13033 | } |
| 13034 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13035 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13036 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13037 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13038 | memset(writer, 0, sizeof(*writer)); |
| 13039 | #ifdef Py_DEBUG |
| 13040 | writer->kind = 5; /* invalid kind */ |
| 13041 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13042 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13043 | } |
| 13044 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13045 | int |
| 13046 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13047 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13048 | { |
| 13049 | Py_ssize_t newlen; |
| 13050 | PyObject *newbuffer; |
| 13051 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13052 | assert(length > 0); |
| 13053 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13054 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13055 | PyErr_NoMemory(); |
| 13056 | return -1; |
| 13057 | } |
| 13058 | newlen = writer->pos + length; |
| 13059 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13060 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13061 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13062 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13063 | assert(!writer->readonly); |
| 13064 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13065 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13066 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13067 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13068 | if (newlen < writer->min_length) |
| 13069 | newlen = writer->min_length; |
| 13070 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13071 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13072 | if (writer->buffer == NULL) |
| 13073 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13074 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13075 | else if (newlen > writer->size) { |
| 13076 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13077 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13078 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13079 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13080 | if (newlen < writer->min_length) |
| 13081 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13082 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13083 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13084 | /* resize + widen */ |
| 13085 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13086 | if (newbuffer == NULL) |
| 13087 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13088 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13089 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13090 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13091 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13092 | } |
| 13093 | else { |
| 13094 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13095 | if (newbuffer == NULL) |
| 13096 | return -1; |
| 13097 | } |
| 13098 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13099 | } |
| 13100 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13101 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13102 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13103 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13104 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13105 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13106 | writer->buffer, 0, writer->pos); |
| 13107 | Py_DECREF(writer->buffer); |
| 13108 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13109 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13110 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13111 | return 0; |
| 13112 | } |
| 13113 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13114 | Py_LOCAL_INLINE(int) |
| 13115 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13116 | { |
| 13117 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13118 | return -1; |
| 13119 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13120 | writer->pos++; |
| 13121 | return 0; |
| 13122 | } |
| 13123 | |
| 13124 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13125 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13126 | { |
| 13127 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13128 | } |
| 13129 | |
| 13130 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13131 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13132 | { |
| 13133 | Py_UCS4 maxchar; |
| 13134 | Py_ssize_t len; |
| 13135 | |
| 13136 | if (PyUnicode_READY(str) == -1) |
| 13137 | return -1; |
| 13138 | len = PyUnicode_GET_LENGTH(str); |
| 13139 | if (len == 0) |
| 13140 | return 0; |
| 13141 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13142 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13143 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13144 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13145 | Py_INCREF(str); |
| 13146 | writer->buffer = str; |
| 13147 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13148 | writer->pos += len; |
| 13149 | return 0; |
| 13150 | } |
| 13151 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13152 | return -1; |
| 13153 | } |
| 13154 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13155 | str, 0, len); |
| 13156 | writer->pos += len; |
| 13157 | return 0; |
| 13158 | } |
| 13159 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13160 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13161 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13162 | Py_ssize_t start, Py_ssize_t end) |
| 13163 | { |
| 13164 | Py_UCS4 maxchar; |
| 13165 | Py_ssize_t len; |
| 13166 | |
| 13167 | if (PyUnicode_READY(str) == -1) |
| 13168 | return -1; |
| 13169 | |
| 13170 | assert(0 <= start); |
| 13171 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13172 | assert(start <= end); |
| 13173 | |
| 13174 | if (end == 0) |
| 13175 | return 0; |
| 13176 | |
| 13177 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13178 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13179 | |
| 13180 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13181 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13182 | else |
| 13183 | maxchar = writer->maxchar; |
| 13184 | len = end - start; |
| 13185 | |
| 13186 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13187 | return -1; |
| 13188 | |
| 13189 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13190 | str, start, len); |
| 13191 | writer->pos += len; |
| 13192 | return 0; |
| 13193 | } |
| 13194 | |
| 13195 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13196 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 13197 | { |
| 13198 | Py_UCS4 maxchar; |
| 13199 | |
| 13200 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13201 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13202 | return -1; |
| 13203 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13204 | writer->pos += len; |
| 13205 | return 0; |
| 13206 | } |
| 13207 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13208 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13209 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13210 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13211 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13212 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13213 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13214 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13215 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13216 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13217 | str = writer->buffer; |
| 13218 | writer->buffer = NULL; |
| 13219 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13220 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13221 | } |
| 13222 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13223 | PyObject *newbuffer; |
| 13224 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13225 | if (newbuffer == NULL) { |
| 13226 | Py_DECREF(writer->buffer); |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13227 | writer->buffer = NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13228 | return NULL; |
| 13229 | } |
| 13230 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13231 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13232 | str = writer->buffer; |
| 13233 | writer->buffer = NULL; |
| 13234 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13235 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13236 | } |
| 13237 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13238 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13239 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13240 | { |
| 13241 | Py_CLEAR(writer->buffer); |
| 13242 | } |
| 13243 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13244 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13245 | |
| 13246 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13247 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13248 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13249 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13250 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13251 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13252 | PyDoc_STRVAR(format_map__doc__, |
| 13253 | "S.format_map(mapping) -> str\n\ |
| 13254 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13255 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13256 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13257 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13258 | static PyObject * |
| 13259 | unicode__format__(PyObject* self, PyObject* args) |
| 13260 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13261 | PyObject *format_spec; |
| 13262 | _PyUnicodeWriter writer; |
| 13263 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13264 | |
| 13265 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13266 | return NULL; |
| 13267 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13268 | if (PyUnicode_READY(self) == -1) |
| 13269 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13270 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13271 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13272 | self, format_spec, 0, |
| 13273 | PyUnicode_GET_LENGTH(format_spec)); |
| 13274 | if (ret == -1) { |
| 13275 | _PyUnicodeWriter_Dealloc(&writer); |
| 13276 | return NULL; |
| 13277 | } |
| 13278 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13279 | } |
| 13280 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13281 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13282 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13283 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13284 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13285 | |
| 13286 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13287 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13288 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13289 | Py_ssize_t size; |
| 13290 | |
| 13291 | /* If it's a compact object, account for base structure + |
| 13292 | character data. */ |
| 13293 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13294 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13295 | else if (PyUnicode_IS_COMPACT(v)) |
| 13296 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13297 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13298 | else { |
| 13299 | /* If it is a two-block object, account for base object, and |
| 13300 | for character block if present. */ |
| 13301 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13302 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13303 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13304 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13305 | } |
| 13306 | /* 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] | 13307 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13308 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13309 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13310 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13311 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13312 | |
| 13313 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13314 | } |
| 13315 | |
| 13316 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13317 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13318 | |
| 13319 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13320 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13321 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13322 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13323 | if (!copy) |
| 13324 | return NULL; |
| 13325 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13326 | } |
| 13327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13328 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13329 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13330 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13331 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13332 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13333 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13334 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13335 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13336 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13337 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13338 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13339 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13340 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13341 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13342 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13343 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13344 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13345 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13346 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13347 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13348 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13349 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13350 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13351 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13352 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13353 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13354 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13355 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13356 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13357 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13358 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13359 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13360 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13361 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13362 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13363 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13364 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13365 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13366 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13367 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13368 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13369 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13370 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13371 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13372 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13373 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13374 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13375 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13376 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13377 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13378 | #endif |
| 13379 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13380 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13381 | {NULL, NULL} |
| 13382 | }; |
| 13383 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13384 | static PyObject * |
| 13385 | unicode_mod(PyObject *v, PyObject *w) |
| 13386 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13387 | if (!PyUnicode_Check(v)) |
| 13388 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13389 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13390 | } |
| 13391 | |
| 13392 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13393 | 0, /*nb_add*/ |
| 13394 | 0, /*nb_subtract*/ |
| 13395 | 0, /*nb_multiply*/ |
| 13396 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13397 | }; |
| 13398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13399 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13400 | (lenfunc) unicode_length, /* sq_length */ |
| 13401 | PyUnicode_Concat, /* sq_concat */ |
| 13402 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13403 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13404 | 0, /* sq_slice */ |
| 13405 | 0, /* sq_ass_item */ |
| 13406 | 0, /* sq_ass_slice */ |
| 13407 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13408 | }; |
| 13409 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13410 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13411 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13412 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13413 | if (PyUnicode_READY(self) == -1) |
| 13414 | return NULL; |
| 13415 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13416 | if (PyIndex_Check(item)) { |
| 13417 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13418 | if (i == -1 && PyErr_Occurred()) |
| 13419 | return NULL; |
| 13420 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13421 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13422 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13423 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13424 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13425 | PyObject *result; |
| 13426 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13427 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13428 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13429 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13430 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13431 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13432 | return NULL; |
| 13433 | } |
| 13434 | |
| 13435 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13436 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13437 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13438 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13439 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13440 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13441 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13442 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13443 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13444 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13445 | src_kind = PyUnicode_KIND(self); |
| 13446 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13447 | if (!PyUnicode_IS_ASCII(self)) { |
| 13448 | kind_limit = kind_maxchar_limit(src_kind); |
| 13449 | max_char = 0; |
| 13450 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13451 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13452 | if (ch > max_char) { |
| 13453 | max_char = ch; |
| 13454 | if (max_char >= kind_limit) |
| 13455 | break; |
| 13456 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13457 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13458 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13459 | else |
| 13460 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13461 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13462 | if (result == NULL) |
| 13463 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13464 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13465 | dest_data = PyUnicode_DATA(result); |
| 13466 | |
| 13467 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13468 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13469 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13470 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13471 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13472 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13473 | } else { |
| 13474 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13475 | return NULL; |
| 13476 | } |
| 13477 | } |
| 13478 | |
| 13479 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13480 | (lenfunc)unicode_length, /* mp_length */ |
| 13481 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13482 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13483 | }; |
| 13484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13486 | /* Helpers for PyUnicode_Format() */ |
| 13487 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13488 | struct unicode_formatter_t { |
| 13489 | PyObject *args; |
| 13490 | int args_owned; |
| 13491 | Py_ssize_t arglen, argidx; |
| 13492 | PyObject *dict; |
| 13493 | |
| 13494 | enum PyUnicode_Kind fmtkind; |
| 13495 | Py_ssize_t fmtcnt, fmtpos; |
| 13496 | void *fmtdata; |
| 13497 | PyObject *fmtstr; |
| 13498 | |
| 13499 | _PyUnicodeWriter writer; |
| 13500 | }; |
| 13501 | |
| 13502 | struct unicode_format_arg_t { |
| 13503 | Py_UCS4 ch; |
| 13504 | int flags; |
| 13505 | Py_ssize_t width; |
| 13506 | int prec; |
| 13507 | int sign; |
| 13508 | }; |
| 13509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13510 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13511 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13512 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13513 | Py_ssize_t argidx = ctx->argidx; |
| 13514 | |
| 13515 | if (argidx < ctx->arglen) { |
| 13516 | ctx->argidx++; |
| 13517 | if (ctx->arglen < 0) |
| 13518 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13519 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13520 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13521 | } |
| 13522 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13524 | return NULL; |
| 13525 | } |
| 13526 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13527 | /* 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] | 13528 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13529 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13530 | otherwise. |
| 13531 | |
| 13532 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13533 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13534 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13535 | PyObject **p_output, |
| 13536 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13537 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13538 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13539 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13540 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13541 | int prec; |
| 13542 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13544 | x = PyFloat_AsDouble(v); |
| 13545 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13546 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13547 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13548 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13549 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13550 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13551 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13552 | if (arg->flags & F_ALT) |
| 13553 | dtoa_flags = Py_DTSF_ALT; |
| 13554 | else |
| 13555 | dtoa_flags = 0; |
| 13556 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13557 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13558 | return -1; |
| 13559 | len = strlen(p); |
| 13560 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13561 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13562 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13563 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13564 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13565 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13566 | writer->pos += len; |
| 13567 | } |
| 13568 | else |
| 13569 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13570 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13571 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13572 | } |
| 13573 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13574 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13575 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13576 | * Python's regular ints. |
| 13577 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13578 | * The output string is of the form |
| 13579 | * "-"? ("0x" | "0X")? digit+ |
| 13580 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13581 | * set in flags. The case of hex digits will be correct, |
| 13582 | * There will be at least prec digits, zero-filled on the left if |
| 13583 | * necessary to get that many. |
| 13584 | * val object to be converted |
| 13585 | * flags bitmask of format flags; only F_ALT is looked at |
| 13586 | * prec minimum number of digits; 0-fill on left if needed |
| 13587 | * type a character in [duoxX]; u acts the same as d |
| 13588 | * |
| 13589 | * CAUTION: o, x and X conversions on regular ints can never |
| 13590 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13591 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13592 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13593 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13594 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13595 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13596 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13597 | Py_ssize_t i; |
| 13598 | int sign; /* 1 if '-', else 0 */ |
| 13599 | int len; /* number of characters */ |
| 13600 | Py_ssize_t llen; |
| 13601 | int numdigits; /* len == numnondigits + numdigits */ |
| 13602 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13603 | int prec = arg->prec; |
| 13604 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13605 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13606 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13607 | if (prec > INT_MAX-3) { |
| 13608 | PyErr_SetString(PyExc_OverflowError, |
| 13609 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13610 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13611 | } |
| 13612 | |
| 13613 | assert(PyLong_Check(val)); |
| 13614 | |
| 13615 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13616 | default: |
| 13617 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13618 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13619 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13620 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13621 | /* int and int subclasses should print numerically when a numeric */ |
| 13622 | /* format code is used (see issue18780) */ |
| 13623 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13624 | break; |
| 13625 | case 'o': |
| 13626 | numnondigits = 2; |
| 13627 | result = PyNumber_ToBase(val, 8); |
| 13628 | break; |
| 13629 | case 'x': |
| 13630 | case 'X': |
| 13631 | numnondigits = 2; |
| 13632 | result = PyNumber_ToBase(val, 16); |
| 13633 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13634 | } |
| 13635 | if (!result) |
| 13636 | return NULL; |
| 13637 | |
| 13638 | assert(unicode_modifiable(result)); |
| 13639 | assert(PyUnicode_IS_READY(result)); |
| 13640 | assert(PyUnicode_IS_ASCII(result)); |
| 13641 | |
| 13642 | /* To modify the string in-place, there can only be one reference. */ |
| 13643 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13644 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13645 | PyErr_BadInternalCall(); |
| 13646 | return NULL; |
| 13647 | } |
| 13648 | buf = PyUnicode_DATA(result); |
| 13649 | llen = PyUnicode_GET_LENGTH(result); |
| 13650 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13651 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13652 | PyErr_SetString(PyExc_ValueError, |
| 13653 | "string too large in _PyBytes_FormatLong"); |
| 13654 | return NULL; |
| 13655 | } |
| 13656 | len = (int)llen; |
| 13657 | sign = buf[0] == '-'; |
| 13658 | numnondigits += sign; |
| 13659 | numdigits = len - numnondigits; |
| 13660 | assert(numdigits > 0); |
| 13661 | |
| 13662 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13663 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13664 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13665 | assert(buf[sign] == '0'); |
| 13666 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13667 | buf[sign+1] == 'o'); |
| 13668 | numnondigits -= 2; |
| 13669 | buf += 2; |
| 13670 | len -= 2; |
| 13671 | if (sign) |
| 13672 | buf[0] = '-'; |
| 13673 | assert(len == numnondigits + numdigits); |
| 13674 | assert(numdigits > 0); |
| 13675 | } |
| 13676 | |
| 13677 | /* Fill with leading zeroes to meet minimum width. */ |
| 13678 | if (prec > numdigits) { |
| 13679 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13680 | numnondigits + prec); |
| 13681 | char *b1; |
| 13682 | if (!r1) { |
| 13683 | Py_DECREF(result); |
| 13684 | return NULL; |
| 13685 | } |
| 13686 | b1 = PyBytes_AS_STRING(r1); |
| 13687 | for (i = 0; i < numnondigits; ++i) |
| 13688 | *b1++ = *buf++; |
| 13689 | for (i = 0; i < prec - numdigits; i++) |
| 13690 | *b1++ = '0'; |
| 13691 | for (i = 0; i < numdigits; i++) |
| 13692 | *b1++ = *buf++; |
| 13693 | *b1 = '\0'; |
| 13694 | Py_DECREF(result); |
| 13695 | result = r1; |
| 13696 | buf = PyBytes_AS_STRING(result); |
| 13697 | len = numnondigits + prec; |
| 13698 | } |
| 13699 | |
| 13700 | /* Fix up case for hex conversions. */ |
| 13701 | if (type == 'X') { |
| 13702 | /* Need to convert all lower case letters to upper case. |
| 13703 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13704 | for (i = 0; i < len; i++) |
| 13705 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13706 | buf[i] -= 'a'-'A'; |
| 13707 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13708 | if (!PyUnicode_Check(result) |
| 13709 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13710 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13711 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13712 | Py_DECREF(result); |
| 13713 | result = unicode; |
| 13714 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13715 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13716 | if (PyUnicode_Resize(&result, len) < 0) |
| 13717 | Py_CLEAR(result); |
| 13718 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13719 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13720 | } |
| 13721 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13722 | /* Format an integer. |
| 13723 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13724 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13725 | * -1 and raise an exception on error */ |
| 13726 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13727 | mainformatlong(PyObject *v, |
| 13728 | struct unicode_format_arg_t *arg, |
| 13729 | PyObject **p_output, |
| 13730 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13731 | { |
| 13732 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13733 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13734 | |
| 13735 | if (!PyNumber_Check(v)) |
| 13736 | goto wrongtype; |
| 13737 | |
| 13738 | if (!PyLong_Check(v)) { |
| 13739 | iobj = PyNumber_Long(v); |
| 13740 | if (iobj == NULL) { |
| 13741 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13742 | goto wrongtype; |
| 13743 | return -1; |
| 13744 | } |
| 13745 | assert(PyLong_Check(iobj)); |
| 13746 | } |
| 13747 | else { |
| 13748 | iobj = v; |
| 13749 | Py_INCREF(iobj); |
| 13750 | } |
| 13751 | |
| 13752 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13753 | && arg->width == -1 && arg->prec == -1 |
| 13754 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13755 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13756 | { |
| 13757 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13758 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13759 | int base; |
| 13760 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13761 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13762 | { |
| 13763 | default: |
| 13764 | assert(0 && "'type' not in [diuoxX]"); |
| 13765 | case 'd': |
| 13766 | case 'i': |
| 13767 | case 'u': |
| 13768 | base = 10; |
| 13769 | break; |
| 13770 | case 'o': |
| 13771 | base = 8; |
| 13772 | break; |
| 13773 | case 'x': |
| 13774 | case 'X': |
| 13775 | base = 16; |
| 13776 | break; |
| 13777 | } |
| 13778 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13779 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13780 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13781 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13782 | } |
| 13783 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13784 | return 1; |
| 13785 | } |
| 13786 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13787 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13788 | Py_DECREF(iobj); |
| 13789 | if (res == NULL) |
| 13790 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13791 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13792 | return 0; |
| 13793 | |
| 13794 | wrongtype: |
| 13795 | PyErr_Format(PyExc_TypeError, |
| 13796 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13797 | "not %.200s", |
| 13798 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13799 | return -1; |
| 13800 | } |
| 13801 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13802 | static Py_UCS4 |
| 13803 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13804 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13805 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13806 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13807 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13808 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13809 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13810 | goto onError; |
| 13811 | } |
| 13812 | else { |
| 13813 | /* Integer input truncated to a character */ |
| 13814 | long x; |
| 13815 | x = PyLong_AsLong(v); |
| 13816 | if (x == -1 && PyErr_Occurred()) |
| 13817 | goto onError; |
| 13818 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13819 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13820 | PyErr_SetString(PyExc_OverflowError, |
| 13821 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13822 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13823 | } |
| 13824 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13825 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13826 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13828 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13829 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13830 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13831 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13832 | } |
| 13833 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13834 | /* Parse options of an argument: flags, width, precision. |
| 13835 | Handle also "%(name)" syntax. |
| 13836 | |
| 13837 | Return 0 if the argument has been formatted into arg->str. |
| 13838 | Return 1 if the argument has been written into ctx->writer, |
| 13839 | Raise an exception and return -1 on error. */ |
| 13840 | static int |
| 13841 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13842 | struct unicode_format_arg_t *arg) |
| 13843 | { |
| 13844 | #define FORMAT_READ(ctx) \ |
| 13845 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13846 | |
| 13847 | PyObject *v; |
| 13848 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13849 | if (arg->ch == '(') { |
| 13850 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13851 | Py_ssize_t keystart; |
| 13852 | Py_ssize_t keylen; |
| 13853 | PyObject *key; |
| 13854 | int pcount = 1; |
| 13855 | |
| 13856 | if (ctx->dict == NULL) { |
| 13857 | PyErr_SetString(PyExc_TypeError, |
| 13858 | "format requires a mapping"); |
| 13859 | return -1; |
| 13860 | } |
| 13861 | ++ctx->fmtpos; |
| 13862 | --ctx->fmtcnt; |
| 13863 | keystart = ctx->fmtpos; |
| 13864 | /* Skip over balanced parentheses */ |
| 13865 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13866 | arg->ch = FORMAT_READ(ctx); |
| 13867 | if (arg->ch == ')') |
| 13868 | --pcount; |
| 13869 | else if (arg->ch == '(') |
| 13870 | ++pcount; |
| 13871 | ctx->fmtpos++; |
| 13872 | } |
| 13873 | keylen = ctx->fmtpos - keystart - 1; |
| 13874 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13875 | PyErr_SetString(PyExc_ValueError, |
| 13876 | "incomplete format key"); |
| 13877 | return -1; |
| 13878 | } |
| 13879 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13880 | keystart, keystart + keylen); |
| 13881 | if (key == NULL) |
| 13882 | return -1; |
| 13883 | if (ctx->args_owned) { |
| 13884 | Py_DECREF(ctx->args); |
| 13885 | ctx->args_owned = 0; |
| 13886 | } |
| 13887 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13888 | Py_DECREF(key); |
| 13889 | if (ctx->args == NULL) |
| 13890 | return -1; |
| 13891 | ctx->args_owned = 1; |
| 13892 | ctx->arglen = -1; |
| 13893 | ctx->argidx = -2; |
| 13894 | } |
| 13895 | |
| 13896 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13897 | while (--ctx->fmtcnt >= 0) { |
| 13898 | arg->ch = FORMAT_READ(ctx); |
| 13899 | ctx->fmtpos++; |
| 13900 | switch (arg->ch) { |
| 13901 | case '-': arg->flags |= F_LJUST; continue; |
| 13902 | case '+': arg->flags |= F_SIGN; continue; |
| 13903 | case ' ': arg->flags |= F_BLANK; continue; |
| 13904 | case '#': arg->flags |= F_ALT; continue; |
| 13905 | case '0': arg->flags |= F_ZERO; continue; |
| 13906 | } |
| 13907 | break; |
| 13908 | } |
| 13909 | |
| 13910 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13911 | if (arg->ch == '*') { |
| 13912 | v = unicode_format_getnextarg(ctx); |
| 13913 | if (v == NULL) |
| 13914 | return -1; |
| 13915 | if (!PyLong_Check(v)) { |
| 13916 | PyErr_SetString(PyExc_TypeError, |
| 13917 | "* wants int"); |
| 13918 | return -1; |
| 13919 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13920 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13921 | if (arg->width == -1 && PyErr_Occurred()) |
| 13922 | return -1; |
| 13923 | if (arg->width < 0) { |
| 13924 | arg->flags |= F_LJUST; |
| 13925 | arg->width = -arg->width; |
| 13926 | } |
| 13927 | if (--ctx->fmtcnt >= 0) { |
| 13928 | arg->ch = FORMAT_READ(ctx); |
| 13929 | ctx->fmtpos++; |
| 13930 | } |
| 13931 | } |
| 13932 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13933 | arg->width = arg->ch - '0'; |
| 13934 | while (--ctx->fmtcnt >= 0) { |
| 13935 | arg->ch = FORMAT_READ(ctx); |
| 13936 | ctx->fmtpos++; |
| 13937 | if (arg->ch < '0' || arg->ch > '9') |
| 13938 | break; |
| 13939 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13940 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13941 | '0' and '9', casting to int is safe. */ |
| 13942 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13943 | PyErr_SetString(PyExc_ValueError, |
| 13944 | "width too big"); |
| 13945 | return -1; |
| 13946 | } |
| 13947 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13948 | } |
| 13949 | } |
| 13950 | |
| 13951 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13952 | if (arg->ch == '.') { |
| 13953 | arg->prec = 0; |
| 13954 | if (--ctx->fmtcnt >= 0) { |
| 13955 | arg->ch = FORMAT_READ(ctx); |
| 13956 | ctx->fmtpos++; |
| 13957 | } |
| 13958 | if (arg->ch == '*') { |
| 13959 | v = unicode_format_getnextarg(ctx); |
| 13960 | if (v == NULL) |
| 13961 | return -1; |
| 13962 | if (!PyLong_Check(v)) { |
| 13963 | PyErr_SetString(PyExc_TypeError, |
| 13964 | "* wants int"); |
| 13965 | return -1; |
| 13966 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13967 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13968 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13969 | return -1; |
| 13970 | if (arg->prec < 0) |
| 13971 | arg->prec = 0; |
| 13972 | if (--ctx->fmtcnt >= 0) { |
| 13973 | arg->ch = FORMAT_READ(ctx); |
| 13974 | ctx->fmtpos++; |
| 13975 | } |
| 13976 | } |
| 13977 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13978 | arg->prec = arg->ch - '0'; |
| 13979 | while (--ctx->fmtcnt >= 0) { |
| 13980 | arg->ch = FORMAT_READ(ctx); |
| 13981 | ctx->fmtpos++; |
| 13982 | if (arg->ch < '0' || arg->ch > '9') |
| 13983 | break; |
| 13984 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13985 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13986 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13987 | return -1; |
| 13988 | } |
| 13989 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13990 | } |
| 13991 | } |
| 13992 | } |
| 13993 | |
| 13994 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13995 | if (ctx->fmtcnt >= 0) { |
| 13996 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13997 | if (--ctx->fmtcnt >= 0) { |
| 13998 | arg->ch = FORMAT_READ(ctx); |
| 13999 | ctx->fmtpos++; |
| 14000 | } |
| 14001 | } |
| 14002 | } |
| 14003 | if (ctx->fmtcnt < 0) { |
| 14004 | PyErr_SetString(PyExc_ValueError, |
| 14005 | "incomplete format"); |
| 14006 | return -1; |
| 14007 | } |
| 14008 | return 0; |
| 14009 | |
| 14010 | #undef FORMAT_READ |
| 14011 | } |
| 14012 | |
| 14013 | /* Format one argument. Supported conversion specifiers: |
| 14014 | |
| 14015 | - "s", "r", "a": any type |
| 14016 | - "i", "d", "u", "o", "x", "X": int |
| 14017 | - "e", "E", "f", "F", "g", "G": float |
| 14018 | - "c": int or str (1 character) |
| 14019 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14020 | When possible, the output is written directly into the Unicode writer |
| 14021 | (ctx->writer). A string is created when padding is required. |
| 14022 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14023 | Return 0 if the argument has been formatted into *p_str, |
| 14024 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14025 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14026 | static int |
| 14027 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14028 | struct unicode_format_arg_t *arg, |
| 14029 | PyObject **p_str) |
| 14030 | { |
| 14031 | PyObject *v; |
| 14032 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14033 | |
| 14034 | if (ctx->fmtcnt == 0) |
| 14035 | ctx->writer.overallocate = 0; |
| 14036 | |
| 14037 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14038 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14039 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14040 | return 1; |
| 14041 | } |
| 14042 | |
| 14043 | v = unicode_format_getnextarg(ctx); |
| 14044 | if (v == NULL) |
| 14045 | return -1; |
| 14046 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14047 | |
| 14048 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14049 | case 's': |
| 14050 | case 'r': |
| 14051 | case 'a': |
| 14052 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14053 | /* Fast path */ |
| 14054 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14055 | return -1; |
| 14056 | return 1; |
| 14057 | } |
| 14058 | |
| 14059 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14060 | *p_str = v; |
| 14061 | Py_INCREF(*p_str); |
| 14062 | } |
| 14063 | else { |
| 14064 | if (arg->ch == 's') |
| 14065 | *p_str = PyObject_Str(v); |
| 14066 | else if (arg->ch == 'r') |
| 14067 | *p_str = PyObject_Repr(v); |
| 14068 | else |
| 14069 | *p_str = PyObject_ASCII(v); |
| 14070 | } |
| 14071 | break; |
| 14072 | |
| 14073 | case 'i': |
| 14074 | case 'd': |
| 14075 | case 'u': |
| 14076 | case 'o': |
| 14077 | case 'x': |
| 14078 | case 'X': |
| 14079 | { |
| 14080 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14081 | if (ret != 0) |
| 14082 | return ret; |
| 14083 | arg->sign = 1; |
| 14084 | break; |
| 14085 | } |
| 14086 | |
| 14087 | case 'e': |
| 14088 | case 'E': |
| 14089 | case 'f': |
| 14090 | case 'F': |
| 14091 | case 'g': |
| 14092 | case 'G': |
| 14093 | if (arg->width == -1 && arg->prec == -1 |
| 14094 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14095 | { |
| 14096 | /* Fast path */ |
| 14097 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14098 | return -1; |
| 14099 | return 1; |
| 14100 | } |
| 14101 | |
| 14102 | arg->sign = 1; |
| 14103 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14104 | return -1; |
| 14105 | break; |
| 14106 | |
| 14107 | case 'c': |
| 14108 | { |
| 14109 | Py_UCS4 ch = formatchar(v); |
| 14110 | if (ch == (Py_UCS4) -1) |
| 14111 | return -1; |
| 14112 | if (arg->width == -1 && arg->prec == -1) { |
| 14113 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14114 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14115 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14116 | return 1; |
| 14117 | } |
| 14118 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14119 | break; |
| 14120 | } |
| 14121 | |
| 14122 | default: |
| 14123 | PyErr_Format(PyExc_ValueError, |
| 14124 | "unsupported format character '%c' (0x%x) " |
| 14125 | "at index %zd", |
| 14126 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14127 | (int)arg->ch, |
| 14128 | ctx->fmtpos - 1); |
| 14129 | return -1; |
| 14130 | } |
| 14131 | if (*p_str == NULL) |
| 14132 | return -1; |
| 14133 | assert (PyUnicode_Check(*p_str)); |
| 14134 | return 0; |
| 14135 | } |
| 14136 | |
| 14137 | static int |
| 14138 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14139 | struct unicode_format_arg_t *arg, |
| 14140 | PyObject *str) |
| 14141 | { |
| 14142 | Py_ssize_t len; |
| 14143 | enum PyUnicode_Kind kind; |
| 14144 | void *pbuf; |
| 14145 | Py_ssize_t pindex; |
| 14146 | Py_UCS4 signchar; |
| 14147 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14148 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14149 | Py_ssize_t sublen; |
| 14150 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14151 | Py_UCS4 fill; |
| 14152 | |
| 14153 | fill = ' '; |
| 14154 | if (arg->sign && arg->flags & F_ZERO) |
| 14155 | fill = '0'; |
| 14156 | |
| 14157 | if (PyUnicode_READY(str) == -1) |
| 14158 | return -1; |
| 14159 | |
| 14160 | len = PyUnicode_GET_LENGTH(str); |
| 14161 | if ((arg->width == -1 || arg->width <= len) |
| 14162 | && (arg->prec == -1 || arg->prec >= len) |
| 14163 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14164 | { |
| 14165 | /* Fast path */ |
| 14166 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14167 | return -1; |
| 14168 | return 0; |
| 14169 | } |
| 14170 | |
| 14171 | /* Truncate the string for "s", "r" and "a" formats |
| 14172 | if the precision is set */ |
| 14173 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14174 | if (arg->prec >= 0 && len > arg->prec) |
| 14175 | len = arg->prec; |
| 14176 | } |
| 14177 | |
| 14178 | /* Adjust sign and width */ |
| 14179 | kind = PyUnicode_KIND(str); |
| 14180 | pbuf = PyUnicode_DATA(str); |
| 14181 | pindex = 0; |
| 14182 | signchar = '\0'; |
| 14183 | if (arg->sign) { |
| 14184 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14185 | if (ch == '-' || ch == '+') { |
| 14186 | signchar = ch; |
| 14187 | len--; |
| 14188 | pindex++; |
| 14189 | } |
| 14190 | else if (arg->flags & F_SIGN) |
| 14191 | signchar = '+'; |
| 14192 | else if (arg->flags & F_BLANK) |
| 14193 | signchar = ' '; |
| 14194 | else |
| 14195 | arg->sign = 0; |
| 14196 | } |
| 14197 | if (arg->width < len) |
| 14198 | arg->width = len; |
| 14199 | |
| 14200 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14201 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14202 | if (!(arg->flags & F_LJUST)) { |
| 14203 | if (arg->sign) { |
| 14204 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14205 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14206 | } |
| 14207 | else { |
| 14208 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14209 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14210 | } |
| 14211 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14212 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14213 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14214 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14215 | } |
| 14216 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14217 | buflen = arg->width; |
| 14218 | if (arg->sign && len == arg->width) |
| 14219 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14220 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14221 | return -1; |
| 14222 | |
| 14223 | /* Write the sign if needed */ |
| 14224 | if (arg->sign) { |
| 14225 | if (fill != ' ') { |
| 14226 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14227 | writer->pos += 1; |
| 14228 | } |
| 14229 | if (arg->width > len) |
| 14230 | arg->width--; |
| 14231 | } |
| 14232 | |
| 14233 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14234 | if the alternate form is used. |
| 14235 | For example, write "0x" for the "%#x" format. */ |
| 14236 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14237 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14238 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14239 | if (fill != ' ') { |
| 14240 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14241 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14242 | writer->pos += 2; |
| 14243 | pindex += 2; |
| 14244 | } |
| 14245 | arg->width -= 2; |
| 14246 | if (arg->width < 0) |
| 14247 | arg->width = 0; |
| 14248 | len -= 2; |
| 14249 | } |
| 14250 | |
| 14251 | /* Pad left with the fill character if needed */ |
| 14252 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14253 | sublen = arg->width - len; |
| 14254 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14255 | writer->pos += sublen; |
| 14256 | arg->width = len; |
| 14257 | } |
| 14258 | |
| 14259 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14260 | the alternate form is used */ |
| 14261 | if (fill == ' ') { |
| 14262 | if (arg->sign) { |
| 14263 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14264 | writer->pos += 1; |
| 14265 | } |
| 14266 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14267 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14268 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14269 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14270 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14271 | writer->pos += 2; |
| 14272 | pindex += 2; |
| 14273 | } |
| 14274 | } |
| 14275 | |
| 14276 | /* Write characters */ |
| 14277 | if (len) { |
| 14278 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14279 | str, pindex, len); |
| 14280 | writer->pos += len; |
| 14281 | } |
| 14282 | |
| 14283 | /* Pad right with the fill character if needed */ |
| 14284 | if (arg->width > len) { |
| 14285 | sublen = arg->width - len; |
| 14286 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14287 | writer->pos += sublen; |
| 14288 | } |
| 14289 | return 0; |
| 14290 | } |
| 14291 | |
| 14292 | /* Helper of PyUnicode_Format(): format one arg. |
| 14293 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14294 | static int |
| 14295 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14296 | { |
| 14297 | struct unicode_format_arg_t arg; |
| 14298 | PyObject *str; |
| 14299 | int ret; |
| 14300 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14301 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14302 | arg.flags = 0; |
| 14303 | arg.width = -1; |
| 14304 | arg.prec = -1; |
| 14305 | arg.sign = 0; |
| 14306 | str = NULL; |
| 14307 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14308 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14309 | if (ret == -1) |
| 14310 | return -1; |
| 14311 | |
| 14312 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14313 | if (ret == -1) |
| 14314 | return -1; |
| 14315 | |
| 14316 | if (ret != 1) { |
| 14317 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14318 | Py_DECREF(str); |
| 14319 | if (ret == -1) |
| 14320 | return -1; |
| 14321 | } |
| 14322 | |
| 14323 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14324 | PyErr_SetString(PyExc_TypeError, |
| 14325 | "not all arguments converted during string formatting"); |
| 14326 | return -1; |
| 14327 | } |
| 14328 | return 0; |
| 14329 | } |
| 14330 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14331 | PyObject * |
| 14332 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14333 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14334 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14336 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14337 | PyErr_BadInternalCall(); |
| 14338 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14339 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14340 | |
| 14341 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14342 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14343 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14344 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14345 | Py_DECREF(ctx.fmtstr); |
| 14346 | return NULL; |
| 14347 | } |
| 14348 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14349 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14350 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14351 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14352 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14353 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14354 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14355 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14357 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14358 | ctx.arglen = PyTuple_Size(args); |
| 14359 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14360 | } |
| 14361 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14362 | ctx.arglen = -1; |
| 14363 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14364 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14365 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14366 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14367 | ctx.dict = args; |
| 14368 | else |
| 14369 | ctx.dict = NULL; |
| 14370 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14371 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14372 | while (--ctx.fmtcnt >= 0) { |
| 14373 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14374 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14375 | |
| 14376 | nonfmtpos = ctx.fmtpos++; |
| 14377 | while (ctx.fmtcnt >= 0 && |
| 14378 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14379 | ctx.fmtpos++; |
| 14380 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14381 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14382 | if (ctx.fmtcnt < 0) { |
| 14383 | ctx.fmtpos--; |
| 14384 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14385 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14386 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14387 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14388 | nonfmtpos, ctx.fmtpos) < 0) |
| 14389 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14390 | } |
| 14391 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14392 | ctx.fmtpos++; |
| 14393 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14394 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14395 | } |
| 14396 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14397 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14398 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14399 | PyErr_SetString(PyExc_TypeError, |
| 14400 | "not all arguments converted during string formatting"); |
| 14401 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14402 | } |
| 14403 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14404 | if (ctx.args_owned) { |
| 14405 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14406 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14407 | Py_DECREF(ctx.fmtstr); |
| 14408 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14409 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14410 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14411 | Py_DECREF(ctx.fmtstr); |
| 14412 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14413 | if (ctx.args_owned) { |
| 14414 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14415 | } |
| 14416 | return NULL; |
| 14417 | } |
| 14418 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14419 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14420 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14421 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14422 | static PyObject * |
| 14423 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14424 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14425 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14426 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14427 | char *encoding = NULL; |
| 14428 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14429 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14430 | if (type != &PyUnicode_Type) |
| 14431 | return unicode_subtype_new(type, args, kwds); |
| 14432 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14433 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14434 | return NULL; |
| 14435 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14436 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14437 | if (encoding == NULL && errors == NULL) |
| 14438 | return PyObject_Str(x); |
| 14439 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14440 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14441 | } |
| 14442 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14443 | static PyObject * |
| 14444 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14445 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14446 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14447 | Py_ssize_t length, char_size; |
| 14448 | int share_wstr, share_utf8; |
| 14449 | unsigned int kind; |
| 14450 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14451 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14452 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14453 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14454 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14455 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14456 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14457 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14458 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14459 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14460 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14461 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14462 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14463 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14464 | if (self == NULL) { |
| 14465 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14466 | return NULL; |
| 14467 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14468 | kind = PyUnicode_KIND(unicode); |
| 14469 | length = PyUnicode_GET_LENGTH(unicode); |
| 14470 | |
| 14471 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14472 | #ifdef Py_DEBUG |
| 14473 | _PyUnicode_HASH(self) = -1; |
| 14474 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14475 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14476 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14477 | _PyUnicode_STATE(self).interned = 0; |
| 14478 | _PyUnicode_STATE(self).kind = kind; |
| 14479 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14480 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14481 | _PyUnicode_STATE(self).ready = 1; |
| 14482 | _PyUnicode_WSTR(self) = NULL; |
| 14483 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14484 | _PyUnicode_UTF8(self) = NULL; |
| 14485 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14486 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14487 | |
| 14488 | share_utf8 = 0; |
| 14489 | share_wstr = 0; |
| 14490 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14491 | char_size = 1; |
| 14492 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14493 | share_utf8 = 1; |
| 14494 | } |
| 14495 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14496 | char_size = 2; |
| 14497 | if (sizeof(wchar_t) == 2) |
| 14498 | share_wstr = 1; |
| 14499 | } |
| 14500 | else { |
| 14501 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14502 | char_size = 4; |
| 14503 | if (sizeof(wchar_t) == 4) |
| 14504 | share_wstr = 1; |
| 14505 | } |
| 14506 | |
| 14507 | /* Ensure we won't overflow the length. */ |
| 14508 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14509 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14510 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14511 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14512 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14513 | if (data == NULL) { |
| 14514 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14515 | goto onError; |
| 14516 | } |
| 14517 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14518 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14519 | if (share_utf8) { |
| 14520 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14521 | _PyUnicode_UTF8(self) = data; |
| 14522 | } |
| 14523 | if (share_wstr) { |
| 14524 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14525 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14526 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14527 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14528 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14529 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14530 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14531 | #ifdef Py_DEBUG |
| 14532 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14533 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14534 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14535 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14536 | |
| 14537 | onError: |
| 14538 | Py_DECREF(unicode); |
| 14539 | Py_DECREF(self); |
| 14540 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14541 | } |
| 14542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14543 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14544 | "str(object='') -> str\n\ |
| 14545 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14546 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14547 | Create a new string object from the given object. If encoding or\n\ |
| 14548 | errors is specified, then the object must expose a data buffer\n\ |
| 14549 | that will be decoded using the given encoding and error handler.\n\ |
| 14550 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14551 | or repr(object).\n\ |
| 14552 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14553 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14554 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14555 | static PyObject *unicode_iter(PyObject *seq); |
| 14556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14557 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14558 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14559 | "str", /* tp_name */ |
| 14560 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14561 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14562 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14563 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14564 | 0, /* tp_print */ |
| 14565 | 0, /* tp_getattr */ |
| 14566 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14567 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14568 | unicode_repr, /* tp_repr */ |
| 14569 | &unicode_as_number, /* tp_as_number */ |
| 14570 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14571 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14572 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14573 | 0, /* tp_call*/ |
| 14574 | (reprfunc) unicode_str, /* tp_str */ |
| 14575 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14576 | 0, /* tp_setattro */ |
| 14577 | 0, /* tp_as_buffer */ |
| 14578 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14579 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14580 | unicode_doc, /* tp_doc */ |
| 14581 | 0, /* tp_traverse */ |
| 14582 | 0, /* tp_clear */ |
| 14583 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14584 | 0, /* tp_weaklistoffset */ |
| 14585 | unicode_iter, /* tp_iter */ |
| 14586 | 0, /* tp_iternext */ |
| 14587 | unicode_methods, /* tp_methods */ |
| 14588 | 0, /* tp_members */ |
| 14589 | 0, /* tp_getset */ |
| 14590 | &PyBaseObject_Type, /* tp_base */ |
| 14591 | 0, /* tp_dict */ |
| 14592 | 0, /* tp_descr_get */ |
| 14593 | 0, /* tp_descr_set */ |
| 14594 | 0, /* tp_dictoffset */ |
| 14595 | 0, /* tp_init */ |
| 14596 | 0, /* tp_alloc */ |
| 14597 | unicode_new, /* tp_new */ |
| 14598 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14599 | }; |
| 14600 | |
| 14601 | /* Initialize the Unicode implementation */ |
| 14602 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14603 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14604 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14605 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14606 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14607 | 0x000A, /* LINE FEED */ |
| 14608 | 0x000D, /* CARRIAGE RETURN */ |
| 14609 | 0x001C, /* FILE SEPARATOR */ |
| 14610 | 0x001D, /* GROUP SEPARATOR */ |
| 14611 | 0x001E, /* RECORD SEPARATOR */ |
| 14612 | 0x0085, /* NEXT LINE */ |
| 14613 | 0x2028, /* LINE SEPARATOR */ |
| 14614 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14615 | }; |
| 14616 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14617 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14618 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14619 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14620 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14621 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14622 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14623 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14624 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14625 | |
| 14626 | /* initialize the linebreak bloom filter */ |
| 14627 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14628 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14629 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14630 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14631 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14632 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14633 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14634 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14635 | Py_FatalError("Can't initialize field name iterator type"); |
| 14636 | |
| 14637 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14638 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14639 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14640 | #ifdef HAVE_MBCS |
| 14641 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14642 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14643 | PyErr_SetFromWindowsErr(0); |
| 14644 | return -1; |
| 14645 | } |
| 14646 | #endif |
| 14647 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14648 | } |
| 14649 | |
| 14650 | /* Finalize the Unicode implementation */ |
| 14651 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14652 | int |
| 14653 | PyUnicode_ClearFreeList(void) |
| 14654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14655 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14656 | } |
| 14657 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14658 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14659 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14660 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14661 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14662 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14663 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14664 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14665 | for (i = 0; i < 256; i++) |
| 14666 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14667 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14668 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14669 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14670 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14671 | void |
| 14672 | PyUnicode_InternInPlace(PyObject **p) |
| 14673 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14674 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14675 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14676 | #ifdef Py_DEBUG |
| 14677 | assert(s != NULL); |
| 14678 | assert(_PyUnicode_CHECK(s)); |
| 14679 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14680 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14681 | return; |
| 14682 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14683 | /* If it's a subclass, we don't really know what putting |
| 14684 | it in the interned dict might do. */ |
| 14685 | if (!PyUnicode_CheckExact(s)) |
| 14686 | return; |
| 14687 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14688 | return; |
| 14689 | if (interned == NULL) { |
| 14690 | interned = PyDict_New(); |
| 14691 | if (interned == NULL) { |
| 14692 | PyErr_Clear(); /* Don't leave an exception */ |
| 14693 | return; |
| 14694 | } |
| 14695 | } |
| 14696 | /* It might be that the GetItem call fails even |
| 14697 | though the key is present in the dictionary, |
| 14698 | namely when this happens during a stack overflow. */ |
| 14699 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14700 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14701 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14702 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14703 | if (t) { |
| 14704 | Py_INCREF(t); |
| 14705 | Py_DECREF(*p); |
| 14706 | *p = t; |
| 14707 | return; |
| 14708 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14709 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14710 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14711 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14712 | PyErr_Clear(); |
| 14713 | PyThreadState_GET()->recursion_critical = 0; |
| 14714 | return; |
| 14715 | } |
| 14716 | PyThreadState_GET()->recursion_critical = 0; |
| 14717 | /* The two references in interned are not counted by refcnt. |
| 14718 | The deallocator will take care of this */ |
| 14719 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14720 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14721 | } |
| 14722 | |
| 14723 | void |
| 14724 | PyUnicode_InternImmortal(PyObject **p) |
| 14725 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14726 | PyUnicode_InternInPlace(p); |
| 14727 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14728 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14729 | Py_INCREF(*p); |
| 14730 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14731 | } |
| 14732 | |
| 14733 | PyObject * |
| 14734 | PyUnicode_InternFromString(const char *cp) |
| 14735 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14736 | PyObject *s = PyUnicode_FromString(cp); |
| 14737 | if (s == NULL) |
| 14738 | return NULL; |
| 14739 | PyUnicode_InternInPlace(&s); |
| 14740 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14741 | } |
| 14742 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14743 | void |
| 14744 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14745 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14746 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14747 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14748 | Py_ssize_t i, n; |
| 14749 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14750 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14751 | if (interned == NULL || !PyDict_Check(interned)) |
| 14752 | return; |
| 14753 | keys = PyDict_Keys(interned); |
| 14754 | if (keys == NULL || !PyList_Check(keys)) { |
| 14755 | PyErr_Clear(); |
| 14756 | return; |
| 14757 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14758 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14759 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14760 | detector, interned unicode strings are not forcibly deallocated; |
| 14761 | rather, we give them their stolen references back, and then clear |
| 14762 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14763 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14764 | n = PyList_GET_SIZE(keys); |
| 14765 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14766 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14767 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14768 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14769 | if (PyUnicode_READY(s) == -1) { |
| 14770 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14771 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14772 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14773 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14774 | case SSTATE_NOT_INTERNED: |
| 14775 | /* XXX Shouldn't happen */ |
| 14776 | break; |
| 14777 | case SSTATE_INTERNED_IMMORTAL: |
| 14778 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14779 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14780 | break; |
| 14781 | case SSTATE_INTERNED_MORTAL: |
| 14782 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14783 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14784 | break; |
| 14785 | default: |
| 14786 | Py_FatalError("Inconsistent interned string state."); |
| 14787 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14788 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14789 | } |
| 14790 | fprintf(stderr, "total size of all interned strings: " |
| 14791 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14792 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14793 | Py_DECREF(keys); |
| 14794 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14795 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14796 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14797 | |
| 14798 | |
| 14799 | /********************* Unicode Iterator **************************/ |
| 14800 | |
| 14801 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14802 | PyObject_HEAD |
| 14803 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14804 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14805 | } unicodeiterobject; |
| 14806 | |
| 14807 | static void |
| 14808 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14809 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14810 | _PyObject_GC_UNTRACK(it); |
| 14811 | Py_XDECREF(it->it_seq); |
| 14812 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14813 | } |
| 14814 | |
| 14815 | static int |
| 14816 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14817 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14818 | Py_VISIT(it->it_seq); |
| 14819 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14820 | } |
| 14821 | |
| 14822 | static PyObject * |
| 14823 | unicodeiter_next(unicodeiterobject *it) |
| 14824 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14825 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14826 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14827 | assert(it != NULL); |
| 14828 | seq = it->it_seq; |
| 14829 | if (seq == NULL) |
| 14830 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14831 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14833 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14834 | int kind = PyUnicode_KIND(seq); |
| 14835 | void *data = PyUnicode_DATA(seq); |
| 14836 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14837 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14838 | if (item != NULL) |
| 14839 | ++it->it_index; |
| 14840 | return item; |
| 14841 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14842 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14843 | Py_DECREF(seq); |
| 14844 | it->it_seq = NULL; |
| 14845 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14846 | } |
| 14847 | |
| 14848 | static PyObject * |
| 14849 | unicodeiter_len(unicodeiterobject *it) |
| 14850 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14851 | Py_ssize_t len = 0; |
| 14852 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14853 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14854 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14855 | } |
| 14856 | |
| 14857 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14858 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14859 | static PyObject * |
| 14860 | unicodeiter_reduce(unicodeiterobject *it) |
| 14861 | { |
| 14862 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14863 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14864 | it->it_seq, it->it_index); |
| 14865 | } else { |
| 14866 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14867 | if (u == NULL) |
| 14868 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14869 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14870 | } |
| 14871 | } |
| 14872 | |
| 14873 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14874 | |
| 14875 | static PyObject * |
| 14876 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14877 | { |
| 14878 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14879 | if (index == -1 && PyErr_Occurred()) |
| 14880 | return NULL; |
| 14881 | if (index < 0) |
| 14882 | index = 0; |
| 14883 | it->it_index = index; |
| 14884 | Py_RETURN_NONE; |
| 14885 | } |
| 14886 | |
| 14887 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14888 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14889 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14890 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14891 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14892 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14893 | reduce_doc}, |
| 14894 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14895 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14896 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14897 | }; |
| 14898 | |
| 14899 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14900 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14901 | "str_iterator", /* tp_name */ |
| 14902 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14903 | 0, /* tp_itemsize */ |
| 14904 | /* methods */ |
| 14905 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14906 | 0, /* tp_print */ |
| 14907 | 0, /* tp_getattr */ |
| 14908 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14909 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14910 | 0, /* tp_repr */ |
| 14911 | 0, /* tp_as_number */ |
| 14912 | 0, /* tp_as_sequence */ |
| 14913 | 0, /* tp_as_mapping */ |
| 14914 | 0, /* tp_hash */ |
| 14915 | 0, /* tp_call */ |
| 14916 | 0, /* tp_str */ |
| 14917 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14918 | 0, /* tp_setattro */ |
| 14919 | 0, /* tp_as_buffer */ |
| 14920 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14921 | 0, /* tp_doc */ |
| 14922 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14923 | 0, /* tp_clear */ |
| 14924 | 0, /* tp_richcompare */ |
| 14925 | 0, /* tp_weaklistoffset */ |
| 14926 | PyObject_SelfIter, /* tp_iter */ |
| 14927 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14928 | unicodeiter_methods, /* tp_methods */ |
| 14929 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14930 | }; |
| 14931 | |
| 14932 | static PyObject * |
| 14933 | unicode_iter(PyObject *seq) |
| 14934 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14935 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14936 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14937 | if (!PyUnicode_Check(seq)) { |
| 14938 | PyErr_BadInternalCall(); |
| 14939 | return NULL; |
| 14940 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14941 | if (PyUnicode_READY(seq) == -1) |
| 14942 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14943 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14944 | if (it == NULL) |
| 14945 | return NULL; |
| 14946 | it->it_index = 0; |
| 14947 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14948 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14949 | _PyObject_GC_TRACK(it); |
| 14950 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14951 | } |
| 14952 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14953 | |
| 14954 | size_t |
| 14955 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14956 | { |
| 14957 | int res = 0; |
| 14958 | while(*u++) |
| 14959 | res++; |
| 14960 | return res; |
| 14961 | } |
| 14962 | |
| 14963 | Py_UNICODE* |
| 14964 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14965 | { |
| 14966 | Py_UNICODE *u = s1; |
| 14967 | while ((*u++ = *s2++)); |
| 14968 | return s1; |
| 14969 | } |
| 14970 | |
| 14971 | Py_UNICODE* |
| 14972 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14973 | { |
| 14974 | Py_UNICODE *u = s1; |
| 14975 | while ((*u++ = *s2++)) |
| 14976 | if (n-- == 0) |
| 14977 | break; |
| 14978 | return s1; |
| 14979 | } |
| 14980 | |
| 14981 | Py_UNICODE* |
| 14982 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14983 | { |
| 14984 | Py_UNICODE *u1 = s1; |
| 14985 | u1 += Py_UNICODE_strlen(u1); |
| 14986 | Py_UNICODE_strcpy(u1, s2); |
| 14987 | return s1; |
| 14988 | } |
| 14989 | |
| 14990 | int |
| 14991 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14992 | { |
| 14993 | while (*s1 && *s2 && *s1 == *s2) |
| 14994 | s1++, s2++; |
| 14995 | if (*s1 && *s2) |
| 14996 | return (*s1 < *s2) ? -1 : +1; |
| 14997 | if (*s1) |
| 14998 | return 1; |
| 14999 | if (*s2) |
| 15000 | return -1; |
| 15001 | return 0; |
| 15002 | } |
| 15003 | |
| 15004 | int |
| 15005 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15006 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15007 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15008 | for (; n != 0; n--) { |
| 15009 | u1 = *s1; |
| 15010 | u2 = *s2; |
| 15011 | if (u1 != u2) |
| 15012 | return (u1 < u2) ? -1 : +1; |
| 15013 | if (u1 == '\0') |
| 15014 | return 0; |
| 15015 | s1++; |
| 15016 | s2++; |
| 15017 | } |
| 15018 | return 0; |
| 15019 | } |
| 15020 | |
| 15021 | Py_UNICODE* |
| 15022 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15023 | { |
| 15024 | const Py_UNICODE *p; |
| 15025 | for (p = s; *p; p++) |
| 15026 | if (*p == c) |
| 15027 | return (Py_UNICODE*)p; |
| 15028 | return NULL; |
| 15029 | } |
| 15030 | |
| 15031 | Py_UNICODE* |
| 15032 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15033 | { |
| 15034 | const Py_UNICODE *p; |
| 15035 | p = s + Py_UNICODE_strlen(s); |
| 15036 | while (p != s) { |
| 15037 | p--; |
| 15038 | if (*p == c) |
| 15039 | return (Py_UNICODE*)p; |
| 15040 | } |
| 15041 | return NULL; |
| 15042 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15043 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15044 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15045 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15046 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15047 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15048 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15049 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15050 | if (!PyUnicode_Check(unicode)) { |
| 15051 | PyErr_BadArgument(); |
| 15052 | return NULL; |
| 15053 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15054 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15055 | if (u == NULL) |
| 15056 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15057 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15058 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15059 | PyErr_NoMemory(); |
| 15060 | return NULL; |
| 15061 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15062 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15063 | size *= sizeof(Py_UNICODE); |
| 15064 | copy = PyMem_Malloc(size); |
| 15065 | if (copy == NULL) { |
| 15066 | PyErr_NoMemory(); |
| 15067 | return NULL; |
| 15068 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15069 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15070 | return copy; |
| 15071 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15072 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15073 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15074 | to the string.Formatter class implemented in Python. */ |
| 15075 | |
| 15076 | static PyMethodDef _string_methods[] = { |
| 15077 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15078 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15079 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15080 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15081 | {NULL, NULL} |
| 15082 | }; |
| 15083 | |
| 15084 | static struct PyModuleDef _string_module = { |
| 15085 | PyModuleDef_HEAD_INIT, |
| 15086 | "_string", |
| 15087 | PyDoc_STR("string helper module"), |
| 15088 | 0, |
| 15089 | _string_methods, |
| 15090 | NULL, |
| 15091 | NULL, |
| 15092 | NULL, |
| 15093 | NULL |
| 15094 | }; |
| 15095 | |
| 15096 | PyMODINIT_FUNC |
| 15097 | PyInit__string(void) |
| 15098 | { |
| 15099 | return PyModule_Create(&_string_module); |
| 15100 | } |
| 15101 | |
| 15102 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15103 | #ifdef __cplusplus |
| 15104 | } |
| 15105 | #endif |